如何仅用两个键即左和右控制蛇

目前,我正在使用所有四个键来左右操纵蛇。我想知道如何只用左右键移动蛇。

                    if event.key == pygame.K_LEFT:
                        snake.direction = 2
                    elif event.key == pygame.K_RIGHT:
                        snake.direction = 3
                    elif event.key == pygame.K_UP:
                        snake.direction = 0
                    elif event.key == pygame.K_DOWN:
                        snake.direction = 1
    def move(self):
        if self.direction is 0:
            self.dy = -self.block
            self.dx = 0
        if self.direction is 1:
            self.dy = self.block
            self.dx = 0
        if self.direction is 2:
            self.dy = 0
            self.dx = -self.block
        if self.direction is 3:
            self.dy = 0
            self.dx = self.block
        self.x += self.dx
        self.y += self.dy

谁能指导我该怎么做?

评论
  • sit_in
    sit_in 回复

                    if event.key == pygame.K_LEFT:
                        if snake.direction == 0
                            snake.direction = 2
                        elif snake.direction == 2
                            snake.direction = 1
                        elif snake.direction == 1
                            snake.direction = 3
                        elif snake.direction == 3
                            snake.direction = 0
                    elif event.key == pygame.K_RIGHT:
                        if snake.direction == 0
                            snake.direction = 3
                        elif snake.direction == 3
                            snake.direction = 1
                        elif snake.direction == 1
                            snake.direction = 2
                        elif snake.direction == 2
                            snake.direction = 0
    def move(self):
        if self.direction is 0:
            self.dy = -self.block
            self.dx = 0
        if self.direction is 1:
            self.dy = self.block
            self.dx = 0
        if self.direction is 2:
            self.dy = 0
            self.dx = -self.block
        if self.direction is 3:
            self.dy = 0
            self.dx = self.block
        self.x += self.dx
        self.y += self.dy
    
    这应该根据蛇的行进方向旋转蛇。

  • 给爷站住
    给爷站住 回复

    左右键不是通过按键设置方向,而是通过在当前方向上增加或减少来调整方向。

    I've also changed the move function so that the directions are in clockwise order.

                    if event.key == pygame.K_LEFT:
                        snake.direction -= 1
                    elif event.key == pygame.K_RIGHT:
                        snake.direction += 1
    
                    if snake.direction > 3:
                        snake.direction = 0
                    elif snake.direction < 0:
                        snake.direction = 3
    def move(self):
        if self.direction is 0:
            self.dy = -self.block
            self.dx = 0
        if self.direction is 1:
            self.dy = 0
            self.dx = -self.block
        if self.direction is 2:
            self.dy = self.block
            self.dx = 0
        if self.direction is 3:
            self.dy = 0
            self.dx = self.block
        self.x += self.dx
        self.y += self.dy