python设计gui贪吃蛇
时间: 2023-08-15 15:32:03 浏览: 168
好的,下面是一个简单的Python GUI贪吃蛇游戏的设计:
```python
import tkinter as tk
import random
# 定义常量
WIDTH = 500
HEIGHT = 500
SIZE = 25
DELAY = 100
# 定义贪吃蛇类
class Snake:
def __init__(self):
self.segments = [(3, 1), (2, 1), (1, 1)]
self.direction = "Right"
self.color = "green"
def move(self):
head = self.segments[0]
if self.direction == "Right":
new_head = (head[0]+1, head[1])
elif self.direction == "Left":
new_head = (head[0]-1, head[1])
elif self.direction == "Up":
new_head = (head[0], head[1]-1)
elif self.direction == "Down":
new_head = (head[0], head[1]+1)
self.segments.insert(0, new_head)
self.segments.pop()
def change_direction(self, direction):
if direction in ["Right", "Left", "Up", "Down"]:
if (direction == "Right" and self.direction != "Left" or
direction == "Left" and self.direction != "Right" or
direction == "Up" and self.direction != "Down" or
direction == "Down" and self.direction != "Up"):
self.direction = direction
def add_segment(self):
tail = self.segments[-1]
if self.direction == "Right":
new_segment = (tail[0]-1, tail[1])
elif self.direction == "Left":
new_segment = (tail[0]+1, tail[1])
elif self.direction == "Up":
new_segment = (tail[0], tail[1]+1)
elif self.direction == "Down":
new_segment = (tail[0], tail[1]-1)
self.segments.append(new_segment)
def draw(self, canvas):
for segment in self.segments:
x, y = segment[0]*SIZE, segment[1]*SIZE
canvas.create_rectangle(x, y, x+SIZE, y+SIZE, fill=self.color)
# 定义食物类
class Food:
def __init__(self):
self.position = (0, 0)
self.color = "red"
def generate(self, snake):
while True:
x = random.randint(0, WIDTH//SIZE-1)
y = random.randint(0, HEIGHT//SIZE-1)
if (x, y) not in snake.segments:
self.position = (x, y)
break
def draw(self, canvas):
x, y = self.position[0]*SIZE, self.position[1]*SIZE
canvas.create_oval(x, y, x+SIZE, y+SIZE, fill=self.color)
# 定义游戏类
class Game:
def __init__(self):
self.root = tk.Tk()
self.root.title("贪吃蛇")
self.canvas = tk.Canvas(self.root, width=WIDTH, height=HEIGHT)
self.canvas.pack()
self.snake = Snake()
self.food = Food()
self.running = False
def start(self):
self.running = True
self.snake.color = "green"
self.food.color = "red"
self.food.generate(self.snake)
self.draw()
self.root.bind("<KeyPress-Right>", self.right_key_pressed)
self.root.bind("<KeyPress-Left>", self.left_key_pressed)
self.root.bind("<KeyPress-Up>", self.up_key_pressed)
self.root.bind("<KeyPress-Down>", self.down_key_pressed)
self.root.after(DELAY, self.update)
def stop(self):
self.running = False
self.canvas.delete("all")
self.snake = Snake()
self.food = Food()
self.root.unbind("<KeyPress-Right>")
self.root.unbind("<KeyPress-Left>")
self.root.unbind("<KeyPress-Up>")
self.root.unbind("<KeyPress-Down>")
def draw(self):
self.canvas.delete("all")
self.snake.draw(self.canvas)
self.food.draw(self.canvas)
def update(self):
self.snake.move()
if self.snake.segments[0] == self.food.position:
self.snake.add_segment()
self.food.generate(self.snake)
if (self.snake.segments[0][0] < 0 or
self.snake.segments[0][0] >= WIDTH//SIZE or
self.snake.segments[0][1] < 0 or
self.snake.segments[0][1] >= HEIGHT//SIZE or
self.snake.segments[0] in self.snake.segments[1:]):
self.game_over()
return
self.draw()
if self.running:
self.root.after(DELAY, self.update)
def game_over(self):
self.snake.color = "gray"
self.food.color = "gray"
self.draw()
self.root.after(1000, self.stop)
def right_key_pressed(self, event):
self.snake.change_direction("Right")
def left_key_pressed(self, event):
self.snake.change_direction("Left")
def up_key_pressed(self, event):
self.snake.change_direction("Up")
def down_key_pressed(self, event):
self.snake.change_direction("Down")
# 启动游戏
game = Game()
game.start()
game.root.mainloop()
```
以上代码使用了Python的`tkinter`模块来创建GUI界面,并使用了`random`模块来随机生成食物的位置。您可以将代码复制到您的Python编辑器中,然后运行来体验贪吃蛇游戏。
阅读全文