为什么我的盒子不会显示在pygame中?

时间:2014-01-05 14:55:37

标签: python screen pygame

所以我有三种不同的状态。 1.在屏幕上显示框2.在屏幕上显示键盘和3.在屏幕上显示不同的框。 (No.3不起作用)。

问题在于No.3的工作原理是当我进入页面时它会加载屏幕而没有任何错误。但是它不会在屏幕上加载我想要的盒子。

所以这是主屏幕: enter image description here

当我点击“Set Phrases”时,如果应该有一个显示“Hello my name is Dave”的方框,则会加载一个空白屏幕。显示在屏幕上: enter image description here

以下是我怀疑的代码部分:

def addPane(self, textToDisplay, keyboardFlag):      
    if (not keyboardFlag) and (self.NoOfPanes > 11):
        print("Limit Reached")            
    else:
        print(int(self.NoOfPanes))

        if keyboardFlag:
            self.Keys.append(Pane(textToDisplay, textToDisplay, self.keyboardPaneLocs[self.NoOfKeys], self.screen))
            self.NoOfKeys += 1
        elif not keyboardFlag:
            self.Panes.append(Pane(textToDisplay, textToDisplay, self.paneLocs[self.NoOfPanes], self.screen))
            self.NoOfPanes += 1
        else:
            self.sPhrases.append(Pane(textToDisplay, textToDisplay, self.sPhraseLoc[self.NoOfPhrases], self.screen))
            self.NoOfPhrases += 1

如果您需要,这是完整的代码:

import pygame
import sys
import csv


WHITE = (255,255,255)
BLACK = (0  ,0  ,0  )



MAIN_BUTTON = 2

KEYBOARD = True
WORDS = False
PHRASE = False



class Pane():

    def __init__(self, textToDisplay, textValue, coordinates, screen):

        self.textToDisplay = textToDisplay
        self.textValue = textValue
        self.coordinates = coordinates
        self.screen = screen
        self.font = pygame.font.SysFont('Palace Script MT', 25)



    def coordinates(self):
        return self.coordinates



    def text(self):
        return self.textValue



    def drawPane(self):
        pygame.draw.rect(self.screen, BLACK, self.coordinates, 2)
        self.screen.blit(self.font.render(self.textToDisplay, True, BLACK), self.coordinates)



class Terminal(Pane):

    def __init__(self, textToDisplay, coordinates, screen):

        Pane.__init__(self, textToDisplay, "", coordinates, screen)

        self.text = "" # keeps text displayed in top pane
        self.text_pos = (10,20) # left margin, top margin
        self.text_changed = True
        self.cursor_visible = True # changes after self.cursor_delay
        self.cursor_delay = 500 # This is 0.5 second
        self.cursor_next_change = pygame.time.get_ticks() + self.cursor_delay

    def draw(self):
        self.drawPane() 
        self.drawText()

    def drawText(self):
        #TODO: split self.text to lines

        # changes the cursor visibility
        if self.cursor_next_change <= pygame.time.get_ticks():
            self.cursor_next_change = pygame.time.get_ticks() + self.cursor_delay
            self.cursor_visible = not self.cursor_visible
            self.text_changed = True

        # renders new text only if text was changed (or cursor changed visibility)
        if self.text_changed:
            if self.cursor_visible:
                self.text_surface = self.font.render(self.text+"_", True, BLACK)
            else:
                self.text_surface = self.font.render(self.text, True, BLACK)

            self.text_rect = self.text_surface.get_rect(topleft = self.text_pos)
            self.text_changed = False

        self.screen.blit(self.text_surface, self.text_rect)

    def setText(self, text):
        self.text = text
        self.text_changed = True

    def addText(self, text):
        if text == "Backspace":
            if self.text: # text not empty
                self.text = self.text[:-1] # delete last char
        else:
            self.text += text
        self.text_changed = True

    def addWord(self, text):
        if text == "Backspace":
            if self.text: # text not empty
                self.text = self.text[:-1] # delete last char
        else:
            if self.text:
                self.text += " " # adds space before word
            self.text += text
        self.text_changed = True


class Application():

    NoOfPanes = 0
    NoOfKeys = 0
    NoOfPhrases = 0
    Panes = []
    Keys = []
    sPhrases = []

    paneLocs = [(583, 334, 300, 150), 
                (633, 150, 200, 125), 
                (633, 600, 200, 125), 
                (350, 360, 200, 100), 
                (925, 360, 200, 100), 
                (1000, 150, 150, 100), 
                (275, 150, 150, 100), 
                (275, 600, 150, 100), 
                (1000, 600, 150, 100), 
                (75, 350, 200, 100),
                (0, 668, 200, 100),
                (1200, 668, 200, 100)
                ]    

    keyboardPaneLocs = [(0, 100, 50, 50),
                        (75, 100, 50, 50),
                        (150, 100, 50, 50),
                        (225, 100, 50, 50),
                        (300, 100, 50, 50),
                        (375, 100, 50, 50),
                        (450, 100, 50, 50),
                        (525, 100, 50, 50),
                        (600, 100, 50, 50),
                        (675, 100, 50, 50),
                        (0, 200, 50, 50),
                        (75, 200, 50, 50),
                        (150, 200, 50, 50),
                        (225, 200, 50, 50),
                        (300, 200, 50, 50),
                        (375, 200, 50, 50),
                        (450, 200, 50, 50),
                        (525, 200, 50, 50),
                        (600, 200, 50, 50),
                        (675, 200, 50, 50),
                        (0, 300, 50, 50),
                        (75, 300, 50, 50),
                        (150, 300, 50, 50),
                        (225, 300, 50, 50),
                        (300, 300, 50, 50),
                        (375, 300, 50, 50),
                        (450, 300, 50, 50),
                        (525, 300, 50, 50),
                        (600, 300, 50, 50),
                        (0, 400, 50, 50),
                        (75, 400, 50, 50),
                        (150, 400, 50, 50),
                        (225, 400, 50, 50),
                        (300, 400, 50, 50),
                        (375, 400, 50, 50),
                        (450, 400, 50, 50),
                        (525, 400, 125, 50),
                        (0, 668, 200, 100),
                        (800, 150, 100, 50),
                        (1200, 668, 200, 100)
                        ]

    textPaneLoc = [(0, 0, 1000, 100)]

    sPhraseLoc = [(0, 100, 150, 50)]



    def __init__(self):

        pygame.init()
        pygame.display.set_caption('Box Test')

        self.font = pygame.font.SysFont('Arial', 25)
        self.screen = pygame.display.set_mode((1366,768), 0, 32)

        self.show_panes = True
        self.show_keyboard = False
        self.show_terminal = True
        self.show_phrases = False

        self.createPanes()
        self.createKeyboard()

        self.terminal = Terminal("", (0,0, 1000, 100), self.screen)
        self.createPhrases()

    def close(self):
        print ("pygame quit")
        pygame.quit()
        sys.exit()

    # Remember to import csv somewhere in your code
    def get_n_nouns(n):

        #Returns the n most common nouns

        with open("nounTree.txt") as in_file:
            reader = csv.reader(in_file)
            data = [[row[0], int(row[1])] for row in list(reader)]

        return sorted(data, key=lambda x: -x[1])[:n]



    #def createPanes(self):
        #words = self.get_n_nouns(10)
        #for word, count in words:
            #self.addPane("{}: {}".format(word, count), WORDS)


    def createPanes(self):
        self.addPane("1", WORDS)
        self.addPane("2", WORDS)
        self.addPane("3", WORDS)
        self.addPane("4", WORDS)
        self.addPane("5", WORDS)
        self.addPane("6", WORDS)
        self.addPane("7", WORDS)
        self.addPane("8", WORDS)
        self.addPane("9", WORDS)
        self.addPane("10", WORDS)
        self.addPane("Keyboard", WORDS)
        self.addPane("Set Phrases", WORDS)


    def createKeyboard(self):
        self.addPane("1", KEYBOARD)
        self.addPane("2", KEYBOARD)
        self.addPane("3", KEYBOARD)
        self.addPane("4", KEYBOARD)
        self.addPane("5", KEYBOARD)
        self.addPane("6", KEYBOARD)
        self.addPane("7", KEYBOARD)
        self.addPane("8", KEYBOARD)
        self.addPane("9", KEYBOARD)
        self.addPane("0", KEYBOARD)
        self.addPane("Q", KEYBOARD)
        self.addPane("W", KEYBOARD)
        self.addPane("E", KEYBOARD)
        self.addPane("R", KEYBOARD)
        self.addPane("T", KEYBOARD)
        self.addPane("Y", KEYBOARD)
        self.addPane("U", KEYBOARD)
        self.addPane("I", KEYBOARD)
        self.addPane("O", KEYBOARD)
        self.addPane("P", KEYBOARD)
        self.addPane("A", KEYBOARD)
        self.addPane("S", KEYBOARD)
        self.addPane("D", KEYBOARD)
        self.addPane("F", KEYBOARD)
        self.addPane("G", KEYBOARD)
        self.addPane("H", KEYBOARD)
        self.addPane("J", KEYBOARD)
        self.addPane("K", KEYBOARD)
        self.addPane("L", KEYBOARD)
        self.addPane("Z", KEYBOARD)
        self.addPane("X", KEYBOARD)
        self.addPane("C", KEYBOARD)
        self.addPane("V", KEYBOARD)
        self.addPane("B", KEYBOARD)
        self.addPane("N", KEYBOARD)
        self.addPane("M", KEYBOARD)
        self.addPane(" ", KEYBOARD)
        self.addPane("Boxes", KEYBOARD)
        self.addPane("Backspace", KEYBOARD)
        self.addPane("Set Phrases", KEYBOARD)

    def createPhrases(self):
        self.addPane("Hi my name is Dave.", PHRASE)


    def addPane(self, textToDisplay, keyboardFlag):      
        if (not keyboardFlag) and (self.NoOfPanes > 11):
            print("Limit Reached")            
        else:
            print(int(self.NoOfPanes))

            if keyboardFlag:
                self.Keys.append(Pane(textToDisplay, textToDisplay, self.keyboardPaneLocs[self.NoOfKeys], self.screen))
                self.NoOfKeys += 1
            elif not keyboardFlag:
                self.Panes.append(Pane(textToDisplay, textToDisplay, self.paneLocs[self.NoOfPanes], self.screen))
                self.NoOfPanes += 1
            else:
                self.sPhrases.append(Pane(textToDisplay, textToDisplay, self.sPhraseLoc[self.NoOfPhrases], self.screen))
                self.NoOfPhrases += 1


    def drawPanes(self):
        for Pane in self.Panes:
            Pane.drawPane()


    def drawKeyboard(self):
        for Key in self.Keys:
            Key.drawPane()

    def drawPhrases(self):
        for Phrase in self.sPhrases:
            Phrase.drawPane()


    def mousePosition(self, event):
        if event.type == MAIN_BUTTON:
            self.Pos = pygame.mouse.get_pos()
            return MAIN_BUTTON
        else:
            return False



    def mouseDown(self):
        posx,posy = pygame.mouse.get_pos()      
        textToReturn = "Nothing selected"
        if self.show_panes:
            for Pane in self.Panes:
                paneCoords = Pane.coordinates
                print(str(paneCoords[0]) + ":" + str(paneCoords[1]) + ":" + str(paneCoords[2]) + ":" + str(paneCoords[3]))
                if (paneCoords[0] <= posx <= paneCoords[0]+paneCoords[2]) and (paneCoords[1] <= posy <= paneCoords[1]+paneCoords[3]):
                    textToReturn = Pane.text()
        elif self.show_keyboard:
            for Pane in self.Keys:
                paneCoords = Pane.coordinates
                print(str(paneCoords[0]) + ":" + str(paneCoords[1]) + ":" + str(paneCoords[2]) + ":" + str(paneCoords[3]))
                if (paneCoords[0] <= posx <= paneCoords[0]+paneCoords[2]) and (paneCoords[1] <= posy <= paneCoords[1]+paneCoords[3]):            
                    textToReturn = Pane.text()
        elif self.show_phrases:
            for Pane in self.sPhrases:
                paneCoords = Pane.coordinates
                print(str(paneCoords[0]) + ":" + str(paneCoords[1]) + ":" + str(paneCoords[2]) + ":" + str(paneCoords[3]))
                if (paneCoords[0] <= posx <= paneCoords[0]+paneCoords[2]) and (paneCoords[1] <= posy <= paneCoords[1]+paneCoords[3]):            
                    textToReturn = Pane.text()

        return textToReturn



    def run(self):

        clock = pygame.time.Clock()

        RUNNING = True
        while RUNNING:

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    RUNNING = False
                    break

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.show_keyboard = not self.show_keyboard
                        self.show_panes = not self.show_panes

                if event.type == pygame.MOUSEBUTTONUP:
                    textSelected = self.mouseDown()
                    print(textSelected)
                    if textSelected == "Keyboard":
                        self.show_keyboard = True
                        self.show_panes = False
                        self.show_phrases = False
                    elif textSelected == "Boxes":
                        self.show_keyboard = False
                        self.show_panes = True
                        self.show_phrases = False
                    elif textSelected == "Set Phrases":
                        self.show_keyboard = False
                        self.show_panes = False
                        self.show_phrases = True
                    elif textSelected != "Nothing selected":
                        if self.show_keyboard:
                            self.terminal.addText(textSelected)
                        else:
                            self.terminal.addWord(textSelected)

            # --- draws ---

            self.screen.fill(WHITE)

            if self.show_panes:
                self.drawPanes()

            if self.show_keyboard:
                self.screen.blit(self.font.render("Spacebar", True, BLACK), (525, 400, 100, 50))
                self.drawKeyboard()

            if self.show_terminal:
                self.terminal.draw()

            if self.show_phrases:
                self.drawPhrases()

            pygame.display.update()

            # --- FPS ---

            clock.tick(60)

        self.close()    


Application().run()

提前感谢您的帮助。

©Burak Yuksel© - 哈哈哈认为这很有趣。 :)

1 个答案:

答案 0 :(得分:0)

我也在addPane中找到了问题...我不确定你想要在那里实现什么,但是窗格的数量是12,这就是它失败的原因。乍一看似乎有“off by one”错误。是

(self.NoOfPanes > 11)

肯定是对的?