困惑为什么PyGame显示屏为黑屏 [英] Confused at why PyGame display's a black screen

查看:1431
本文介绍了困惑为什么PyGame显示屏为黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在我决定问这个问题之前,我做了一些研究以了解我的问题,并遇到了这个问题:

So before I decided to ask this question I did a little research to see what my problem was and came across this: Code Only Produces Black Screen In Pygame Window However this is not all that helpful as I haven't got the same issue as this person. I for sure know that my PyGame is not outdated or anything like that as I had a previous version of my code working.

每次运行代码时,我都会黑屏.

Every time I run the code I end up getting a black screen.

这是我的代码(对不起,对不起):

Here is my code (A bit long sorry):

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 Screen()****class Pane()**中的窗格(现在是一个窗格),但是就像我之前说的那样,当我运行代码时,我得到了黑屏.但是由于某种原因,鼠标坐标跟踪器仍然可以工作.任何帮助将不胜感激.

So I am basically trying to display the panes (well one for now) from **class Pane()** in **class Screen()** however like I said before when I run the code I get a black screen. But for some reason the mouse co-ordinate tracker still works. Any help is greatly appreciated.

推荐答案

问题是您的代码组织.

Problem is your code organization.

您有树时间pygame.display.set_mode().每次调用pygame.display.set_mode()时,都将销毁先前的screen,创建新的screen,并且新屏幕始终为黑色.

You have tree times pygame.display.set_mode(). Every time you call pygame.display.set_mode() you destroy previous screen, create new screen and new screen is always black.

您只需创建一次screen,然后将其作为参数发送给其他类.

You should create screen only once and send it to other class as parameter.

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 

并从clear()功能中删除pygame.display.set_mode()-使用一个屏幕显示程序的末尾.

And remove pygame.display.set_mode() from clear() function - use one screen to the end of the program.

现在我可以看到带有你好"的窗格了

Now I can see your pane with "hello"

这篇关于困惑为什么PyGame显示屏为黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆