0

Here are the relevant parts of my code:

# Changes the direction of the snake image depending on the direction the snake is moving. 
# Or at least, it's supposed to.
def snake_head_direction(direction, snake):
        if direction == "right":
            image = pygame.transform.rotate(snake.image, 270)
        elif direction == "left":
            image = pygame.transform.rotate(snake.image, 90)
        elif direction == "down":
            image = pygame.transform.rotate(snake.image, 180)
        else:
            image = snake.image
        gameDisplay.blit(image, (snake.trail[-1][0], snake.trail[-1][1]))
        for XnY in snake.trail[:-1]:
            pygame.draw.rect(gameDisplay, GREEN, [XnY[0], XnY[1], snake.size, snake.size])

Snake class:

class Snake:
    def __init__(self, image, size, trail, start_size):
        self.image = image
        self.size = size
        self.trail = trail
        self.start_size = start_size

Inside the game loop:

        # Handles arrow key and WASD events.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                if event.key in (pygame.K_LEFT, pygame.K_a):
                    lead_x_change = move_speed_neg
                    lead_y_change = 0
                    snake_head_direction("left", player_snake)
                elif event.key in (pygame.K_RIGHT, pygame.K_d):
                    lead_x_change = move_speed
                    lead_y_change = 0
                    snake_head_direction("right", player_snake)
                elif event.key in (pygame.K_UP, pygame.K_w):
                    lead_y_change = move_speed_neg
                    lead_x_change = 0
                    snake_head_direction("up", player_snake)
                elif event.key in (pygame.K_DOWN, pygame.K_s):
                    lead_y_change = move_speed
                    lead_x_change = 0
                    snake_head_direction("down", player_snake)
                elif event.key in (pygame.K_p, pygame.K_ESCAPE):
                    pause()

Originally the code had the directional changing bits inside of the snake class and it worked fine! Upon separating it, at first the image would not rotate at all. A few minor changes later and the snake no longer even appears on the screen. I could easily revert to the original code (which I saved) but I'm trying to separate the class and function so I can make other types of snakes in the future. Help me understand this.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
austi
  • 5
  • 3
  • Seeing as how you have zero comments I would need to see all code. Including how you create the snake and pass it's parameters! Thanks – TheLazyScripter Apr 17 '16 at 18:06
  • By the way: having more code inside the snake class is a more object-orientated approach - and that's surely nothing bad. Don't understand, why you move code outside of the snake class. Could you post original code as well? – AnnoSiedler Apr 17 '16 at 18:15
  • Where are you adding the snake head to the snake list? Also, how are the variables like move_speed in the event look passed to the snake? – marienbad Apr 18 '16 at 00:02

0 Answers0