[英]Why is my Pygame Zero program 'not responding' when I try to rotate Actor?
I've just started trying to teach myself Pygame Zero, because we are doing an assignment on it in class next term and I want to be prepared.我刚开始尝试自学 Pygame 零,因为我们在下学期的 class 做一个作业,我想做好准备。
I tried to make a simple program where your spaceship constantly accelerates forwards and you can use the left and right arrows to turn it.我试着做一个简单的程序,让你的飞船不断向前加速,你可以使用左右箭头来转动它。 The problem is that my code is bugging in two ways.
问题是我的代码有两种错误。 Firstly, when the program first starts, the ship is facing up, but it continually moves right and nothing can stop it, even though it should be accelerating in the same direction as the ship.
首先,程序刚开始时,船是朝上的,但它不断向右移动,没有什么可以阻止它,即使它应该在与船相同的方向加速。 Also, when I try and press the left and right arrows to turn it, the program freezes, and then stops responding.
此外,当我尝试按左右箭头转动它时,程序冻结,然后停止响应。 I've tried my hardest to fix it but there is very little online about Pygame Zero.
我已经尽力修复它,但网上关于 Pygame 零的信息很少。 If someone could help me I'd really appreciate it.
如果有人可以帮助我,我将不胜感激。
Heres my code:这是我的代码:
import pgzrun
ship=Actor('ship')
WIDTH = 800
HEIGHT = 450
def draw():
screen.clear()
screen.blit("background",(WIDTH,HEIGHT))
screen.fill((255, 255, 255))
ship.draw()
def on_key_down(key):
if key == keys.LEFT:
left = True
while left == True:
ship.angle += 10
if key == keys.RIGHT:
right = True
while right == True:
ship.angle -= 10
def on_key_up(key):
if key == keys.LEFT:
left = False
if key == keys.RIGHT:
right = False
def update():
if ship.angle >= 350:
ship.x += 0
ship.y -= 3.5
elif ship.angle >= 330:
ship.x += 0.1
ship.y -= 3.3
elif ship.angle >= 310:
ship.x += 0.3
ship.y -= 3.1
elif ship.angle >= 290:
ship.x += 0.5
ship.y -= 2.9
elif ship.angle >= 270:
ship.x += 0.7
ship.y -= 2.7
elif ship.angle >= 250:
ship.x += 0.9
ship.y -= 2.5
elif ship.angle >= 230:
ship.x += 1.1
ship.y -= 2.3
elif ship.angle >= 210:
ship.x += 1.3
ship.y -= 2.1
elif ship.angle >= 190:
ship.x += 1.5
ship.y -= 1.9
elif ship.angle >= 170:
ship.x += 1.7
ship.y -= 1.7
elif ship.angle >= 150:
ship.x += 1.9
ship.y -= 1.5
elif ship.angle >= 130:
ship.x += 2.1
ship.y -= 1.3
elif ship.angle >= 110:
ship.x += 2.3
ship.y -= 1.1
elif ship.angle >= 90:
ship.x += 2.5
ship.y -= 0.9
elif ship.angle >= 70:
ship.x += 2.7
ship.y -= 0.7
elif ship.angle >= 50:
ship.x += 2.9
ship.y -= 0.5
elif ship.angle >= 30:
ship.x += 3.1
ship.y -= 0.3
elif ship.angle >= 10:
ship.x += 3.3
ship.y -= 0.1
else:
ship.x += 3.5
ship.y -= 0
pgzrun.go()
This is the part that causes the freeze:这是导致冻结的部分:
def on_key_down(key):
if key == keys.LEFT:
left = True
while left == True:
ship.angle += 10
if key == keys.RIGHT:
right = True
while right == True:
ship.angle -= 10
As you can see, you start a while
loop with a condition that is always True
.如您所见,您开始了一个
while
循环,其条件始终为True
。 The code is constructed with the assumption that the on_key_up
method will be called as soon as the key is relased, and the variable controling the while
condition would be changed to False.代码的构造假设一旦键被释放就会调用
on_key_up
方法,并且控制while
条件的变量将更改为 False。 That could happen if this code would be multi-threading - then the other function could be called concurrently.如果此代码是多线程的,则可能会发生这种情况 - 然后可以同时调用另一个 function。 That is not the case: the main loop of pygame zero is single-threaded and depend on each of the called functions be run to completion and returning, before the game state can be updated.
事实并非如此:pygame 零的主循环是单线程的,并且依赖于每个被调用的函数运行到完成并返回,在游戏 state 可以更新之前。
(and even if it was multithreaded, and pygamezero could change the left
and right
checks to False, the two line while loop would update the ship angle several million times per second, when you probably want it to update 10 degrees (or less) per game frame ). (即使它是多线程的,并且
right
left
更改为 False,当您可能希望它每更新 10 度(或更小)时,两行 while 循环将每秒更新船角数百万次游戏框架)。
All you need to do is to upgrade the right
and left
variables indicating rotation on the "keyup" and "keydown" events, and make the angle change itself on the update function - that is called once per frame (I guess - there is a lot of time since I tried pygame zero).您需要做的就是
left
right
,并在更新 function 时自行改变角度 - 每帧调用一次(我猜 - 有一个自从我尝试 pygame 零以来已经有很多时间了)。
On a side note, you could get a mathematical formula for updating the ship position based on angle, and not rely on that chain of if/elifs - while it will work, it is not the most fancy code.附带说明一下,您可以获得基于角度更新船舶 position 的数学公式,而不依赖于 if/elifs 链 - 虽然它会起作用,但它不是最花哨的代码。
import math
...
def draw(...):
...
left = right = False
def on_key_down(key):
global left, right
if key == keys.LEFT:
left = True
if key == keys.RIGHT:
right = True
def on_key_up(key):
global left, right
if key == keys.LEFT:
left = False
if key == keys.RIGHT:
right = False
def update():
global left, right
if left:
ship.angle += 10
elif right:
ship.angle -= 10
# constrain the angle to 0-360 range:
ship.angle %= 360
ship.x += 3.5 * sin(math.radians(angle))
ship.y -= 3.5 * cos(math.radians(angle))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.