我的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] = 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)
答案 0 :(得分:0)
我不确定落后的含义,但我在下面的重写中解决了几个问题。首先,海龟是全球实体,在正常情况下不会收集垃圾,因此不要重新创建它们,重新使用它们。其次,当你有自己的while
循环来控制游戏时,你可能会锁定事件。我已经消除了你的循环并修改了游戏以使用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()