how to control snake with only two keys i.e left and right

会有一股神秘感。 提交于 2021-02-05 08:36:25

问题


currently, i'm using all four keys to steer the snake left, right, up and down. I'm wondering how can i only use left and right key to move the snake around.

                    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

can anyone guide me how to do that?


回答1:


Define the directions as follows:

  • 0: move up
  • 1: move right
  • 2: move down
  • 3: move right
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 = 0
        self.dx = -self.block
    if self.direction is 3:
        self.dy = self.block
        self.dx = 0

    self.x += self.dx
    self.y += self.dy

When right is pressed then add 1 to snake.direction and when left is pressed the subtract 1. Use the % (modulo) operator (see Binary arithmetic operations) to ensure tha the result is in rage [0, 3]:

if event.key == pygame.K_LEFT:
    snake.direction = (snake.direction - 1) % 4
if event.key == pygame.K_RIGHT:
    snake.direction = (snake.direction + 1) % 4



回答2:


                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

This should rotate your snake based on the direction it was traveling before.




回答3:


Rather than set the direction based on keypress, have the left and right keys adjust the direction by adding or subtracting from the current direction.

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


来源:https://stackoverflow.com/questions/61862293/how-to-control-snake-with-only-two-keys-i-e-left-and-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!