0

我很困惑为什么当我调用两个不同的窗格时它们不会同时显示在同一个屏幕上。它是一个或另一个。有人可以告诉我为什么会这样,还可以告诉我如何解决问题以便能够在屏幕上显示两个窗格。

这是完整的代码:

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)
        self.Screen.fill((white))
        pygame.display.update()

    def drawPane(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (75, 135))
        pygame.draw.rect(self.Screen, (black), (0, 100, 200, 100), 2)

    def drawPane1(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.Screen, (black), (200, 300, 200, 100), 2)

    def drawPane2(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.Screen, (black), (400, 500, 200, 100), 2)

    def drawPane3(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.Screen, (black), (600, 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), (800, 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()
        pygame.display.set_caption('Box Test')
        numberOfPanes = 0
        self.NoOfPanes = numberOfPanes

    def addPane(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane(textToDisplay)

    def addPane1(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane1(textToDisplay)

    def addPane2(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane2(textToDisplay)

    def addPane3(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane3(textToDisplay)

    def addPane4(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane4(textToDisplay)

    def addPane5(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane5(textToDisplay)

    def addPane6(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane6(textToDisplay)

    def addPane7(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane7(textToDisplay)

    def addPane8(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane8(textToDisplay)

    def addPane9(self, textToDisplay):
        myPane = Pane()
        myPane.drawPane9(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.addPane1("b")
    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()
                if (posx >=175 or posx <=375) and (posy >=75 or posy <= 175):
                    print("bob")
                else:
                    print("error")


        for event in pygame.event.get():        
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

任何帮助深表感谢。谢谢。

4

2 回答 2

1

Pygame is essentially a wrapper for the SDL, and support for multiple displays and windows (what you call "Panes") was added in version 2.0 (launched aug 2013). Unfortunately, as far as I know the last version of Pygame was 1.9.1, and it was launched on august 2009 so there's no support for the newest features.

Therefore, I'm afraid what you're trying to do is not possible using Pygame. However you should check PySDL2, which seems to be actively developed and up to date with the latest additions of SDL.

于 2013-11-13T18:34:25.047 回答
1

您只需使用pygame.display.set_mode()一次。

请参阅我对您的问题的回答对PyGame 显示为黑屏的原因感到困惑


完整的工作代码。

我将事件循环移到类中,因为它使用较少的 CPU。

我还添加clock了 12 FPS - 如果您要制作动画,您可以将其更改为 25 FPS 或更高。

我将其他东西移到类中,并将其名称更改为,Application因为它是代码的组织方式

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, screen):
        self.Screen = pygame.display.set_mode((1000,600), 0, 32)
        self.Screen = screen #pygame.display.set_mode((1000,600), 0, 32)
        self.font = pygame.font.SysFont('Arial', 25)
        self.Screen.fill((white))
        pygame.display.update()

    def drawPane(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (75, 135))
        pygame.draw.rect(self.Screen, (black), (0, 100, 200, 100), 2)

    def drawPane1(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.Screen, (black), (200, 300, 200, 100), 2)

    def drawPane2(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.Screen, (black), (400, 500, 200, 100), 2)

    def drawPane3(self, textToDisplay):
        self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
        pygame.draw.rect(self.Screen, (black), (600, 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), (800, 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 Application():

    def __init__(self):
        pygame.init()
        pygame.display.set_caption('Box Test')
        self.Screen = pygame.display.set_mode((1000,600), 0, 32)

        numberOfPanes = 0
        self.NoOfPanes = numberOfPanes

        self.addPane("hello")
        self.addPane1("b")
        self.mousePosition()
        pygame.display.update()


    def addPane(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane(textToDisplay)

    def addPane1(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane1(textToDisplay)

    def addPane2(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane2(textToDisplay)

    def addPane3(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane3(textToDisplay)

    def addPane4(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane4(textToDisplay)

    def addPane5(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane5(textToDisplay)

    def addPane6(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane6(textToDisplay)

    def addPane7(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane7(textToDisplay)

    def addPane8(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane8(textToDisplay)

    def addPane9(self, textToDisplay):
        myPane = Pane(self.Screen )
        myPane.drawPane9(textToDisplay)

    def clearScreen(self):
        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

    def run(self):
        clock = pygame.time.Clock()

        while True:
            # --- events ---
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                if event.type == pygame.MOUSEBUTTONUP:
                    posx,posy = pygame.mouse.get_pos()
                    if (175 <= posx <= 375) and (75 <= posy <= 175):
                        print("bob")
                    else:
                        print("error")

            # --- FPS ---
            clock.tick(12) # 12 FPS (Frames Per Second)

if __name__ == '__main__':
    Application().run()

编辑:

您应该以这种方式测试鼠标位置以使其正常工作:

if (175 <= posx <= 375) and (75 <= posy <= 175):
    print("bob")
else:
    print("error")
于 2013-11-13T18:56:35.723 回答