简体   繁体   English

当我尝试旋转 Actor 时,为什么我的 Pygame 零程序“没有响应”?

[英]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.

相关问题 当我尝试运行代码时,Pygame 窗口没有响应错误 - Pygame window not responding error when i try to run a code 为什么我的 pygame 应用程序没有响应? - Why is my pygame application not responding? Pygame 已安装,但是当我尝试运行我的程序时,window 不会打开 - Pygame is installed, but when I try to run my program the window won't open 我的 pygame window 当我使用 collidepoint() - My pygame window says “not responding” when I use collidepoint() 当我尝试用我的代码导入它时,为什么不能升华找到 pygame? - Why cant sublime find pygame when i try to import it with my code? 当我尝试退出它时,为什么我的 pygame 窗口不是白色并且没有保持打开和退出? - Why isn't my pygame window not white and not staying open and quitting when I try to exit it? 每当我尝试使用 pygame 运行我的 python 代码时,window 打开,然后在一秒钟或任何输入后停止响应 - Whenever i try run my python code with pygame the window opens and then stops responding after a second or any input 为什么当我移动鼠标时,我的 pygame 程序会变慢? - Why does my pygame program slowdown, when I move my mouse? 我做了一个非常简单的 Pygame 零游戏,但是我的 Actor 图像和 screen.clear() 不起作用。 你能帮我解决这个问题吗? - I made a very simple Pygame Zero game, but my Actor image and the screen.clear() is not working. Can you help me fix this? 我的程序无法运行,而不是使用黑色背景,而是尝试将图像用作背景错误:pygame.surface'对象不可调用 - My program will not run, when instead of using a black background i try to use a image as background ERROR: pygame.surface' object is not callable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM