游戏名:《贪吃蛇》

import tkinter as tk
from random import randint
from collections import deque

class SnakeGame:
    def __init__(self, master):
        self.master = master
        self.master.title("PySnake Pro")
        self.master.resizable(False, False)
        
        # 游戏参数
        self.cell_size = 20
        self.width = 30
        self.height = 30
        self.speed = 150
        self.score = 0
        self.direction = "Right"
        self.snake = deque([(5, 5), (4, 5), (3, 5)])
        self.food = None
        self.special_food = None
        self.speed_boost = 1.0
        self.growth_counter = 0
        
        # 界面布局
        self.canvas = tk.Canvas(master, 
                              width=self.width*self.cell_size,
                              height=self.height*self.cell_size,
                              bg="black")
        self.canvas.pack()
        self.score_label = tk.Label(master, text=f"Score: {self.score}", font=('Arial', 14))
        self.score_label.pack()
        
        # 初始化游戏
        self.create_food()
        self.create_special_food()
        self.bind_keys()
        self.update()
    
    def bind_keys(self):
        self.master.bind("<Left>", lambda e: self.change_direction("Left"))
        self.master.bind("<Right>", lambda e: self.change_direction("Right"))
        self.master.bind("<Up>", lambda e: self.change_direction("Up"))
        self.master.bind("<Down>", lambda e: self.change_direction("Down"))
    
    def change_direction(self, new_dir):
        opposites = [("Left", "Right"), ("Right", "Left"), 
                    ("Up", "Down"), ("Down", "Up")]
        if (self.direction, new_dir) not in opposites:
            self.direction = new_dir
    
    def create_food(self):
        while True:
            x = randint(0, self.width-1)
            y = randint(0, self.height-1)
            if (x, y) not in self.snake:
                self.food = (x, y)
                break
    
    def create_special_food(self):
        while True:
            x = randint(0, self.width-1)
            y = randint(0, self.height-1)
            if (x, y) not in self.snake and (x, y) != self.food:
                self.special_food = {
                    "pos": (x, y),
                    "type": randint(1, 3),  # 1:加速 2:减速 3:成长
                    "timer": 50
                }
                break
    
    def move_snake(self):
        head = self.snake[0]
        x, y = head
        
        if self.direction == "Left": x -= 1
        elif self.direction == "Right": x += 1
        elif self.direction == "Up": y -= 1
        elif self.direction == "Down": y += 1
        
        # 边界检测
        if x < 0 or x >= self.width or y < 0 or y >= self.height:
            self.game_over()
            return
        
        # 自我碰撞检测
        if (x, y) in self.snake:
            self.game_over()
            return
        
        self.snake.appendleft((x, y))
        
        # 普通食物检测
        if (x, y) == self.food:
            self.score += 10
            self.create_food()
            if randint(1, 5) == 1:  # 20%概率生成特殊食物
                self.create_special_food()
        else:
            self.snake.pop()
        
        # 特殊食物检测
        if self.special_food and (x, y) == self.special_food["pos"]:
            effect = self.special_food["type"]
            if effect == 1:
                self.speed_boost = 0.7
            elif effect == 2:
                self.speed_boost = 1.5
            elif effect == 3:
                self.growth_counter += 3
            self.score += 50
            self.special_food = None
        
        # 处理成长效果
        if self.growth_counter > 0:
            self.snake.append(self.snake[-1])
            self.growth_counter -= 1
    
    def update(self):
        self.move_snake()
        self.draw()
        if self.special_food:
            self.special_food["timer"] -= 1
            if self.special_food["timer"] <= 0:
                self.special_food = None
        self.master.after(int(self.speed * self.speed_boost), self.update)
    
    def draw(self):
        self.canvas.delete("all")
        
        # 绘制蛇
        for i, (x, y) in enumerate(self.snake):
            color = "#00FF00" if i ==0 else "#009900"
            self.canvas.create_rectangle(
                x*self.cell_size, y*self.cell_size,
                (x+1)*self.cell_size, (y+1)*self.cell_size,
                fill=color, outline="")
        
        # 绘制普通食物
        fx, fy = self.food
        self.canvas.create_oval(
            fx*self.cell_size, fy*self.cell_size,
            (fx+1)*self.cell_size, (fy+1)*self.cell_size,
            fill="red")
        
        # 绘制特殊食物
        if self.special_food:
            sx, sy = self.special_food["pos"]
            colors = ["#FFA500", "#FF69B4", "#00BFFF"]
            self.canvas.create_rectangle(
                sx*self.cell_size, sy*self.cell_size,
                (sx+1)*self.cell_size, (sy+1)*self.cell_size,
                fill=colors[self.special_food["type"]-1])
        
        # 更新分数
        self.score_label.config(text=f"Score: {self.score}")
    
    def game_over(self):
        self.canvas.create_text(
            self.width*self.cell_size/2,
            self.height*self.cell_size/2,
            text=f"Game Over! Score: {self.score}",
            fill="white",
            font=('Arial', 24))
        self.master.unbind("<Left>")
        self.master.unbind("<Right>")
        self.master.unbind("<Up>")
        self.master.unbind("<Down>")
        # 添加重新开始按钮
        tk.Button(self.master, text="Play Again", command=self.restart).pack()
    
    def restart(self):
        self.master.destroy()
        root = tk.Tk()
        SnakeGame(root)
        root.mainloop()

if __name__ == "__main__":
    root = tk.Tk()
    game = SnakeGame(root)
    root.mainloop()

特色:

  1. 三种特殊食物机制

    • 橙色加速食物:暂时提升移动速度
    • 粉色减速食物:暂时降低移动速度
    • 蓝色成长食物:使蛇身额外增长3节
  2. 动态难度系统

    • 20%概率生成特殊食物
    • 特殊食物有时间限制
    • 速度增益/减益效果会随时间自动恢复
  3. 改进的视觉效果

    • 蛇头与身体颜色区分
    • 不同颜色代表不同效果的食物
    • 流畅的动画效果
  4. 增强的游戏性

    • 分数系统(普通食物10分,特殊食物50分)
    • 自动重新开始功能
    • 碰撞检测优化
  5. 代码结构特点

    • 完全使用Python标准库(仅需tkinter)
    • 面向对象设计
    • 清晰的游戏状态管理

游戏玩法:

  • 使用方向键控制蛇的移动
  • 吃红色普通食物增长1节
  • 收集特殊食物获得不同效果
  • 避免撞墙和撞到自己身体
  • 争取获得最高分数