请帮我写一份简单双人贪吃蛇python游戏
时间: 2023-08-16 18:05:24 浏览: 85
Python编写一个贪吃蛇游戏
好的,下面是一份简单的双人贪吃蛇Python游戏的代码,希望对你有所帮助:
```python
import tkinter as tk
import random
# 定义游戏区域大小
WIDTH = 500
HEIGHT = 500
# 定义蛇的初始长度
INIT_LENGTH = 5
# 定义每个格子的大小
GRID_SIZE = 20
# 定义蛇的移动速度
SPEED = 100
# 定义蛇的颜色
SNAKE_COLOR = "green"
# 定义食物的颜色
FOOD_COLOR = "red"
# 定义蛇的方向
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
class Snake:
def __init__(self, canvas, x, y, length, color):
self.canvas = canvas
self.x = x
self.y = y
self.length = length
self.color = color
self.direction = random.randint(1, 4)
self.body = []
for i in range(length):
self.body.append(canvas.create_rectangle(x-i*GRID_SIZE, y, x-(i-1)*GRID_SIZE, y+GRID_SIZE, fill=color))
def move(self):
if self.direction == UP:
self.y -= GRID_SIZE
elif self.direction == DOWN:
self.y += GRID_SIZE
elif self.direction == LEFT:
self.x -= GRID_SIZE
elif self.direction == RIGHT:
self.x += GRID_SIZE
for i in range(self.length-1, 0, -1):
self.canvas.coords(self.body[i], *self.canvas.coords(self.body[i-1]))
self.canvas.coords(self.body[0], self.x, self.y, self.x+GRID_SIZE, self.y+GRID_SIZE)
def change_direction(self, direction):
if direction == UP and self.direction != DOWN:
self.direction = UP
elif direction == DOWN and self.direction != UP:
self.direction = DOWN
elif direction == LEFT and self.direction != RIGHT:
self.direction = LEFT
elif direction == RIGHT and self.direction != LEFT:
self.direction = RIGHT
def eat_food(self, food):
x1, y1, x2, y2 = self.canvas.coords(self.body[0])
if x1 == food.x and y1 == food.y:
self.length += 1
self.body.append(self.canvas.create_rectangle(*self.canvas.coords(self.body[-1]), fill=self.color))
food.new_food()
class Food:
def __init__(self, canvas, color):
self.canvas = canvas
self.color = color
self.x = random.randint(0, WIDTH/GRID_SIZE-1) * GRID_SIZE
self.y = random.randint(0, HEIGHT/GRID_SIZE-1) * GRID_SIZE
self.body = canvas.create_rectangle(self.x, self.y, self.x+GRID_SIZE, self.y+GRID_SIZE, fill=color)
def new_food(self):
self.x = random.randint(0, WIDTH/GRID_SIZE-1) * GRID_SIZE
self.y = random.randint(0, HEIGHT/GRID_SIZE-1) * GRID_SIZE
self.canvas.coords(self.body, self.x, self.y, self.x+GRID_SIZE, self.y+GRID_SIZE)
class Game:
def __init__(self, master):
self.master = master
self.canvas = tk.Canvas(master, width=WIDTH, height=HEIGHT, bg="white")
self.canvas.pack()
self.snake1 = Snake(self.canvas, WIDTH/2, HEIGHT/2, INIT_LENGTH, SNAKE_COLOR)
self.snake2 = Snake(self.canvas, WIDTH/2 - GRID_SIZE, HEIGHT/2, INIT_LENGTH, "blue")
self.food = Food(self.canvas, FOOD_COLOR)
self.canvas.bind_all("<Up>", lambda event: self.snake1.change_direction(UP))
self.canvas.bind_all("<Down>", lambda event: self.snake1.change_direction(DOWN))
self.canvas.bind_all("<Left>", lambda event: self.snake1.change_direction(LEFT))
self.canvas.bind_all("<Right>", lambda event: self.snake1.change_direction(RIGHT))
self.canvas.bind_all("w", lambda event: self.snake2.change_direction(UP))
self.canvas.bind_all("s", lambda event: self.snake2.change_direction(DOWN))
self.canvas.bind_all("a", lambda event: self.snake2.change_direction(LEFT))
self.canvas.bind_all("d", lambda event: self.snake2.change_direction(RIGHT))
self.timer = self.master.after(SPEED, self.game_loop)
def game_loop(self):
self.snake1.move()
self.snake2.move()
self.snake1.eat_food(self.food)
self.snake2.eat_food(self.food)
if self.check_collision():
self.master.after_cancel(self.timer)
self.canvas.create_text(WIDTH/2, HEIGHT/2, text="游戏结束!", fill="red", font=("Arial", 30))
return
self.timer = self.master.after(SPEED, self.game_loop)
def check_collision(self):
x1, y1, x2, y2 = self.canvas.coords(self.snake1.body[0])
if x1 < 0 or x2 > WIDTH or y1 < 0 or y2 > HEIGHT:
return True
for body in self.snake1.body[1:]:
if self.canvas.coords(body) == (x1, y1, x2, y2):
return True
for body in self.snake2.body:
if self.canvas.coords(body) == (x1, y1, x2, y2):
return True
x1, y1, x2, y2 = self.canvas.coords(self.snake2.body[0])
if x1 < 0 or x2 > WIDTH or y1 < 0 or y2 > HEIGHT:
return True
for body in self.snake2.body[1:]:
if self.canvas.coords(body) == (x1, y1, x2, y2):
return True
for body in self.snake1.body:
if self.canvas.coords(body) == (x1, y1, x2, y2):
return True
return False
root = tk.Tk()
game = Game(root)
root.mainloop()
```
该游戏使用tkinter库实现,运行后会在窗口中显示一个游戏区域,两条贪吃蛇和一个食物。玩家1使用键盘上的方向键控制自己的蛇,玩家2使用W、A、S、D键控制自己的蛇,当蛇吃到食物时,身体长度会增加。当任意一条蛇撞到边界或自己的身体时,游戏结束。
阅读全文