【问题标题】:Pygame crashes after only a few seconds of rendering my codePygame 在渲染我的代码几秒钟后就崩溃了
【发布时间】:2020-11-22 02:59:00
【问题描述】:

由于某种原因,这个程序在运行大约一秒钟后就崩溃了。有趣的是,改变 turnAngle 或 fps 会影响它在崩溃前的持续时间。它抛出的错误是 ValueError: subsurface rectangle outside of surface area 第 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)

【问题讨论】:

  • 不,这个答案并不真正适用于我的代码。这实际上让我非常困惑。

标签: python pygame crash


【解决方案1】:

见How do I rotate an image around its center using PyGame?

此外,您还必须处理应用程序循环中的事件。分别见pygame.event.get()pygame.event.pump():

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

例如:

import pygame as pg

def rot_center(image, angle, center):
    rotated_image = pg.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = center)
    return rotated_image, new_rect

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()
run = True
crashed = False
while not crashed and run:

    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False

    frame += 1
    angle = (frame*turnAngle) % 360
    centerGreen = (189, 189)
    centerRed = (453, 230)

    mainDisplay.blit(imgSplashBG, (0, 0))
    mainDisplay.blit( *rot_center(imgSplashGreenCircle, angle, centerGreen) )
    mainDisplay.blit( *rot_center(imgSplashRedCircle, angle,centerRed) )
    mainDisplay.blit(imgSplashFG, (0, 0))
    pg.display.flip()
    clock.tick(30)

pygame.quit()

【讨论】:

  • 难以置信!我不知道为什么,但这完全解决了问题。我不敢相信我忘记添加了。
【解决方案2】:

你可以尝试在你的循环之前添加一个时间变量'while not'

像这样:

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

【讨论】:

  • 我继续添加该行,但它仍然崩溃。
猜你喜欢
  • 1970-01-01
  • 2013-12-23
  • 2020-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多