0

2 つの異なるペインを呼び出すと、両方が同じ画面に表示されない理由がわかりません。それはどちらかです。誰かがなぜこれが当てはまるのかを教えて助けてください。また、問題を修正して両方のペインを画面に表示できるようにする方法を教えてください。

完全なコードは次のとおりです。

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.display.set_mode()

あなたの質問に対する私の回答を参照してくださいPyGameのディスプレイが黒い画面である理由に混乱しています


完全な作業コード。

CPU の使用量が少ないため、イベント ループをクラスに移動します。

また、12 FPS を作成するために追加clockします。アニメーションを作成する場合は、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 に答える
1

Pygame は本質的に SDL のラッパーであり、複数のディスプレイとウィンドウ ( 「ペイン」と呼ばれるもの) のサポートがバージョン 2.0 (2013 年 8 月に開始) で追加されました。残念ながら、私の知る限り、Pygame の最後のバージョンは 1.9.1 で、2009 年 8 月にリリースされたので、最新の機能はサポートされていません。

したがって、あなたがやろうとしていることは Pygame では不可能です。ただし、積極的に開発されており、最新の SDL が追加されているように見えるPySDL2を確認する必要があります。

于 2013-11-13T18:34:25.047 に答える