|

楼主 |
发表于 2025-2-3 15:05:40
|
显示全部楼层
deepseek编写的贪吃蛇程序,亲测可用 
------------------------
# 导入所需模块
import turtle # 图形绘制库
import time # 时间控制
import random # 随机数生成
# 初始化游戏窗口
window = turtle.Screen() # 创建屏幕对象
window.title("贪吃蛇 by Python技术") # 窗口标题
window.bgcolor("black") # 背景颜色
window.setup(width=600, height=600) # 窗口尺寸
window.tracer(0) # 关闭自动刷新,改为手动更新
# 创建蛇头对象
snake_head = turtle.Turtle() # 创建海龟对象
snake_head.speed(0) # 最快移动速度
snake_head.shape("square") # 方形形状
snake_head.color("green") # 绿色蛇头
snake_head.penup() # 抬起画笔,移动时不画线
snake_head.goto(0, 0) # 初始位置在原点
snake_head.direction = "stop" # 自定义属性,初始静止
# 创建食物对象
food = turtle.Turtle()
food.speed(0)
food.shape("circle") # 圆形食物
food.color("red")
food.penup()
food.goto(0, 100) # 初始食物位置
# 创建计分板
score_display = turtle.Turtle()
score_display.speed(0) # 静态文字无需动画
score_display.color("white")
score_display.penup() # 抬起画笔
score_display.hideturtle() # 隐藏海龟图标
score_display.goto(0, 260) # 定位到屏幕顶部
score = 0 # 当前得分
high_score = 0 # 历史最高分
# 显示初始分数
score_display.write("得分: 0 最高分: 0", align="center", font=("Arial", 24, "normal"))
# 初始化蛇身体列表(初始为空)
snake_segments = []
# 方向控制函数定义
def go_up():
"""禁止直接反向移动"""
if snake_head.direction != "down":
snake_head.direction = "up"
def go_down():
if snake_head.direction != "up":
snake_head.direction = "down"
def go_left():
if snake_head.direction != "right":
snake_head.direction = "left"
def go_right():
if snake_head.direction != "left":
snake_head.direction = "right"
# 绑定键盘事件
window.listen() # 开始监听键盘输入
window.onkey(go_up, "w") # W键上移
window.onkey(go_down, "s") # S键下移
window.onkey(go_left, "a") # A键左移
window.onkey(go_right, "d") # D键右移
def move():
"""根据当前方向移动蛇头"""
if snake_head.direction == "up":
y = snake_head.ycor() # 获取当前y坐标
snake_head.sety(y + 20) # 向上移动20像素
if snake_head.direction == "down":
y = snake_head.ycor()
snake_head.sety(y - 20)
if snake_head.direction == "left":
x = snake_head.xcor() # 获取当前x坐标
snake_head.setx(x - 20)
if snake_head.direction == "right":
x = snake_head.xcor()
snake_head.setx(x + 20)
def spawn_food():
"""在随机位置生成新食物"""
x = random.randint(-290, 290) # x轴边界留10像素余量
y = random.randint(-290, 270) # 避开顶部计分区域
food.goto(x, y)
# 游戏主循环
while True:
window.update() # 手动更新画面
# 边界碰撞检测
if (snake_head.xcor() > 290 or snake_head.xcor() < -290 or
snake_head.ycor() > 290 or snake_head.ycor() < -290):
time.sleep(1) # 暂停1秒
snake_head.goto(0, 0) # 重置蛇头位置
snake_head.direction = "stop" # 停止移动
# 隐藏所有身体段
for segment in snake_segments:
segment.goto(1000, 1000) # 移到屏幕外
snake_segments.clear() # 清空身体列表
# 重置得分显示
score = 0
score_display.clear()
score_display.write(f"得分: {score} 最高分: {high_score}",
align="center", font=("Arial", 24, "normal"))
# 检测是否吃到食物(两者距离小于20像素)
if snake_head.distance(food) < 20:
spawn_food() # 生成新食物
# 添加新的身体段
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey") # 灰色身体
new_segment.penup()
snake_segments.append(new_segment) # 加入身体列表
# 更新分数
score += 10
if score > high_score: # 更新最高分
high_score = score
score_display.clear()
score_display.write(f"得分: {score} 最高分: {high_score}",
align="center", font=("Arial", 24, "normal"))
# 移动身体段(从最后一段开始向前移动)
for index in range(len(snake_segments)-1, 0, -1):
# 前一段的位置赋值给当前段
x = snake_segments[index-1].xcor()
y = snake_segments[index-1].ycor()
snake_segments[index].goto(x, y)
# 移动第一个身体段到蛇头位置
if len(snake_segments) > 0:
x = snake_head.xcor()
y = snake_head.ycor()
snake_segments[0].goto(x, y)
move() # 调用移动函数
# 自身碰撞检测
for segment in snake_segments:
if segment.distance(snake_head) < 20: # 与任意身体段碰撞
time.sleep(1)
snake_head.goto(0, 0)
snake_head.direction = "stop"
# 清除身体段
for segment in snake_segments:
segment.goto(1000, 1000)
snake_segments.clear()
# 重置分数
score = 0
score_display.clear()
score_display.write(f"得分: {score} 最高分: {high_score}",
align="center", font=("Arial", 24, "normal"))
time.sleep(0.3) # 控制游戏速度
window.mainloop() # 保持窗口打开(实际在while True循环中不会执行到这里)
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|