用 Python 做个贪吃蛇游戏

你有没有小时候玩贪吃蛇的美好回忆?现在,我们不光要玩,还要自己用 Python 亲手做一个贪吃蛇游戏,炫酷不?你有没有小时候玩贪吃蛇的美好回忆?现在,我们不光要玩,还要自己用 Python 亲手做一个贪吃蛇游戏,炫酷不?

第一步:准备工作 — 下载 VS Code 和 Python

先说正事儿,没有好工具怎么行?所以我们先把两个神器安利给你:

1. 安装 VS Code

VS Code(Visual Studio Code)是微软家的免费代码编辑器,功能强大,插件多,界面还很友好。放心,绝对比你想象的还好用。

打开官网:https://code.visualstudio.com/

下载适合你系统的版本(Windows、macOS、Linux都有)

2. 安装 Python

Python 是咱们写游戏的“超级工具箱”,版本推荐用最新的 3.11 或以上。

打开官网:https://python.org/downloads/

下载安装包,安装的时候记得勾选“Add Python to PATH”(这个超重要!)

安装完成后,在终端(Windows 上叫 PowerShell,macOS/Linux 叫 Terminal)输入:

 python --version

如果显示版本号,说明你成功了!

第二步:创建贪吃蛇游戏

现在,重头戏来了!咱们用 Python 的 turtle 模块(就是那个小乌龟画画的库)来做游戏。它简单又好玩,不用担心复杂图形学!

1. 新建项目文件

打开 VS Code,新建一个文件夹,随便起名,比如 snake_game。在里面新建一个 snake.py 文件。

2. 写代码,画出我们的贪吃蛇

复制下面代码到 snake.py,别被吓到,我会慢慢说:

 import turtle,time,random;delay=0.1;score=0;high_score=0;wn=turtle.Screen();wn.title("贪吃蛇 by 你的名字");wn.bgcolor("black");wn.setup(width=600,height=600);wn.tracer(0);head=turtle.Turtle();head.speed(0);head.shape("square");head.color("green");head.penup();head.goto(0,0);head.direction="stop";food=turtle.Turtle();food.speed(0);food.shape("circle");food.color("red");food.penup();food.goto(0,100);segments=[];pen=turtle.Turtle();pen.speed(0);pen.shape("square");pen.color("white");pen.penup();pen.hideturtle();pen.goto(0,260);pen.write("分数: 0 最高分: 0",align="center",font=("Courier",24,"normal"));def move():\n if head.direction=="up":y=head.ycor();head.sety(y+20);\n if head.direction=="down":y=head.ycor();head.sety(y-20);\n if head.direction=="left":x=head.xcor();head.setx(x-20);\n if head.direction=="right":x=head.xcor();head.setx(x+20);def go_up():\n if head.direction!="down":head.direction="up";def go_down():\n if head.direction!="up":head.direction="down";def go_left():\n if head.direction!="right":head.direction="left";def go_right():\n if head.direction!="left":head.direction="right";wn.listen();wn.onkeypress(go_up,"w");wn.onkeypress(go_down,"s");wn.onkeypress(go_left,"a");wn.onkeypress(go_right,"d");while True:wn.update();if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:time.sleep(1);head.goto(0,0);head.direction="stop";[segment.goto(1000,1000) for segment in segments];segments.clear();score=0;pen.clear();pen.write(f"分数: {score} 最高分: {high_score}",align="center",font=("Courier",24,"normal"));if head.distance(food)<20:x=random.randint(-290,290);y=random.randint(-290,290);food.goto(x,y);new_segment=turtle.Turtle();new_segment.speed(0);new_segment.shape("square");new_segment.color("green");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=("Courier",24,"normal"));for index in range(len(segments)-1,0,-1):x=segments[index-1].xcor();y=segments[index-1].ycor();segments[index].goto(x,y);if len(segments)>0:x=head.xcor();y=head.ycor();segments[0].goto(x,y);move();for segment in segments:if segment.distance(head)<20:time.sleep(1);head.goto(0,0);head.direction="stop";[segment.goto(1000,1000) for segment in segments];segments.clear();score=0;pen.clear();pen.write(f"分数: {score} 最高分: {high_score}",align="center",font=("Courier",24,"normal"));time.sleep(delay);wn.mainloop()

3. 运行试试看!

打开 VS Code 终端,运行:

python snake.py

立马弹出一个黑色窗口,绿色方块就是你的蛇,红色圆点是食物。

按 W、A、S、D 控制方向,试试吃到红点变长,撞墙或撞自己就要重新开始。

 

文末附加内容
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇