import random
import time
import turtle
#分数
score =0
#最高分
heigh_score =0
# 移动延迟
delay =0.2
#创建屏幕
window=turtle.Screen()
#设置标题
window.title("贪吃蛇")
#背景颜色
window.bgcolor("white")
#窗口大小
window.setup(width=600,height=600)
#创建蛇
head=turtle.Turtle()
head.speed(0)
head.color("grey")
head.shape("square")
head.penup()
head.goto(0,0)
head.direction="Stop"#设置头部为静止
head.shapesize(0.5,0.5,1)#宽度、长度、轮廓
#设置蛇的身体
segments=[]
#创建食物
food=turtle.Turtle()
food.speed(0)
food.color("blue")
food.shape("square")
food.penup()
food.goto(0,100)
food.shapesize(0.5,0.5,1)#宽度、长度、轮廓
#创建计数板
pen=turtle.Turtle()
pen.speed(0)
pen.color("red")
pen.penup()
pen.hideturtle()#隐藏海龟
pen.goto(0,260)
pen.write("得分:0 最高分:0",align="center",font=("Courier",27,"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": #蛇头往上
y = head.ycor()#获取当前y轴的坐标
head.sety(y+20) #向上移动一格
if head.direction=="down": #蛇头往下
y = head.ycor()#获取当前y轴的坐标
head.sety(y-20) #向下移动一格
if head.direction=="left": #蛇头往左
x = head.xcor()#获取当前x轴的坐标
head.setx(x-20) #向左移动一格
if head.direction=="right": #蛇头往右
x = head.xcor()#获取当前x轴的坐标
head.setx(x+20) #向右移动一格
#设置监听事件
window.listen()
window.onkeypress(go_up,"w")#绑定w键,触发do_up函数,蛇向上走
window.onkeypress(go_down,"s")#绑定w键,触发do_down函数
window.onkeypress(go_left,"a")#绑定w键,触发do_left函数
window.onkeypress(go_right,"d")#绑定w键,触发do_right函数
while True:
window.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"
for segment in segments:
segment.goto(1000,1000)
segments.clear()
score=0
delay=0.2
pen.clear()
pen.write("得分:{}最高分:{}".format(score, heigh_score), align="center", font=("Courier", 27, "normal"))
#判断是否吃到食物
if head.distance(food) < 20: #如果蛇头与食物的距离是否小于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("grey")
new_segment.penup()
segments.append(new_segment)
new_segment.shapesize(0.5,0.5,1)
delay -= 0.001
score +=10
if score>heigh_score:
heigh_score=score
pen.clear()
pen.write("得分:{}最高分:{}".format(score, heigh_score), align="center", font=("Courier", 27, "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"
for segment in segments:
segment.goto(1000, 1000)
segments.clear()
score = 0
delay = 0.2
pen.clear()
pen.write("得分:{}最高分:{}".format(score, heigh_score), align="center", font=("Courier", 27, "normal"))
time.sleep(delay)
#主循环事件
window.mainloop()
运行结果: