游戏规则
1.玩家控制蛇在屏幕上移动(上下左右方向键),目标是吃到随机出现的食物。
2.每次吃到食物后,蛇的长度会增加。
3.游戏难度逐渐增加,蛇的移动速度会随着长度的增加而加快。
4.如果蛇撞到自己或碰到墙壁,游戏结束。
代码
import tkinter as tk
import random
from tkinter import messagebox
class SnakeGame:
def __init__(self, root):
self.root = root
self.root.title("贪吃蛇游戏")
# 游戏参数
self.game_width = 400
self.game_height = 400
self.snake_size = 20
self.snake_speed = 200 # 初始速度(毫秒)
self.food_eaten = 0 # 吃到的食物数量
# 创建画布
self.canvas = tk.Canvas(root, width=self.game_width, height=self.game_height, bg="black")
self.canvas.pack()
# 初始化蛇和食物
self.snake = [(100, 100), (80, 100), (60, 100)] # 初始蛇的位置
self.direction = "Right" # 初始方向
self.food = self.create_food()
# 绑定按键
self.root.bind("<KeyPress>", self.change_direction)
# 开始游戏
self.running = True
self.update_game()
# 创建食物
def create_food(self):
while True:
x = random.randint(0, (self.game_width // self.snake_size) - 1) * self.snake_size
y = random.randint(0, (self.game_height // self.snake_size) - 1) * self.snake_size
if (x, y) not in self.snake:
return (x, y)
# 更新游戏状态
def update_game(self):
if self.running:
self.move_snake()
self.check_collisions()
self.draw_elements()
self.root.after(self.snake_speed, self.update_game)
# 改变方向
def change_direction(self, event):
if event.keysym == "Up" and self.direction != "Down":
self.direction = "Up"
elif event.keysym == "Down" and self.direction != "Up":
self.direction = "Down"
elif event.keysym == "Left" and self.direction != "Right":
self.direction = "Left"
elif event.keysym == "Right" and self.direction != "Left":
self.direction = "Right"
# 移动蛇
def move_snake(self):
head_x, head_y = self.snake[0]
if self.direction == "Up":
head_y -= self.snake_size
elif self.direction == "Down":
head_y += self.snake_size
elif self.direction == "Left":
head_x -= self.snake_size
elif self.direction == "Right":
head_x += self.snake_size
new_head = (head_x, head_y)
self.snake = [new_head] + self.snake[:-1]
# 吃到食物
if new_head == self.food:
self.snake.append(self.snake[-1]) # 增长蛇身
self.food = self.create_food()
self.food_eaten += 1 # 记录吃到的食物数量
if self.snake_speed > 50:
self.snake_speed -= 5 # 吃到食物后加快速度
# 检查碰撞
def check_collisions(self):
head_x, head_y = self.snake[0]
# 碰到墙壁
if head_x < 0 or head_x >= self.game_width or head_y < 0 or head_y >= self.game_height:
self.game_over()
# 撞到自己
if (head_x, head_y) in self.snake[1:]:
self.game_over()
# 绘制蛇和食物
def draw_elements(self):
self.canvas.delete("all")
for x, y in self.snake:
self.canvas.create_rectangle(x, y, x + self.snake_size, y + self.snake_size, fill="green")
food_x, food_y = self.food
self.canvas.create_oval(food_x, food_y, food_x + self.snake_size, food_y + self.snake_size, fill="red")
# 游戏结束
def game_over(self):
self.running = False
self.show_game_over_message()
# 显示游戏结束信息
def show_game_over_message(self):
messagebox.showinfo("游戏结束", f"你一共吃了 {self.food_eaten} 个食物!")
self.root.quit() # 关闭程序
# 创建窗口并启动游戏
root = tk.Tk()
game = SnakeGame(root)
root.mainloop()
标签:head,python,self,direction,food,game,小游戏,snake,贪吃蛇
From: https://blog.csdn.net/C7211BA/article/details/141944403