Python Pygame仅在呈现我的代码几秒钟后崩溃

Python Pygame仅在呈现我的代码几秒钟后崩溃,python,pygame,crash,Python,Pygame,Crash,出于某种原因,此程序在运行大约一秒钟后崩溃。有趣的是,改变转角或fps会影响碰撞前的持续时间。它抛出的错误是一个错误 ValueError:第29行/第9行的表面积外的次表面矩形。有什么想法吗 import pygame as pg def rot_center(image, angle): orig_rect = image.get_rect() rot_image = pg.transform.rotate(image, angle) rot_rect = ori

出于某种原因,此程序在运行大约一秒钟后崩溃。有趣的是,改变转角或fps会影响碰撞前的持续时间。它抛出的错误是一个错误
ValueError:第29行/第9行的表面积外的次表面矩形。有什么想法吗

import pygame as pg


def rot_center(image, angle):
    orig_rect = image.get_rect()
    rot_image = pg.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image


pg.init()

mainDisplay = pg.display.set_mode([600, 376])

imgSplashBG = pg.image.load('guiAssets/splash/BG.png')
imgSplashFG = pg.image.load('guiAssets/splash/FG.png')
imgSplashRedCircle = pg.image.load('guiAssets/splash/redCircle.png')
imgSplashGreenCircle = pg.image.load('guiAssets/splash/greenCircle.png')

turnAngle = 10

frame = 0
clock = pg.time.Clock()
crashed = False
while not crashed:
    frame += 1
    mainDisplay.blit(imgSplashBG, (0, 0))
    mainDisplay.blit(rot_center(imgSplashGreenCircle, ((frame % 360) * 10)), (-189, -189))
    mainDisplay.blit(rot_center(imgSplashRedCircle, ((frame % 360) * 10)), (453, 230))
    mainDisplay.blit(imgSplashFG, (0, 0))
    pg.display.flip()
    clock.tick(30)

能否尝试在循环“while not”之前添加时间变量

像这样:

.....
frame = 0
clock = pg.time.Clock()
time = pg.time.get_ticks()      #here
crashed = False
while not .....

能否尝试在循环“while not”之前添加时间变量

像这样:

.....
frame = 0
clock = pg.time.Clock()
time = pg.time.get_ticks()      #here
crashed = False
while not .....

此外,还必须处理应用程序循环中的事件。分别见:

对于游戏的每一帧,您都需要对事件队列进行某种调用。这确保了您的程序可以在内部与操作系统的其余部分交互

例如:

将pygame导入为pg
def旋转中心(图像、角度、中心):
旋转的图像=pg.transform.rotate(图像,角度)
新建=旋转图像。获取(中心=中心)
返回旋转的图像,新建
第init页()
mainDisplay=pg.display.set_模式([600376])
imgSplashBG=pg.image.load('guiAssets/splash/BG.png')
imgSplashFG=pg.image.load('guiAssets/splash/FG.png')
imgSplashRedCircle=pg.image.load('guiAssets/splash/redCircle.png')
imgSplashGreenCircle=pg.image.load('guiAssets/splash/greenCircle.png')
转角=10
帧=0
时钟=pg.time.clock()
运行=真
崩溃=错误
未崩溃并运行时:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
运行=错误
帧+=1
角度=(机架*转角)%360
中心绿=(189189)
居中=(453230)
main display.blit(imgSplashBG,(0,0))
主显示。blit(*旋转中心(IMGSPLASHGREENCELE,角度,中心绿色))
主显示。blit(*旋转中心(imgSplashRedCircle,角度,中心))
main display.blit(imgSplashFG,(0,0))
pg.display.flip()
时钟滴答(30)
pygame.quit()
请参见

此外,还必须处理应用程序循环中的事件。分别见:

对于游戏的每一帧,您都需要对事件队列进行某种调用。这确保了您的程序可以在内部与操作系统的其余部分交互

例如:

将pygame导入为pg
def旋转中心(图像、角度、中心):
旋转的图像=pg.transform.rotate(图像,角度)
新建=旋转图像。获取(中心=中心)
返回旋转的图像,新建
第init页()
mainDisplay=pg.display.set_模式([600376])
imgSplashBG=pg.image.load('guiAssets/splash/BG.png')
imgSplashFG=pg.image.load('guiAssets/splash/FG.png')
imgSplashRedCircle=pg.image.load('guiAssets/splash/redCircle.png')
imgSplashGreenCircle=pg.image.load('guiAssets/splash/greenCircle.png')
转角=10
帧=0
时钟=pg.time.clock()
运行=真
崩溃=错误
未崩溃并运行时:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
运行=错误
帧+=1
角度=(机架*转角)%360
中心绿=(189189)
居中=(453230)
main display.blit(imgSplashBG,(0,0))
主显示。blit(*旋转中心(IMGSPLASHGREENCELE,角度,中心绿色))
主显示。blit(*旋转中心(imgSplashRedCircle,角度,中心))
main display.blit(imgSplashFG,(0,0))
pg.display.flip()
时钟滴答(30)
pygame.quit()

我继续添加了那一行,但它仍然崩溃。我继续添加了那一行,但它仍然崩溃。不,这个答案并不适用于我的代码。这实际上让我非常困惑。不,这个答案并不适用于我的代码。这对我来说真是太让人迷惑了。难以置信!我不知道为什么,但这完全解决了问题。我真不敢相信我忘了加上那个。难以置信!我不知道为什么,但这完全解决了问题。我真不敢相信我忘了加上那个。