python 代码设计贪吃蛇

代码:
# -*- codeing = utf-8 -*-
import tkinter as tk
import random
from tkinter import messagebox

class Snake:
    def __init__(self, master):
        self.master = master
        self.master.title("Snake")

        # 创建画布
        self.canvas = tk.Canvas(self.master, width=400, height=400, bg="white")
        self.canvas.pack()

        # 初始化游戏数据
        self.snake = [(0, 0), (0, 1), (0, 2)]
        self.food = (5, 5)
        self.direction = "Right"
        self.score = 0

        # 绑定键盘事件
        self.master.bind("<Key>", self.on_key_press)

        # 开始游戏
        self.start_game()

    def start_game(self):
        # 绘制贪吃蛇和食物
        self.draw_snake()
        self.draw_food()

        # 更新游戏状态
        self.update_game()

    def draw_snake(self):
        # 清空画布
        self.canvas.delete("all")

        # 绘制贪吃蛇
        for x, y in self.snake:
            x1 = x * 20
            y1 = y * 20
            x2 = x1 + 20
            y2 = y1 + 20
            self.canvas.create_rectangle(x1, y1, x2, y2, fill="green")

    def draw_food(self):
        # 绘制食物
        x1 = self.food[0] * 20
        y1 = self.food[1] * 20
        x2 = x1 + 20
        y2 = y1 + 20
        self.canvas.create_oval(x1, y1, x2, y2, fill="red")

    def update_game(self):
        # 更新贪吃蛇位置
        head_x, head_y = self.snake[-1]
        if self.direction == "Left":
            new_head = (head_x - 1, head_y)
        elif self.direction == "Right":
            new_head = (head_x + 1, head_y)
        elif self.direction == "Up":
            new_head = (head_x, head_y - 1)
        else:
            new_head = (head_x, head_y + 1)
        self.snake.append(new_head)
        del self.snake[0]

        # 检查游戏是否结束
        if new_head[0] < 0 or new_head[0] >= 20 or new_head[1] < 0 or new_head[1] >= 20 or new_head in self.snake[:-1]:
            tk.messagebox.showinfo("Game Over", f"Score: {self.score}")
            return

        # 检查贪吃蛇是否吃掉食物
        if new_head == self.food:
            while True:
                food_x = random.randint(0, 19)
                food_y = random.randint(0, 19)
                if (food_x, food_y) not in self.snake:
                    break
            self.food = (food_x, food_y)
            tail_x, tail_y = self.snake[0]
            if self.direction == "Left":
                new_tail = (tail_x + 1, tail_y)
            elif self.direction == "Right":
                new_tail = (tail_x - 1, tail_y)
            elif self.direction == "Up":
                new_tail = (tail_x, tail_y + 1)
            else:
                new_tail = (tail_x, tail_y - 1)
            self.snake.insert(0, new_tail)
            self.score += 1

        # 绘制贪吃蛇和食物
        self.draw_snake()
        self.draw_food()

        # 定时更新游戏状态
        self.master.after(200, self.update_game)

    def on_key_press(self, event):
        if event.keysym in ["Left", "Right", "Up", "Down"]:
            self.direction = event.keysym

if __name__ == "__main__":
    root = tk.Tk()
    snake = Snake(root)
    root.mainloop()

结果图:

相关推荐

  1. 贪吃代码python实现

    2024-07-12 20:28:01       21 阅读
  2. python贪吃

    2024-07-12 20:28:01       52 阅读
  3. python 贪吃

    2024-07-12 20:28:01       24 阅读
  4. python制作贪吃游戏

    2024-07-12 20:28:01       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 20:28:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 20:28:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 20:28:01       45 阅读
  4. Python语言-面向对象

    2024-07-12 20:28:01       55 阅读

热门阅读

  1. Python通过继承实现多线程

    2024-07-12 20:28:01       17 阅读
  2. 2024年C#优秀实用的类库推荐

    2024-07-12 20:28:01       21 阅读
  3. L1 Simple_ReAct_Agent

    2024-07-12 20:28:01       18 阅读
  4. rust way step 7

    2024-07-12 20:28:01       18 阅读
  5. sqlalchemy通过查询参数生成query

    2024-07-12 20:28:01       15 阅读
  6. git reset hard和soft的使用和区别

    2024-07-12 20:28:01       18 阅读
  7. 目前分布式光纤测温系统的主流架构有哪些?

    2024-07-12 20:28:01       16 阅读