Python turtle实现贪吃蛇游戏
作者:allway2 时间:2021-07-20 12:57:57
本文实例为大家分享了Python turtle实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
# Simple Snake Game in Python 3 for Beginners
import turtle
import time
import random
delay = 0.1
# Score
score = 0
high_score = 0
# Set up the screen
wn = turtle.Screen()
wn.title("Snake Game by @TokyoEdTech")
wn.bgcolor("green")
wn.setup(width=600, height=600)
wn.tracer(0) # Turns off the screen updates
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Snake food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)
segments = []
# Pen
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("Score: 0 High Score: 0", align="center",
font=("Courier", 24, "normal"))
# Functions
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":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
# Keyboard bindings
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")
# Main game loop
while True:
wn.update()
# Check for a collision with the border
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"
# Hide the segments
for segment in segments:
segment.goto(1000, 1000)
# Clear the segments list
segments.clear()
# Reset the score
score = 0
# Reset the delay
delay = 0.1
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score),
align="center", font=("Courier", 24, "normal"))
# Check for a collision with the food
if head.distance(food) < 20:
# Move the food to a random spot
x = random.randint(-290, 290)
y = random.randint(-290, 290)
food.goto(x, y)
# Add a segment
new_segment = turtle.Turtle()
new_segment.speed(0)
new_segment.shape("square")
new_segment.color("grey")
new_segment.penup()
segments.append(new_segment)
# Shorten the delay
delay -= 0.001
# Increase the score
score += 10
if score > high_score:
high_score = score
pen.clear()
pen.write("Score: {} High Score: {}".format(score, high_score),
align="center", font=("Courier", 24, "normal"))
# Move the end segments first in reverse order
for index in range(len(segments)-1, 0, -1):
x = segments[index-1].xcor()
y = segments[index-1].ycor()
segments[index].goto(x, y)
# Move segment 0 to where the head is
if len(segments) > 0:
x = head.xcor()
y = head.ycor()
segments[0].goto(x, y)
move()
# Check for head collision with the body segments
for segment in segments:
if segment.distance(head) < 20:
time.sleep(1)
head.goto(0, 0)
head.direction = "stop"
# Hide the segments
for segment in segments:
segment.goto(1000, 1000)
# Clear the segments list
segments.clear()
# Reset the score
score = 0
# Reset the delay
delay = 0.1
# Update the score display
pen.clear()
pen.write("Score: {} High Score: {}".format(
score, high_score), align="center", font=("Courier", 24, "normal"))
time.sleep(delay)
wn.mainloop()
来源:https://blog.csdn.net/allway2/article/details/117790619
标签:python,turtle,贪吃蛇
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2022-04-07 09:50:44
![](https://img.aspxhome.com/file/2023/1/101721_0s.gif)
django解决跨域请求的问题
2021-09-01 05:43:23
python读取文本中数据并转化为DataFrame的实例
2021-10-24 13:08:28
![](https://img.aspxhome.com/file/2023/5/77785_0s.jpg)
Python排序算法之插入排序及其优化方案详解
2021-04-03 05:39:31
![](https://img.aspxhome.com/file/2023/6/72086_0s.png)
windows下python 3.6.4安装配置图文教程
2023-08-10 02:57:37
![](https://img.aspxhome.com/file/2023/7/77827_0s.jpg)
Python中多线程的创建及基本调用方法
2021-12-14 00:26:26
使用Python处理json字符串中的非法双引号问题
2021-01-19 19:26:13
python数据解析BeautifulSoup爬取三国演义章节示例
2021-03-21 13:27:27
![](https://img.aspxhome.com/file/2023/2/104982_0s.jpg)
ASP 相关文章或者相关产品
2011-03-30 11:12:00
Python使用内置json模块解析json格式数据的方法
2023-07-30 14:10:45
![](https://img.aspxhome.com/file/2023/6/61226_0s.png)
Django 删除upload_to文件的步骤
2022-03-23 05:47:14
![](https://img.aspxhome.com/file/2023/7/107627_0s.jpg)
python3调用R的示例代码
2021-05-01 21:53:59
详解pandas获取Dataframe元素值的几种方法
2022-12-28 07:30:01
python中ImageTk.PhotoImage()不显示图片却不报错问题解决
2023-08-26 18:12:06
![](https://img.aspxhome.com/file/2023/9/69589_0s.png)
后工业时代的后规范思考
2009-06-03 20:30:00
960px下的黄金分割
2009-07-31 14:33:00
![](https://img.aspxhome.com/file/UploadPic/20097/31/498022_1248973274hhmt-19s.jpg)
python密码错误三次锁定(实例讲解)
2022-03-02 07:51:23
![](https://img.aspxhome.com/file/2023/2/101112_0s.png)
深入浅出SQL嵌套SELECT语句
2009-02-06 14:25:00
如何优雅地处理Django中的favicon.ico图标详解
2023-12-03 09:41:36
Python轻松管理与操作文件的技巧分享
2021-11-19 14:42:12