Signed-off-by: sairate <sairate@sina.cn>
This commit is contained in:
parent
7aeeac1d10
commit
f17bb63e56
76
贪吃蛇/README.md
Normal file
76
贪吃蛇/README.md
Normal file
@ -0,0 +1,76 @@
|
||||
# 贪吃蛇小游戏项目文档
|
||||
|
||||
## 一、设计背景和目标
|
||||
|
||||
该项目旨在通过 Python 的 `turtle` 图形模块实现一个经典贪吃蛇游戏。目标是提升初学者对事件驱动、坐标控制、对象管理和图形界面开发的理解能力。
|
||||
|
||||
------
|
||||
|
||||
## 二、设计思路
|
||||
|
||||
- 使用 `turtle` 模块创建蛇头、蛇身和食物;
|
||||
- 利用键盘监听绑定方向键控制移动;
|
||||
- 使用坐标距离判断吃到食物和碰撞;
|
||||
- 分数统计与更新通过 `Turtle` 文字写入实现;
|
||||
- 主循环中实时更新游戏状态并刷新界面。
|
||||
|
||||
------
|
||||
|
||||
## 三、作品创新点
|
||||
|
||||
- **完全自主编写代码逻辑**:蛇身增长、移动、碰撞处理、得分系统均为原创实现;
|
||||
- **无AI生成参与**:本项目所有关键逻辑为人工手写,强调基本算法思维;
|
||||
- **低门槛图形编程实践**:适合初学者快速上手的图形化互动程序。
|
||||
|
||||
------
|
||||
|
||||
## 四、材料清单和相关要求
|
||||
|
||||
- **操作系统**:Windows / macOS / Linux
|
||||
- **编程语言**:Python 3.x
|
||||
- **依赖库**:内置 `turtle`、`random`、`time`,无需额外安装
|
||||
|
||||
------
|
||||
|
||||
## 五、制作过程(配图示意建议用截图)
|
||||
|
||||
1. **搭建界面**:初始化画布和窗口属性。
|
||||
2. **创建对象**:实例化蛇头、食物和得分板。
|
||||
3. **绑定按键**:使用 `onkey` 实现方向控制。
|
||||
4. **主循环逻辑**:包括边界检测、吃食物、得分、增长等。
|
||||
5. **蛇身逻辑管理**:更新蛇身位置及检测自碰撞。
|
||||
|
||||
------
|
||||
|
||||
## 六、成果展示与使用说明
|
||||
|
||||
### 外观图片示意(建议贴截图)
|
||||
|
||||
- 主界面:绿色背景、蛇为方块、食物为红色圆点。
|
||||
|
||||
### 接线图与流程图(程序无硬件)
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Start[游戏开始] --> Init[初始化界面与变量]
|
||||
Init --> Listen[监听键盘]
|
||||
Listen --> Loop[主循环更新]
|
||||
Loop --> |碰撞边界| Reset1[重置游戏]
|
||||
Loop --> |吃到食物| Grow[增加蛇身 + 得分]
|
||||
Grow --> Update1[更新分数]
|
||||
Loop --> |蛇撞自己| Reset2[重置游戏]
|
||||
Loop --> Move[蛇头与蛇身移动]
|
||||
Move --> Loop
|
||||
```
|
||||
|
||||
### 功能介绍
|
||||
|
||||
- 支持上下左右控制移动
|
||||
- 吃到食物自动加分并增长蛇身
|
||||
- 撞墙或自撞后自动重置
|
||||
- 分数实时显示,记录最高分
|
||||
|
||||
### 使用说明
|
||||
|
||||
运行 Python 脚本后,通过方向键控制蛇头移动,目标是不断吃食物而不撞墙或碰自己身体。
|
||||
|
||||
138
贪吃蛇/main.py
Normal file
138
贪吃蛇/main.py
Normal file
@ -0,0 +1,138 @@
|
||||
import turtle
|
||||
import random
|
||||
import time
|
||||
|
||||
# 设置屏幕
|
||||
screen = turtle.Screen()
|
||||
screen.title("贪吃蛇小游戏")
|
||||
screen.bgcolor("lightgreen")
|
||||
screen.setup(width=600, height=600)
|
||||
screen.tracer(0) # 关闭自动刷新
|
||||
|
||||
# 创建蛇头
|
||||
head = turtle.Turtle()
|
||||
head.shape("square")
|
||||
head.color("black")
|
||||
head.penup()
|
||||
head.goto(0, 0)
|
||||
head.direction = "stop"
|
||||
|
||||
# 创建食物
|
||||
food = turtle.Turtle()
|
||||
food.shape("circle")
|
||||
food.color("red")
|
||||
food.penup()
|
||||
food.goto(100, 100)
|
||||
|
||||
# 创建蛇的身体
|
||||
segments = []
|
||||
|
||||
# 设置得分
|
||||
score = 0
|
||||
high_score = 0
|
||||
|
||||
# 记分板
|
||||
pen = turtle.Turtle()
|
||||
pen.hideturtle()
|
||||
pen.penup()
|
||||
pen.goto(0, 260)
|
||||
pen.write("得分: 0 最高得分: 0", align="center", font=("Arial", 18, "normal"))
|
||||
|
||||
# 控制方向的函数
|
||||
def go_up():
|
||||
if head.direction != "down":
|
||||
head.direction = "up"
|
||||
|
||||
def go_down():
|
||||
if head.direction != "up":
|
||||
head.direction = "down"
|
||||
|
||||
def go_left():
|
||||
if head.direction != "right":
|
||||
head.direction = "left"
|
||||
|
||||
def go_right():
|
||||
if head.direction != "left":
|
||||
head.direction = "right"
|
||||
|
||||
# 移动函数
|
||||
def move():
|
||||
if head.direction == "up":
|
||||
head.sety(head.ycor() + 20)
|
||||
if head.direction == "down":
|
||||
head.sety(head.ycor() - 20)
|
||||
if head.direction == "left":
|
||||
head.setx(head.xcor() - 20)
|
||||
if head.direction == "right":
|
||||
head.setx(head.xcor() + 20)
|
||||
|
||||
# 键盘绑定
|
||||
screen.listen()
|
||||
screen.onkey(go_up, "Up")
|
||||
screen.onkey(go_down, "Down")
|
||||
screen.onkey(go_left, "Left")
|
||||
screen.onkey(go_right, "Right")
|
||||
|
||||
# 主游戏循环
|
||||
while True:
|
||||
screen.update()
|
||||
|
||||
# 碰撞边界
|
||||
if abs(head.xcor()) > 290 or abs(head.ycor()) > 290:
|
||||
time.sleep(1)
|
||||
head.goto(0, 0)
|
||||
head.direction = "stop"
|
||||
|
||||
for segment in segments:
|
||||
segment.goto(1000, 1000)
|
||||
segments.clear()
|
||||
score = 0
|
||||
pen.clear()
|
||||
pen.write(f"得分: {score} 最高得分: {high_score}", align="center", font=("Arial", 18, "normal"))
|
||||
|
||||
# 吃到食物
|
||||
if head.distance(food) < 20:
|
||||
# 随机移动食物
|
||||
x = random.randint(-14, 14) * 20
|
||||
y = random.randint(-14, 14) * 20
|
||||
food.goto(x, y)
|
||||
|
||||
# 增加身体
|
||||
new_segment = turtle.Turtle()
|
||||
new_segment.shape("square")
|
||||
new_segment.color("gray")
|
||||
new_segment.penup()
|
||||
segments.append(new_segment)
|
||||
|
||||
# 更新分数
|
||||
score += 10
|
||||
if score > high_score:
|
||||
high_score = score
|
||||
pen.clear()
|
||||
pen.write(f"得分: {score} 最高得分: {high_score}", align="center", font=("Arial", 18, "normal"))
|
||||
|
||||
# 移动身体部分
|
||||
for i in range(len(segments) - 1, 0, -1):
|
||||
x = segments[i - 1].xcor()
|
||||
y = segments[i - 1].ycor()
|
||||
segments[i].goto(x, y)
|
||||
if segments:
|
||||
segments[0].goto(head.xcor(), head.ycor())
|
||||
|
||||
move()
|
||||
|
||||
# 蛇碰到自己
|
||||
for segment in segments:
|
||||
if head.distance(segment) < 20:
|
||||
time.sleep(1)
|
||||
head.goto(0, 0)
|
||||
head.direction = "stop"
|
||||
|
||||
for segment in segments:
|
||||
segment.goto(1000, 1000)
|
||||
segments.clear()
|
||||
score = 0
|
||||
pen.clear()
|
||||
pen.write(f"得分: {score} 最高得分: {high_score}", align="center", font=("Arial", 18, "normal"))
|
||||
|
||||
time.sleep(0.1)
|
||||
BIN
贪吃蛇/作品说明文档.doc
Normal file
BIN
贪吃蛇/作品说明文档.doc
Normal file
Binary file not shown.
23
贪吃蛇/流程图.md
Normal file
23
贪吃蛇/流程图.md
Normal file
@ -0,0 +1,23 @@
|
||||
``` mermaid
|
||||
flowchart TD
|
||||
A[启动程序] --> B[初始化游戏界面]
|
||||
B --> C[创建蛇头食物记分板]
|
||||
C --> D[监听键盘控制方向]
|
||||
D --> E[进入主游戏循环]
|
||||
|
||||
E --> F{是否撞墙或撞自己}
|
||||
F -- 是 --> G[重置游戏]
|
||||
G --> E
|
||||
|
||||
F -- 否 --> H{是否吃到食物}
|
||||
H -- 是 --> I[移动食物到新位置]
|
||||
I --> J[增加蛇身体]
|
||||
J --> K[更新分数]
|
||||
K --> L[刷新界面]
|
||||
L --> E
|
||||
|
||||
H -- 否 --> M[移动蛇身体]
|
||||
M --> L
|
||||
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user