Conceptualization of historical geography and its research objectives
时间: 2024-05-14 22:15:54 浏览: 101
采茶机器人设计.doc
5星 · 资源好评率100%
好的,下面是使用arcade库实现双人贪吃蛇的代码:
```python
import arcade
import random
# 定义贪吃蛇
class Snake:
def __init__(self, x, y):
self.x = x
self.y = y
self.w = 20 # 定义蛇头的大小
self.speed = 20 # 定义每次移动的距离
self.dir_x = self.speed
self.dir_y = 0
self.body = [[x, y], [x-self.w, y], [x-self.w*2, y]] # 初始化蛇身列表
self.color = arcade.color.YELLOW_ORANGE # 定义颜色
# 更新蛇的位置和方向
def update(self):
self.x += self.dir_x
self.y += self.dir_y
self.body.insert(0, [self.x, self.y])
self.body.pop()
# 绘制蛇
def draw(self):
arcade.draw_rectangle_filled(self.x, self.y, self.w, self.w, self.color)
for i in range(1, len(self.body)):
arcade.draw_rectangle_filled(self.body[i][0], self.body[i][1], self.w, self.w, arcade.color.YELLOW_ORANGE)
# 定义食物
class Food:
def __init__(self):
self.x = random.randint(1, 39) * 20 # 随机生成食物的位置
self.y = random.randint(1, 39) * 20
self.w = 20 # 食物的大小
self.color = arcade.color.RED # 颜色
# 重新生成食物
def respawn(self):
self.x = random.randint(1, 39) * 20
self.y = random.randint(1, 39) * 20
# 绘制食物
def draw(self):
arcade.draw_rectangle_filled(self.x, self.y, self.w, self.w, self.color)
class SnakeGame(arcade.Window):
def __init__(self):
super().__init__(800, 800, "双人贪吃蛇")
arcade.set_background_color(arcade.color.BLACK)
self.snake1 = Snake(200, 200) # 初始化两只蛇
self.snake2 = Snake(600, 600)
self.food = Food() # 初始化食物
# 检查蛇是否碰到边缘或者碰到对手
def check_collisions(self):
if self.snake1.x < 0 or self.snake1.x > 780 or self.snake1.y < 0 or self.snake1.y > 780:
arcade.close_window()
print("Snake 2 Wins!")
if self.snake2.x < 0 or self.snake2.x > 780 or self.snake2.y < 0 or self.snake2.y > 780:
arcade.close_window()
print("Snake 1 Wins!")
for i in range(1, len(self.snake1.body)):
if self.snake1.x == self.snake1.body[i][0] and self.snake1.y == self.snake1.body[i][1]:
arcade.close_window()
print("Snake 2 Wins!")
for i in range(1, len(self.snake2.body)):
if self.snake2.x == self.snake2.body[i][0] and self.snake2.y == self.snake2.body[i][1]:
arcade.close_window()
print("Snake 1 Wins!")
if self.snake1.x == self.snake2.x and self.snake1.y == self.snake2.y:
arcade.close_window()
print("Draw!")
# 蛇吃到食物
def check_eat(self):
if self.snake1.x == self.food.x and self.snake1.y == self.food.y:
self.food.respawn()
self.snake1.body.append([self.snake1.x, self.snake1.y])
if self.snake2.x == self.food.x and self.snake2.y == self.food.y:
self.food.respawn()
self.snake2.body.append([self.snake2.x, self.snake2.y])
# 检查键盘输入
def on_key_press(self, key, modifiers):
if key == arcade.key.LEFT and self.snake1.dir_x != self.snake1.speed:
self.snake1.dir_x = -self.snake1.speed
self.snake1.dir_y = 0
elif key == arcade.key.RIGHT and self.snake1.dir_x != -self.snake1.speed:
self.snake1.dir_x = self.snake1.speed
self.snake1.dir_y = 0
elif key == arcade.key.UP and self.snake1.dir_y != -self.snake1.speed:
self.snake1.dir_x = 0
self.snake1.dir_y = self.snake1.speed
elif key == arcade.key.DOWN and self.snake1.dir_y != self.snake1.speed:
self.snake1.dir_x = 0
self.snake1.dir_y = -self.snake1.speed
elif key == arcade.key.A and self.snake2.dir_x != self.snake2.speed:
self.snake2.dir_x = -self.snake2.speed
self.snake2.dir_y = 0
elif key == arcade.key.D and self.snake2.dir_x != -self.snake2.speed:
self.snake2.dir_x = self.snake2.speed
self.snake2.dir_y = 0
elif key == arcade.key.W and self.snake2.dir_y != -self.snake2.speed:
self.snake2.dir_x = 0
self.snake2.dir_y = self.snake2.speed
elif key == arcade.key.S and self.snake2.dir_y != self.snake2.speed:
self.snake2.dir_x = 0
self.snake2.dir_y = -self.snake2.speed
# 每帧更新
def on_draw(self):
arcade.start_render()
self.snake1.draw()
self.snake2.draw()
self.food.draw()
# 更新位置
def update(self, delta_time):
self.snake1.update()
self.snake2.update()
self.check_collisions()
self.check_eat()
if __name__ == '__main__':
game = SnakeGame()
arcade.run()
```
希望对你有所帮助!
阅读全文