困惑为什么PyGame显示屏为黑屏
因此,在决定问这个问题之前,我做了一点研究,以了解问题所在,并发现了这个问题:代码仅在Pygame窗口中产生黑屏,但这并不是全部有用,因为我没有遇到与此相同的问题人。我肯定知道我的PyGame没有过时或类似的事情,因为我的代码在以前的版本中可以正常工作。
每次我运行代码时,最终都会出现黑屏。
这是我的代码(有点抱歉):
import pygameimport sys
from pygame.locals import *
white = (255,255,255)
black = (0,0,0)
objs = []
MAIN_BUTTON = 1
class Pane():
def __init__(self):
self.Screen = pygame.display.set_mode((1000,600), 0, 32)
self.font = pygame.font.SysFont('Arial', 25)
def drawPane(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane1(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane2(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane3(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane4(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane5(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane6(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane7(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane8(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
def drawPane9(self, textToDisplay):
self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
pygame.draw.rect(self.Screen, (black), (175, 75, 200, 100), 2)
class Screen():
def __init__(self):
pygame.init()
self.font = pygame.font.SysFont('Arial', 25)
pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((1000,600), 0, 32)
self.screen.fill((white))
pygame.display.update()
numberOfPanes = 0
self.NoOfPanes = numberOfPanes
def addPane(self, textToDisplay):
myPane = Pane()
myPane.drawPane(textToDisplay)
def clearScreen(self):
self.screen = pygame.display.set_mode((600,400), 0, 32)
self.screen.fill((white))
pygame.display.update()
def mousePosition(self):
global clickPos
global releasePos
for event in pygame.event.get():
if event.type == MAIN_BUTTON:
self.Pos = pygame.mouse.get_pos()
return MAIN_BUTTON
else:
return False
if __name__ == '__main__':
Pan3 = Screen()
Pan3.addPane("hello")
Pan3.mousePosition()
pygame.display.update()
while True:
ev = pygame.event.get()
for event in ev:
if event.type == pygame.MOUSEBUTTONUP:
posx,posy = pygame.mouse.get_pos()
print(posx)
print(posy)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();
所以我基本上是试图从中显示窗格(现在是一个)**class Pane()**
,**class
Screen()**但是就像我在运行代码之前所说的那样,我得到了黑屏。但是由于某些原因,鼠标坐标跟踪器仍然可以工作。任何帮助是极大的赞赏。
回答:
问题是您的代码组织。
你有树时间pygame.display.set_mode()
。每次您呼叫pygame.display.set_mode()
销毁先前的设备时screen
,创建screen
新屏幕和新屏幕始终为黑色。
您应该screen
只创建一次,然后将其作为参数发送到其他类。
def addPane(self, textToDisplay): myPane = Pane(self.screen) # send screen to Pane
myPane.drawPane(textToDisplay)
# ...
class Pane():
def __init__(self, screen):
self.Screen = screen # get screen
并pygame.display.set_mode()
从clear()
功能中删除-使用一个屏幕显示程序的末尾。
现在,我可以看到带有“你好”的窗格
以上是 困惑为什么PyGame显示屏为黑屏 的全部内容, 来源链接: utcz.com/qa/416322.html