tkinter贪吃虫程序编写

时间: 2024-04-23 07:18:46 浏览: 7
Tkinter是Python的一个标准GUI库,可以用于创建图形用户界面。贪吃虫程序是一个经典的小游戏,玩家控制一个虫子在屏幕上移动并吃掉食物,每吃到一个食物,虫子的身体会变长一节。下面是一个简单的Tkinter贪吃虫程序的编写示例: ```python import tkinter as tk import random class SnakeGame(tk.Canvas): def __init__(self, master): super().__init__(master, width=400, height=400, background="black") self.snake_body = [(100, 100), (90, 100), (80, 100)] self.food = self.create_food() self.direction = "Right" self.bind_all("<Key>", self.on_key_press) self.score = 0 self.speed = 150 self.game_loop() def create_food(self): while True: x = random.randint(1, 39) * 10 y = random.randint(1, 39) * 10 if (x, y) not in self.snake_body: return self.create_oval(x, y, x+10, y+10, fill="red") def move(self): head_x, head_y = self.snake_body[0] if self.direction == "Up": new_head = (head_x, head_y - 10) elif self.direction == "Down": new_head = (head_x, head_y + 10) elif self.direction == "Left": new_head = (head_x - 10, head_y) else: new_head = (head_x + 10, head_y) self.snake_body.insert(0, new_head) if self.food in self.snake_body: self.score += 1 self.delete(self.food) self.food = self.create_food() if self.score % 5 == 0: self.speed -= 10 else: self.delete(self.snake_body.pop()) if (new_head[0] < 0 or new_head[0] >= 400 or new_head[1] < 0 or new_head[1] >= 400 or new_head in self.snake_body[1:]): self.game_over() else: self.create_rectangle(new_head[0], new_head[1], new_head[0]+10, new_head[1]+10, fill="green") def game_loop(self): self.move() if not self.game_over_flag: self.after(self.speed, self.game_loop) def on_key_press(self, event): keysyms = event.keysym.lower() if keysyms in ["up", "down", "left", "right"]: opposite_directions = {"Up": "Down", "Down": "Up", "Left": "Right", "Right": "Left"} if keysyms != opposite_directions[self.direction]: self.direction = keysyms def game_over(self): self.delete(tk.ALL) self.create_text(self.winfo_width() / 2, self.winfo_height() / 2, text=f"Game Over! Score: {self.score}", fill="white", font=("Courier", 20), justify=tk.CENTER) self.game_over_flag = True root = tk.Tk() root.title("Snake Game") root.resizable(False, False) game = SnakeGame(root) game.pack() root.mainloop() ``` 这个示例代码使用了Tkinter的Canvas组件来创建游戏界面,通过继承Canvas类并重写相关方法来实现贪吃虫游戏的逻辑。玩家可以通过方向键控制虫子的移动方向,当虫子吃到食物时,得分增加并且速度加快。当虫子碰到边界或者自己的身体时,游戏结束。

相关推荐

最新推荐

recommend-type

基于python tkinter的点名小程序功能的实例代码

主要介绍了基于python tkinter的点名小程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

基于python的Tkinter编写登陆注册界面

主要为大家详细介绍了基于python的Tkinter编写登陆注册界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Python tkinter模版代码实例

主要介绍了Python tkinter模版代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

python Tkinter的图片刷新实例

今天小编就为大家分享一篇python Tkinter的图片刷新实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

python tkinter组件摆放方式详解

主要介绍了python tkinter组件摆放方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。