Python海龟图形游戏
我的Python游戏落后了很多。我开始制作一个新的Python游戏,每次测试它时,它总是会滞后,尽管我没有使用任何图像Python海龟图形游戏,python,turtle-graphics,Python,Turtle Graphics,我的Python游戏落后了很多。我开始制作一个新的Python游戏,每次测试它时,它总是会滞后,尽管我没有使用任何图像 import turtle import random #head orientation h = [0] #score a = [0] b = [0] #food coord fcoord = [0,0,0] #position pos = [] def home(x,y): x = 0 y = 0 a[0] = 0 b[0] =
import turtle
import random
#head orientation
h = [0]
#score
a = [0]
b = [0]
#food coord
fcoord = [0,0,0]
#position
pos = []
def home(x,y):
x = 0
y = 0
a[0] = 0
b[0] = 0
h[0] = 0
fcoord[2] = 0
pos[:] = []
turtle.hideturtle()
turtle.clear()
turtle.pu()
turtle.color("lime")
turtle.goto(0,0)
turtle.write("PLAY", align="center",font="Calibri")
turtle.title("Snake Game")
turtle.onscreenclick(start)
turtle.mainloop()
def level_1():
turtle.clear()
turtle.pu()
turtle.speed(0)
turtle.pensize(20)
turtle.color("grey")
turtle.goto(-220,220)
turtle.pd()
turtle.goto(220,220)
turtle.goto(220,-220)
turtle.goto(-220,-220)
turtle.goto(-220,220)
turtle.pu()
turtle.goto(0,0)
def start(x,y):
turtle.onscreenclick(None)
level_1()
tfood = turtle.Turtle()
tfood.hideturtle()
tfood.pu()
tfood.speed(0)
tfood.shape("square")
tfood.color("red")
tscore = turtle.Turtle()
tscore.hideturtle()
tscore.pu()
tscore.speed(0)
tscore.goto(100,-250)
tscore.write("Score:" + str(a[0]), align="center",font=(10))
while x > -210 and x < 210 and y > -210 and y <210:
if fcoord[2] == 0:
food(tfood)
fcoord[2] = 1
turtle.onkey(u,"Up")
turtle.onkey(l,"Left")
turtle.onkey(r,"Right")
turtle.onkey(d,"Down")
turtle.listen()
move()
x = turtle.xcor()
y = turtle.ycor()
if x > fcoord[0]*20-5 and x < fcoord[0]*20+5 and y > fcoord[1]*20-5 and y < fcoord[1]*20+5:
fcoord[2] = 0
tfood.clear()
a[0] += 1
tscore.clear()
tscore.write("Score:" + str(a[0]), align="center",font=(10))
if len(pos) > 1:
for i in range(1,len(pos)):
if x < pos[i][0]+5 and x > pos[i][0]-5 and y < pos[i][1]+5 and y > pos[i][1]-5:
tscore.clear()
tfood.clear()
gameover()
tscore.clear()
tfood.clear()
gameover()
#Food
def food(tfood):
x = random.randrange(-8,8,1)
y = random.randrange(-8,8,1)
fcoord[0] = x
fcoord[1] = y
tfood.hideturtle()
tfood.pu()
tfood.shape("circle")
tfood.color("red")
tfood.goto(x*20,y*20)
tfood.stamp()
#Up
def u():
if h[0] == 270:
pass
else:
h[0] = 90
#Down
def d():
if h[0] == 90:
pass
else:
h[0] = 270
#Left
def l():
if h[0] == 0:
pass
else:
h[0] = 180
#Right
def r():
if h[0] == 180:
pass
else:
h[0] = 0
def move():
turtle.pensize(1)
turtle.color("green")
turtle.pu()
turtle.speed(3)
turtle.setheading(h[0])
turtle.shape("square")
turtle.stamp()
turtle.fd(20)
x = turtle.xcor()
y = turtle.ycor()
if b[0] > a[0]:
turtle.clearstamps(1)
pos.insert(0,[round(x),round(y)])
pos.pop(-1)
else:
pos.insert(0,[round(x),round(y)])
b[0] += 1
def gameover():
turtle.onscreenclick(None)
turtle.speed(0)
turtle.pu()
turtle.goto(0,150)
turtle.color("red")
turtle.write("Game Over",align="center", font=(10))
turtle.goto(0,50)
turtle.write("Score:" + str(a[0]),align="center",font=(10))
turtle.goto(200,-200)
turtle.write("(Click anywhere to return to the main menu)",align="right",font=(0.0000001))
turtle.onscreenclick(home)
turtle.mainloop()
# # # # # # # # # # # # # # # # # # # # # #
# Main #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
home(0,0)
导入海龟
随机输入
#头部定位
h=[0]
#得分
a=[0]
b=[0]
#食品合作社
fcoord=[0,0,0]
#位置
pos=[]
def home(x,y):
x=0
y=0
a[0]=0
b[0]=0
h[0]=0
fcoord[2]=0
位置[:]=[]
乌龟
乌龟
乌龟
乌龟颜色(“青柠”)
乌龟。后藤(0,0)
乌龟。写(“PLAY”,align=“center”,font=“Calibri”)
乌龟。标题(“蛇游戏”)
海龟。屏幕上单击(开始)
tutle.mainloop()
def液位_1():
乌龟
乌龟
乌龟。速度(0)
海龟。养老金(20)
乌龟。颜色(“灰色”)
乌龟。转到(-220220)
(d)
乌龟。后藤(220220)
乌龟。后藤(220,-220)
乌龟。转到(-220,-220)
乌龟。转到(-220220)
乌龟
乌龟。后藤(0,0)
def启动(x,y):
海龟。屏幕单击(无)
第1级()
tfood=海龟。海龟()
tfood.hideturtle()
t food.pu()
t良好的速度(0)
t良好的形状(“方形”)
t食品颜色(“红色”)
tscore=海龟。海龟()
tscore.hideturtle()
tscore.pu()
t核心速度(0)
tscore.goto(100,-250)
tscore.write(“分数:+str(a[0]),align=“center”,font=(10))
而x>-210和x<210和y>-210和y-fcoord[0]*20-5和xfcoord[1]*20-5和y1:
对于范围(1,len(pos))中的i:
如果xpos[i][0]-5和ypos[i][1]-5:
tscore.clear()
t好的
gameover()
tscore.clear()
t好的
gameover()
#食物
def食品(tfood):
x=随机随机。随机范围(-8,8,1)
y=random.randrange(-8,8,1)
fcoord[0]=x
fcoord[1]=y
tfood.hideturtle()
t food.pu()
t好的形状(“圆”)
t食品颜色(“红色”)
tfood.goto(x*20,y*20)
tfood.stamp()
#向上
def u():
如果h[0]==270:
通过
其他:
h[0]=90
#向下
def d():
如果h[0]==90:
通过
其他:
h[0]=270
#左
def l():
如果h[0]==0:
通过
其他:
h[0]=180
#对
def r():
如果h[0]==180:
通过
其他:
h[0]=0
def move():
海龟。养老金(1)
海龟。颜色(“绿色”)
乌龟
乌龟。速度(3)
乌龟.设置标题(h[0])
龟形(“方形”)
乌龟邮票()
乌龟.fd(20)
x=turtle.xcor()
y=海龟。ycor()
如果b[0]>a[0]:
乌龟:1只
位置插入(0,[圆形(x),圆形(y)])
位置pop(-1)
其他:
位置插入(0,[圆形(x),圆形(y)])
b[0]+=1
def gameover():
海龟。屏幕单击(无)
乌龟。速度(0)
乌龟
乌龟。后藤(0150)
海龟。颜色(“红色”)
乌龟。写(“游戏结束”,align=“center”,font=(10))
乌龟。后藤(0,50)
编写(“分数:+str(a[0]),align=“center”,font=(10))
乌龟。后藤(200,-200)
write(“(单击任意位置返回主菜单)”,align=“right”,font=(0.0000001))
海龟。点击屏幕(主页)
tutle.mainloop()
# # # # # # # # # # # # # # # # # # # # # #
#主要#
# # # # # # # # # # # # # # # # # # # # # #
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
家(0,0)
我不知道你说的滞后是什么意思,但我在下面的重写中已经解决了几个问题。首先,海龟是全局实体,在正常情况下不会收集垃圾,所以不要重新创建它们,而是重用它们。其次,当你有自己的而循环控制游戏时,你可能会锁定事件。我已经消除了你的循环,并修改了游戏以处理ontimer
事件,因此乌龟的移动应该由处理用户输入的相同事件循环来处理,使其对用户的响应稍微更灵敏:
from turtle import Turtle, Screen
from random import randint
FONT = ('Arial', 18, 'bold')
# Up
def u():
if h[0] != 270:
h[0] = 90
# Down
def d():
if h[0] != 90:
h[0] = 270
# Left
def l():
if h[0] != 0:
h[0] = 180
# Right
def r():
if h[0] != 180:
h[0] = 0
def gameover():
screen.onkey(None, "Up")
screen.onkey(None, "Left")
screen.onkey(None, "Right")
screen.onkey(None, "Down")
tscore.clear()
tfood.clear()
tplayer.clear()
tfood.hideturtle()
tplayer.hideturtle()
tscore.color("red")
tscore.goto(0, 150)
tscore.write("Game Over", align="center", font=FONT)
tscore.goto(0, 50)
tscore.write("Score:" + str(a[0]), align="center", font=FONT)
tscore.goto(0, -200)
tscore.write("(Click anywhere to return to the main menu)", align="center", font=FONT)
screen.onscreenclick(home)
def food(tfood):
x = randint(-160, 160)
y = randint(-160, 160)
tfood.goto(x, y)
tfood.showturtle()
def move():
x, y = tplayer.position()
if -210 < x < 210 and -210 < y < 210:
if not tfood.isvisible():
food(tfood)
tplayer.setheading(h[0])
tplayer.stamp()
tplayer.forward(20)
if b[0] > a[0]:
tplayer.clearstamps(1)
pos.insert(0, [round(x), round(y)])
pos.pop(-1)
else:
pos.insert(0, [round(x), round(y)])
b[0] += 1
if tplayer.distance(tfood) < 15:
tfood.hideturtle()
tfood.clear()
a[0] += 1
tscore.clear()
tscore.write("Score:" + str(a[0]), align="center", font=FONT)
flag = True
x, y = tplayer.position()
if len(pos) > 1:
for i in range(1, len(pos)):
if pos[i][0] - 5 < x < pos[i][0] + 5 and pos[i][1] - 5 < y < pos[i][1] + 5:
flag = False
break
if flag:
screen.ontimer(move, 25)
else:
screen.ontimer(gameover, 100)
def level_1():
tmarker.penup()
tmarker.goto(-220, 220)
tmarker.pendown()
tmarker.goto(220, 220)
tmarker.goto(220, -220)
tmarker.goto(-220, -220)
tmarker.goto(-220, 220)
tmarker.penup()
def start(x, y):
screen.onscreenclick(None)
tscore.clear()
level_1()
tplayer.home()
tplayer.setheading(h[0])
tscore.goto(100, -250)
tscore.write("Score:" + str(a[0]), align="center", font=FONT)
screen.onkey(u, "Up")
screen.onkey(l, "Left")
screen.onkey(r, "Right")
screen.onkey(d, "Down")
move()
def home(x=0, y=0):
screen.onscreenclick(None)
a[0] = 0
b[0] = 0
h[0] = 0
pos[:] = []
tscore.clear()
tscore.home()
tscore.color('lime')
tscore.write("PLAY", align="center", font=FONT)
screen.onscreenclick(start)
# head orientation
h = [0]
# score
a = [0]
b = [0]
# position
pos = []
# turtles
tfood = Turtle('circle', visible=False)
tfood.speed('fastest')
tfood.color('red')
tfood.penup()
tscore = Turtle(visible=False)
tscore.speed('fastest')
tscore.penup()
tplayer = Turtle("square", visible=False)
tplayer.speed('slow')
tplayer.color("green")
tplayer.penup()
tmarker = Turtle(visible=False)
tmarker.speed('fastest')
tmarker.pensize(20)
tmarker.color("grey")
# # # # # # # # # # # # # # # # # # # # # #
# Main #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
screen = Screen()
screen.title("Snake Game")
screen.listen()
home()
screen.mainloop()
从海龟导入海龟,屏幕
从随机导入randint
字体=('Arial',18',粗体')
#向上
def u():
如果h[0]!=270:
h[0]=90
#向下
def d():
如果h[0]!=90:
h[0]=270
#左
def l():
如果h[0]!=0:
h[0]=180
#对
def r():
如果h[0]!=180:
h[0]=0
def gameover():
屏幕打开键(无,“向上”)
屏幕打开键(无,“左”)
屏幕。ON键(无,“右”)
屏幕打开键(无,“向下”)
tscore.clear()
t好的
tplayer.clear()
tfood.hideturtle()
tplayer.hideturtle()
t颜色(“红色”)
tscore.goto(0150)
tscore.write(“游戏结束”,align=“中心”,font=font)
tscore.goto(0,50)
tscore.write(“分数:+str(a[0]),align=“center”,font=font)
tscore.goto(0,-200)
tscore.write(“(单击任意位置返回主菜单)”,align=“center”,font=font)
屏幕。屏幕上单击(主页)
def食品(tfood):
x=randint(-160160)
y=randint(-160160)
tfood.goto(x,y)
t食物
def move():
x、 y=tplayer.position()
如果-210a[0]:
tplayer.clearstamps(1)
位置插入(0,[圆形(x),圆形(y)])
位置pop(-1)
其他:
位置插入(0,[圆形(x),圆形(y)])
b[0]+=1
如果tplayer.距离(tfood)<15:
tfood.hideturtle()
t好的
a[0]+=1
tscore.clear()
tscore.write(“分数:+str(a[0]),align=“center”,font=font)
flag=True
x、 y=tplayer.position()
如果长度(位置)>1:
对于范围内的i(1,le