我试图写一个完全基于GUI的文本冒险(有框显示文本以及按钮,您可以点击移动和攻击)。我在显示我的框时遇到了一些小问题,以下是违规的代码行:
下一个文件中导入的相关变量:
textWhite = (240, 234, 214)错误发生的地方
from globalvars import *
import pygame
class AbstractBanner:
    def __init__(self, centerX, centerY, width, height, color):
        self.leftX = centerX - width//2
        self.topY = centerY - height//2
        self.width = width
        self.height = height
        self.color = color
        self.gameDisplay = pygame.display.set_mode((scrW, scrH))
class Banner(AbstractBanner):
    def __init__(self, centerX, centerY, width, height, color, text, textSize):
        super().__init__(centerX, centerY, width, height, color)
        self.text = text
        self.textObject = pygame.font.SysFont("arial.ttf", textSize)
    def display(self):
        surface = self.textObject.render(self.text, True, textWhite)
        rect = surface.get_rect()
        rect.center = (self.leftX, self.topY)
        self.gameDisplay.blit(surface, rect)
        pygame.draw.rect(self.gameDisplay, self.color, (self.leftX, self.topY, self.width, self.height))执行上述代码的截断版本(无游戏循环):
class screenDisplay():
    def __init__(self):
        self.state = "TITLE_SCREEN"
        self.states = ["TITLE_SCREEN", "MAIN_GAME", "COMBAT", "CUTSCENE"]
        if self.state == self.states[0]:
            self.showTitleScreen()
def showTitleScreen(self):
        #Background
        background = Banner(scrW//2, scrH//2, scrW, scrH, avocado, "  ", 2)
        background.display()这里,在第四个.py文件中,是执行上述操作的游戏循环:
import pygame
import time
from screenDisplay import screenDisplay
import globalvars
# pylint: disable=no-member 
pygame.init() 
# pylint: enable=no-member
clock = pygame.time.Clock()
showGame = screenDisplay()
while not globalvars.gameIsDone:
    for event in pygame.event.get():
        # pylint: disable=no-member 
        if event.type == pygame.QUIT:
            done = True
            pygame.quit() 
            # pylint: enable=no-member
    pygame.display.update()
    clock.tick(30)我读过关于this的问题,但是我不知道我应该把del行放在哪里,甚至不知道我应该删除什么。如果解决方案涉及将这两行放在同一个文件中,那么还有其他选择吗?
编辑:哦,最重要的是,这是错误!
Traceback (most recent call last):
  File "c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\gameLauncher.py", line 9, in <module>
    showGame = screenDisplay()
  File "c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\screenDisplay.py", line 9, in __init__
    self.showTitleScreen()
  File "c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\screenDisplay.py", line 26, in showTitleScreen
    background.display()
  File "c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\Banner.py", line 19, in display
    surface = self.textObject.render(self.text, True, textWhite)
pygame.error: Text has zero width发布于 2019-04-01 20:33:34
pygame.quit()不退出程序-它只退出/关闭pygame -这意味着它从内存中删除pygame的元素。
但是当你关闭游戏后,它仍然会执行pygame.display.update(),这就产生了问题。
因此,您需要exit()或pygame.quit()之后的sys.exit()立即退出程序。
 if event.type == pygame.QUIT:
     pygame.quit() 
     exit() 
     #sys.exit()或者您应该保留while循环--使用globalvars.gameIsDone = True而不是done = True,然后使用pygame.quit() --类似于@9000答案。
while not globalvars.gameIsDone:
    for event in pygame.event.get():
        # pylint: disable=no-member 
        if event.type == pygame.QUIT:
            globalvars.gameIsDone = True
            # pylint: enable=no-member
    pygame.display.update()
    clock.tick(30)
pygame.quit() https://stackoverflow.com/questions/55462724
复制相似问题