为什么我的蛇不出现在屏幕上?

时间:2016-04-17 17:51:23

标签: python class pygame sprite

以下是我的代码的相关部分:

# 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])

蛇类:

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

在游戏循环中:

        # 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()

最初代码在snake类中有方向更改位,它工作正常!在分离时,首先图像根本不会旋转。稍后稍微改动一下,蛇甚至不再出现在屏幕上。我可以很容易地恢复原始代码(我保存了),但是我试图将类和功能分开,以便将来可以制作其他类型的蛇。帮助我理解这一点。

0 个答案:

没有答案