Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygame.error:文本的宽度为零;什么';这是一个更好的办法吗?_Python_Oop_Text_Pygame - Fatal编程技术网

Python pygame.error:文本的宽度为零;什么';这是一个更好的办法吗?

Python pygame.error:文本的宽度为零;什么';这是一个更好的办法吗?,python,oop,text,pygame,Python,Oop,Text,Pygame,我正在尝试编写一个完全基于GUI的文本冒险(有一些显示文本的框以及可以单击移动和攻击的按钮)。我在展示我的箱子时遇到了一点麻烦 以下是有问题的代码行: 下一个文件中导入的相关变量: textWhite = (240, 234, 214) 发生错误的地方 from globalvars import * import pygame class AbstractBanner: def __init__(self, centerX, centerY, width, height, colo

我正在尝试编写一个完全基于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))
执行上述代码的截断版本(无游戏循环):

这里,在第四个.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)
我已经阅读了关于这一点的问题,但我不知道我应该把
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
回溯(最近一次呼叫最后一次):
文件“c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\gameLauncher.py”,第9行,在
showGame=屏幕显示()
文件“c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\screensdisplay.py”,第9行,在\uuu init中__
self.showtTitleScreen()
showTitleScreen第26行的文件“c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\screenDisplay.py”
background.display()
文件“c:\Users\Robertson\Desktop\Personal Projects\pytextrpg\Banner.py”,第19行,显示
surface=self.textObject.render(self.text,True,textWhite)
pygame.error:文本的宽度为零
不要在事件循环中执行pygame.quit()。请注意如何显示
。在显示后立即更新
。退出循环后,作为最后一件事退出

pygame.init()
完成=错误
虽然没有完成,但…:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
休息#其他事件变得无关紧要。
pygame.display.update()
时钟滴答(30)
#现在我们完成了:
pygame.quit()
不要在事件循环中执行pygame.quit()
。请注意如何显示
。在显示后立即更新
。退出循环后,作为最后一件事退出

pygame.init()
完成=错误
虽然没有完成,但…:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
休息#其他事件变得无关紧要。
pygame.display.update()
时钟滴答(30)
#现在我们完成了:
pygame.quit()
pygame.quit()
不会退出程序-它只会退出/关闭
pygame
,这意味着它会从内存中删除
pygame
的元素

但在关闭pygame后,它仍然执行
pygame.display.update()
,这就产生了问题

所以您需要在
pygame.quit()之后
exit()
sys.exit()
立即退出程序

 if event.type == pygame.QUIT:
     pygame.quit() 
     exit() 
     #sys.exit()
或者你应该在循环时离开
,使用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() 
pygame.quit()
不会退出程序-它只会退出/关闭
pygame
-这意味着它会从内存中删除
pygame
的元素

但在关闭pygame后,它仍然执行
pygame.display.update()
,这就产生了问题

所以您需要在
pygame.quit()之后
exit()
sys.exit()
立即退出程序

 if event.type == pygame.QUIT:
     pygame.quit() 
     exit() 
     #sys.exit()
或者你应该在循环时离开
,使用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() 

谢谢你,但我还是犯了同样的错误。我确实更改了globalvars.gameIsDone,但我不得不这样做。在
pygame.quit()之后,您是否在绘图?那幅画就是问题所在。它是最后一个运行的东西吗?我不知道。谢谢你,但我还是犯了同样的错误。我确实更改了globalvars.gameIsDone,但我不得不这样做。在
pygame.quit()之后,您是否在绘图?那幅画就是问题所在。它是最后一个运行的东西吗?我不知道。