如何只用左右两个键控制蛇
how to control snake with only two keys i.e left and right
目前,我正在使用所有四个键来向左、向右、向上和向下操纵蛇。我想知道如何只使用左右键来移动蛇。
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
谁能指导我怎么做?
不是根据按键设置方向,而是让左右键通过增加或减少当前方向来调整方向。
我还更改了 move
函数,使方向按顺时针顺序排列。
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
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
这应该会根据它之前行进的方向旋转你的蛇。
定义方向如下:
- 0:向上移动
- 1:向右移动
- 2:下移
- 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 = 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
当按下 right 时,snake.direction
加 1,当按下 left 时,减 1。使用 %
(取模)运算符(参见 Binary arithmetic operations)以确保结果在 [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
目前,我正在使用所有四个键来向左、向右、向上和向下操纵蛇。我想知道如何只使用左右键来移动蛇。
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
谁能指导我怎么做?
不是根据按键设置方向,而是让左右键通过增加或减少当前方向来调整方向。
我还更改了 move
函数,使方向按顺时针顺序排列。
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
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
这应该会根据它之前行进的方向旋转你的蛇。
定义方向如下:
- 0:向上移动
- 1:向右移动
- 2:下移
- 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 = 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
当按下 right 时,snake.direction
加 1,当按下 left 时,减 1。使用 %
(取模)运算符(参见 Binary arithmetic operations)以确保结果在 [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