Skip to content Skip to sidebar Skip to footer

How To Control Snake With Only Two Keys I.e Left And Right

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

Solution 1:

if event.key == pygame.K_LEFT:
                    if snake.direction == 0
                        snake.direction = 2elif snake.direction == 2
                        snake.direction = 1elif snake.direction == 1
                        snake.direction = 3elif snake.direction == 3
                        snake.direction = 0elif event.key == pygame.K_RIGHT:
                    if snake.direction == 0
                        snake.direction = 3elif snake.direction == 3
                        snake.direction = 1elif snake.direction == 1
                        snake.direction = 2elif snake.direction == 2
                        snake.direction = 0defmove(self):
    if self.direction is0:
        self.dy = -self.block
        self.dx = 0if self.direction is1:
        self.dy = self.block
        self.dx = 0if self.direction is2:
        self.dy = 0
        self.dx = -self.block
    if self.direction is3:
        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.

Solution 2:

Define the directions as follows:

  • 0: move up
  • 1: move right
  • 2: move down
  • 3: move right
defmove(self):
    if self.direction is0:
        self.dy = -self.block
        self.dx = 0if self.direction is1:
        self.dy = 0
        self.dx = self.block
    if self.direction is2:
        self.dy = 0
        self.dx = -self.block
    if self.direction is3:
        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) % 4if event.key == pygame.K_RIGHT:
    snake.direction = (snake.direction + 1) % 4

Solution 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 -= 1elif event.key == pygame.K_RIGHT:
                    snake.direction += 1if snake.direction > 3:
                    snake.direction = 0elif snake.direction < 0:
                    snake.direction = 3defmove(self):
    if self.direction is0:
        self.dy = -self.block
        self.dx = 0if self.direction is1:
        self.dy = 0
        self.dx = -self.block
    if self.direction is2:
        self.dy = self.block
        self.dx = 0if self.direction is3:
        self.dy = 0
        self.dx = self.block
    self.x += self.dx
    self.y += self.dy

Post a Comment for "How To Control Snake With Only Two Keys I.e Left And Right"