Python while循环中的time.sleep函数持续崩溃程序、解决方案或替代方法?
我试图用海龟制作一个太空入侵者游戏,为了让子弹在屏幕上移动,我试图每0.5秒更新一次子弹的y坐标。我尝试使用while循环和time.sleep()函数来实现这一点,但每次我尝试发射子弹时,程序都会崩溃。我有没有办法阻止它这样做?或者为什么它不起作用?有没有一种同样有效的方法不会使我的程序崩溃Python while循环中的time.sleep函数持续崩溃程序、解决方案或替代方法?,python,time,crash,sleep,turtle-graphics,Python,Time,Crash,Sleep,Turtle Graphics,我试图用海龟制作一个太空入侵者游戏,为了让子弹在屏幕上移动,我试图每0.5秒更新一次子弹的y坐标。我尝试使用while循环和time.sleep()函数来实现这一点,但每次我尝试发射子弹时,程序都会崩溃。我有没有办法阻止它这样做?或者为什么它不起作用?有没有一种同样有效的方法不会使我的程序崩溃 import turtle import time userx = 0 x = 0 y = -300 enemies = [(300,50, "left"), (400,50, "left"), (
import turtle
import time
userx = 0
x = 0
y = -300
enemies = [(300,50, "left"), (400,50, "left"), (350, -50, "right")]
bullets = []
gameover = False
def frame():
pass
ship = turtle.Turtle()
ship.pu()
ship.ht()
ship.setpos(userx, -300)
ship.pd()
ship.circle(5)
bullet = turtle.Turtle()
bullet.pu()
bullet.ht()
bullet.setpos(x, y)
bullet.pd()
bullet.circle(2)
def key_left():
global userx
pass
userx += -10
print(userx)
def key_right():
global userx
pass
userx += 10
print(userx)
def key_space():
pass # your code here
global x
global y
global bullets
x = userx
while y <= 300:
time.sleep(0.5)
y += 4
else: y = 0
bullets += (x,y)
def physics():
global bullets
global enemies
pass
def ai():
global enemies
global gameover
pass
def reset():
global enemies
global bullets
global userx
global gameover
pass
def main():
turtle.tracer(0,0)
turtle.hideturtle()
turtle.onkey(key_left, "Left")
turtle.onkey(key_right, "Right")
turtle.onkey(key_space, "space")
turtle.listen()
reset()
while not gameover:
turtle.clear()
physics()
ai()
frame()
turtle.update()
time.sleep(0.05)
main()
导入海龟
导入时间
userx=0
x=0
y=-300
敌人=[(300,50,“左”),(400,50,“左”),(350,-50,“右”)]
项目符号=[]
gameover=False
def frame():
通过
ship=海龟
ship.pu()
ship.ht()
ship.setpos(userx,-300)
ship.pd()
船。圆圈(5)
子弹=乌龟。乌龟()
bullet.pu()
bullet.ht()
bullet.setpos(x,y)
bullet.pd()
项目符号。圆圈(2)
def键_左()
全局用户X
通过
userx+=-10
打印(userx)
def key_right():
全局用户X
通过
userx+=10
打印(userx)
def key_space():
把你的密码传过来
全球x
全局y
全球子弹
x=userx
而y我会:
def key_space():
# simply spawn a new bullet and put it into your bullets list
def advance_bullet(): # call this after paintnig all bullets in frame
# iterate over all bullets inside bullets, advance the Y position by 3
def frame():
# add code to paint all bullets inside bullets - and call advance_bullets()
在检查是否与敌人发生碰撞的代码中:
# remove a bullet from bullets when its outside your screen (or passed all enemies max y coord) - no need to track that bullet anymore
如果您希望使项目符号的前进速度比主循环间隔慢,则制作一个“framespassed”计数器,查看是否framespassed%something==0
,然后再推进项目符号
适应您已有的内容
您需要更改这些部分:
def frame():
global bullets
pass
ship = turtle.Turtle()
ship.pu()
ship.ht()
ship.setpos(userx, -300)
ship.pd()
ship.circle(5)
# debugging all bullets:
# print(bullets) # remove this
for idx in range(0,len(bullets)): # paint ALL bullets in the bullets list
bulletTurtle = turtle.Turtle()
b = bullets[idx] # current bullet we are painting
bulletTurtle.pu()
bulletTurtle.ht()
bulletTurtle.setpos(b[0], b[1])
bulletTurtle.pd()
bulletTurtle.circle(2)
b[1] += 13 # quick and dirty approach, move bulltet after painting
# to save another complete bullets-foreach-loop
# quick n dirty bullet removal for out of screen bullets
# normally I would do this probably in your "check if enemy hit"
# method as you are going over bullets there as well and can remove
# them as soon as they passed all enemies
bullets = [x for x in bullets if x[1] < 500]
def key_space():
global x
global y
global bullets
bullets.append([userx,-300]) # start a new bullet at current player position
def frame():
全球子弹
通过
ship=海龟
ship.pu()
ship.ht()
ship.setpos(userx,-300)
ship.pd()
船。圆圈(5)
#调试所有项目符号:
#打印(项目符号)#删除此
对于范围内的idx(0,len(项目符号)):#绘制项目符号列表中的所有项目符号
bulletTurtle=海龟。海龟()
b=子弹[idx]#我们正在绘制的当前子弹
bullettle.pu()
bullettle.ht()
bullettle.setpos(b[0],b[1])
bullettle.pd()
公告牌.圆圈(2)
b[1]+=13#快速且肮脏的接近,涂漆后移动Bullette
#保存每个循环的另一个完整项目符号
#快速清除屏幕外子弹的脏子弹
#通常我会在你的“检查敌人是否击中”中这样做
#方法,因为您正在检查子弹以及可以删除
#他们一经过所有的敌人就把他们赶走了
项目符号=[x代表x,如果x[1]<500]
def key_space():
全球x
全局y
全球子弹
子弹。追加([userx,-300])#在当前玩家位置开始一个新子弹
编辑:
你可能想看看turtle.shape、turtle.size和turtle.stamp——通过使用“圆形”形状和合适的尺寸,你可能可以“压印”这个形状。优点:你可以简单地删除印记的形状-它的整数ID。我还没有与乌龟合作过-想想你自己,如果这是一个很容易重新绘制你的球员位置和/或子弹的方式< /P> < P>我会:
def key_space():
# simply spawn a new bullet and put it into your bullets list
def advance_bullet(): # call this after paintnig all bullets in frame
# iterate over all bullets inside bullets, advance the Y position by 3
def frame():
# add code to paint all bullets inside bullets - and call advance_bullets()
在检查是否与敌人发生碰撞的代码中:
# remove a bullet from bullets when its outside your screen (or passed all enemies max y coord) - no need to track that bullet anymore
如果您希望使项目符号的前进速度比主循环间隔慢,则制作一个“framespassed”计数器,查看是否framespassed%something==0
,然后再推进项目符号
适应您已有的内容
您需要更改这些部分:
def frame():
global bullets
pass
ship = turtle.Turtle()
ship.pu()
ship.ht()
ship.setpos(userx, -300)
ship.pd()
ship.circle(5)
# debugging all bullets:
# print(bullets) # remove this
for idx in range(0,len(bullets)): # paint ALL bullets in the bullets list
bulletTurtle = turtle.Turtle()
b = bullets[idx] # current bullet we are painting
bulletTurtle.pu()
bulletTurtle.ht()
bulletTurtle.setpos(b[0], b[1])
bulletTurtle.pd()
bulletTurtle.circle(2)
b[1] += 13 # quick and dirty approach, move bulltet after painting
# to save another complete bullets-foreach-loop
# quick n dirty bullet removal for out of screen bullets
# normally I would do this probably in your "check if enemy hit"
# method as you are going over bullets there as well and can remove
# them as soon as they passed all enemies
bullets = [x for x in bullets if x[1] < 500]
def key_space():
global x
global y
global bullets
bullets.append([userx,-300]) # start a new bullet at current player position
def frame():
全球子弹
通过
ship=海龟
ship.pu()
ship.ht()
ship.setpos(userx,-300)
ship.pd()
船。圆圈(5)
#调试所有项目符号:
#打印(项目符号)#删除此
对于范围内的idx(0,len(项目符号)):#绘制项目符号列表中的所有项目符号
bulletTurtle=海龟。海龟()
b=子弹[idx]#我们正在绘制的当前子弹
bullettle.pu()
bullettle.ht()
bullettle.setpos(b[0],b[1])
bullettle.pd()
公告牌.圆圈(2)
b[1]+=13#快速且肮脏的接近,涂漆后移动Bullette
#保存每个循环的另一个完整项目符号
#快速清除屏幕外子弹的脏子弹
#通常我会在你的“检查敌人是否击中”中这样做
#方法,因为您正在检查子弹以及可以删除
#他们一经过所有的敌人就把他们赶走了
项目符号=[x代表x,如果x[1]<500]
def key_space():
全球x
全局y
全球子弹
子弹。追加([userx,-300])#在当前玩家位置开始一个新子弹
编辑:
你可能想看看turtle.shape、turtle.size和turtle.stamp——通过使用“圆形”形状和合适的尺寸,你可能可以“压印”这个形状。优点:你可以简单地删除印记的形状-由它的整数ID。我还没有与乌龟合作过-想想你自己,如果这是一个很容易重新绘制你的球员位置和/或子弹< < /P> > P>的方法,它不理想,但它开始工作。< /P>
在key\u空格中
只向列表中添加新项目符号。
在而不是gameover
中,您运行将移动所有项目符号的函数。
在frame
中绘制所有项目符号
import turtle
import time
userx = 0
x = 0
y = -300
enemies = [(300,50, "left"), (400,50, "left"), (350, -50, "right")]
bullets = []
gameover = False
def frame():
pass
ship = turtle.Turtle()
ship.pu()
ship.ht()
ship.setpos(userx, -300)
ship.pd()
ship.circle(5)
# redraw bullets
for x, y in bullets:
bullet = turtle.Turtle()
bullet.pu()
bullet.ht()
bullet.setpos(x, y)
bullet.pd()
bullet.circle(2)
def key_left():
global userx
pass
userx += -10
print(userx)
def key_right():
global userx
pass
userx += 10
print(userx)
def key_space():
pass # your code here
global x
global y
global bullets
x = userx
# add bullet to list
bullets.append([x, -300])
def move_bullets():
global bullets
live_bullets = []
# move every bullet and check if should still live
for x, y in bullets:
y += 4
# keep only some bullets and other forget (kill/remove)
if y <= 300:
live_bullets.append([x, y])
bullets = live_bullets
def physics():
global bullets
global enemies
pass
def ai():
global enemies
global gameover
pass
def reset():
global enemies
global bullets
global userx
global gameover
pass
def main():
turtle.tracer(0,0)
turtle.hideturtle()
turtle.onkey(key_left, "Left")
turtle.onkey(key_right, "Right")
turtle.onkey(key_space, "space")
turtle.listen()
reset()
while not gameover:
turtle.clear()
physics()
ai()
move_bullets() # <-- move bullets (or maybe before physics)
frame()
turtle.update()
time.sleep(0.05)
main()
导入海龟
导入时间
userx=0
x=0
y=-300
敌人=[(300,50,“左”),(400,50,“左”),(350,-50,“右”)]
项目符号=[]
gameover=False
def frame():
通过
ship=海龟
ship.pu()
ship.ht()
ship.setpos(userx,-300)
ship.pd()
船。圆圈(5)
#重画子弹
对于项目符号中的x,y:
子弹=乌龟。乌龟()
bullet.pu()
bullet.ht()
bullet.setpos(x,y)
bullet.pd()
项目符号。圆圈(2)
def键_左()
全局用户X
通过
userx+=-10
打印(userx)
def key_right():
全局用户X
通过
userx+=10
打印(userx)
def key_space():
把你的密码传过来
全球x
G