defupdate(): global direction # 将direction设为全局变量 if keyboard.left: direction = 'left' if keyboard.right: direction = 'right' if keyboard.up: direction = 'up' if keyboard.down: direction = 'down' newSnakeHead = Actor('snake1') # 创建新蛇头 if direction == 'up': newSnakeHead.x = Snake[0].x newSnakeHead.y = Snake[0].y - TILE_SIZE if direction == 'down': newSnakeHead.x = Snake[0].x newSnakeHead.y = Snake[0].y + TILE_SIZE if direction == 'left': newSnakeHead.x = Snake[0].x - TILE_SIZE newSnakeHead.y = Snake[0].y if direction == 'right': newSnakeHead.x = Snake[0].x + TILE_SIZE newSnakeHead.y = Snake[0].y Snake.insert(0, newSnakeHead) del Snake[len(Snake) - 1] time.sleep(0.2)