pygame.error:文本的宽度为零;有什么更好的方法可以使它起作用?

时间:2019-04-01 20:02:13

标签: python oop text pygame

我正在尝试编写一个完全基于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

2 个答案:

答案 0 :(得分:0)

在事件循环中不要执行pygame.quit()。请注意紧接display.update之后的操作。在退出循环后退出 作为最后一件事。

pygame.init()

done = False
while not done and ...:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True
      break # Other events became irrelevant.
  pygame.display.update()
  clock.tick(30)

# Now that we're done:
pygame.quit()

答案 1 :(得分:0)

pygame.quit()不会退出程序-只会退出/关闭pygame-这意味着它会从内存中删除pygame的元素。

但是在关闭pygame之后,它仍然执行pygame.display.update(),这带来了问题。

因此,您需要在exit()之后输入sys.exit()pygame.quit()才能立即退出程序。

 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()