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)#宽度、长度、轮廓
#创建食物
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 do_up():
if head.direction != "down":
head.direction ="up"
def do_down():
if head.direction != "up":
head.direction ="down"
def do_left():
if head.direction != "right":
head.direction ="left"
def do_right():
if head.direction != "left":
head.direction ="right"
def move():
if head.direction=="up": #蛇头往上
y = head.ycor()#获取当前y轴的坐标
he服务器托管网ad.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.l服务器托管网isten()
window.onkeypress(do_up,"w")#绑定w键,触发do_up函数,蛇向上走
window.onkeypress(do_down,"s")#绑定w键,触发do_down函数
window.onkeypress(do_left,"a")#绑定w键,触发do_left函数
window.onkeypress(do_right,"d")#绑定w键,触发do_right函数
#设置蛇的身体
segments=[]
while True:
window.update() #更新窗口
if head.xcor()>290 or head.xcor()290 or head.ycor()heigh_score:
heigh_score=score
pen.clear()
pen.write("得分:", 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(0,y)
move()
#主循环事件
window.mainloop()
运行结果:
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: 【李老师云计算】HBase+Zookeeper部署及Maven访问(HBase集群实验)
索引 前言 1. Zookeeper 1.1 主机下载Zookeeper安装包 1.2 主机解压Zookeeper 1.3 ★解决解压后文件缺失 1.4 主机配置Zookeeper文件 1.4.1 配置zoo_sample.cfg文件 1.4.2 配置/dat…