目前,我正在使用所有四个键来左右操纵蛇。我想知道如何只用左右键移动蛇。
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
谁能指导我该怎么做?
左右键不是通过按键设置方向,而是通过在当前方向上增加或减少来调整方向。
I've also changed the
move
function so that the directions are in clockwise order.