1

所以在我决定问这个问题之前,我做了一些研究,看看我的问题是什么,并遇到了这个问题:Code Only Produces Black Screen In Pygame Window然而,这并不是很有帮助,因为我没有遇到与这个相同的问题人。我肯定知道我的 PyGame 并没有过时或类似的东西,因为我的代码的先前版本正在运行。

每次我运行代码时,都会出现黑屏。

这是我的代码(抱歉有点长):

import pygame
import 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()**但是就像我之前说的那样,当我运行代码时出现黑屏。但由于某种原因,鼠标坐标跟踪器仍然有效。任何帮助是极大的赞赏。

4

2 回答 2

5

问题是您的代码组织。

你有树次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()功能中删除 - 使用一个屏幕到程序结束。

现在我可以看到带有“hello”的窗格

于 2013-11-13T18:26:32.207 回答
1

好的,所以我现在已经解决了我的问题。(啊哈新手错误)。因此,在__init__(self)该区域内,class Pane()我添加了一行self.Screen.fill((white)),使其看起来像这样:

    def __init__(self):
        self.Screen = pygame.display.set_mode((1000,600), 0, 32)
        self.font = pygame.font.SysFont('Arial', 25)
        self.Screen.fill((white))

我不确定这是否是解决问题的最佳方法,但它确实有效。所以这很好。但是,如果您认为这不是解决问题的好方法,那么一定要教我一个更好的方法来解决我的问题。

于 2013-11-13T17:29:22.470 回答