为什么PyGame显示黑屏感到困惑

时间:2013-11-13 17:29:22

标签: python screen pygame

所以在我决定提出这个问题之前,我做了一些研究,看看我的问题是什么,并且遇到了这个问题: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()**显示窗格(现在好一个),但是就像我之前说的那样,当我运行代码时,我得到一个黑屏。但由于某些原因,鼠标坐标跟踪器仍然有效。非常感谢任何帮助。

2 个答案:

答案 0 :(得分: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() - 使用一个屏幕到程序的末尾。

现在我可以看到你的窗格中有“你好”

答案 1 :(得分: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))

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