Python turtle实现贪吃蛇游戏
作者:allway2 发布时间:2021-07-20 12:57:57
标签:python,turtle,贪吃蛇
本文实例为大家分享了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


猜你喜欢
- 1、简介这篇博客将会非常基础,如果有MySQL经验的可以跳过,写这篇博客的原因是给初学者看的。下面将会讲解如何使用select查看指定表的单
- mysql中replace函数替换字符串介绍下mysql的REPLACE()函数语法:REPLACE()函数的语法如下:REPLACE(st
- 说明视频剪辑时需要为视频添加字幕,添加字幕方法之一:根据字幕文本文件批量生成透明底只有字幕内容的图片文件,如下图,然后将这些图片文件添加到视
- 小编想把用python将列表[1,1,1,1,1,1,1,1,1,1] 和 列表 [2,2,2,2,2,2,2,2,2,2]对应相加成[3,
- 每每见到这三个函数,我都会很懵,一定要到网上搜搜;今天,恰巧又见到了它们,所以想必是时候为它们做个笔记啦1.slice(数组)用法:arra
- SQL Server 联机帮助给出了详细说明。 -->目录 -->SQL Server架构 --&
- 任何熟悉SQL和关系数据库的人都遇见过大量的连接类型。最简单的说,连接(join)会把两个表的内容组合到一个虚拟表或者recordset内。
- 1、enumerate返回针对序列类型的可迭代对象的枚举对象。2、eval取出字符串中的内容。将str中有效的表达式返回计算结果。3、exe
- 我就废话不多说,直接上代码吧!from PIL import ImageGrabimport timeimport scheduleimpo
- 微信小程序之自定义底部弹出框动画,供大家参考,具体内容如下最近做小程序时,会经常用到各种弹框。直接做显示和隐藏虽然也能达到效果,但是体验性太
- numpy.flip(m, axis=None)Reverse the order of elements in an array alon
- 前言在设计爬虫项目的时候,首先要在脑内明确人工浏览页面获得图片时的步骤一般地,我们去网上批量打开壁纸的时候一般操作如下:1、打开壁纸网页2、
- 我们经常在使用CLI工具的时候,都会有这样的参数输出:➜ ~ docker versionClient: Docker Engine - C
- Python引入了一个机制:引用计数。引用计数python内部使用引用计数,来保持追踪内存中的对象,Python内部记录了对象有多少个引用,
- 一、Mysql分区类型1、RANGE 分区:基于属于一个给定连续区间的列值,把多行分配给分区。2、HASH分区:基于用户定义的表达式的返回值
- python来写一个试试吧,这里使用了cPAMIE模块,代码如下:代码from cPAMIE import PAMIEie=PAMIE(&q
- php输出全部gb2312编码内的汉字,$area表示分区,$pos表示分区内所在位置。<?php$fp = fopen('t
- 教大家做一个简易的猜数字的游戏,但确实蛮简单的,我这个四年级的小盆友也才学了一天,所以给那些不会的教一下。我们先来看导入模块:"&
- Microsoft SQL server2000由一系列相互协作的组件构成。能满足最大的WEB站点和企业数据处理系统存储
- 提要:作为普通的Python开发者来讲,深入理解object、type不是必要的,但了解他们确实元编程这个词很多朋友都听过,可能用的却不多。