我还是python和OpenGL的新手。我写了一个简短的代码来移动汽车穿过z轴,here是我完全编写的代码,如果需要的话
我想要做的是在用户点击" p"时暂停游戏,在" r" " q"然后退出程序。 ...退出工作完美但我尽管我所有的试验都不能重新启动或暂停游戏,这里是我写的部分,以考虑所有情况:
def keyboard (key,x,y):
if key == b"q" :
sys.exit(0)
if key == b"r" :
restart_program()
if key == b"p":
pause ()
P.S。 :我尝试了Pygame的自由,但它并没有像预期的那样正常工作
任何帮助?!
答案 0 :(得分:0)
See the link on my comment for a more complete way of handling pausing, but you could also just refactor your drawing code to something along the lines of:
elapsed_time = 0
paused = False
def update():
start_time = time.time()
if key == b"r" :
paused = False
if key == b"p":
paused = True
if paused:
elapsed_time = time.time() - start_time
else:
elapsed_time = 0
UpdateAllTheThings(elapsed_time)
while True:
update()
Then, while updating all the moving parts of your system, you will want to scale each move by the amount to time passed. By having a separate update()
function which takes elapsed time as a parameter, you can simulate pausing by passing it 0
. The system of game states makes this far more robust and extensible, it's worth learning about.