0

(这个问题的措辞不是很好,所以如果你认为你可以改写它更好,那么请这样做。)我基本上在 PyGame 屏幕上显示了十个几乎相同的窗格。但是代码太长了,所以我想知道是否有更好的方法来做我所做的事情。

def addPane(self, textToDisplay):
    #right now it is displaying them all change so that it checks how many
    #panes are on through (numberOfPanes = 0) 10 is limit display no more
    #than ten.
    paneOne = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneTwo = pygame.draw.rect(self.screen, (black), (0, 0, 200, 100), 2)
    paneThree = pygame.draw.rect(self.screen, (black), (600, 400, 200, 100), 2)
    paneFour = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneFive = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneSix = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneSeven = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneEight = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneNine = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    paneTen = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
    if self.NoOfPanes > 10:
        print("Limit reached")
    else:
        paneOne
        self.NoOfPanes = self.NoOfPanes + 1
        if self.NoOfPanes > 10:
            print("Limit reached")
        else:
            paneTwo
            self.screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
            self.NoOfPanes = self.NoOfPanes + 1
            if self.NoOfPanes > 10:
                print("Limit reached")
            else:
                paneThree
                self.NoOfPanes = self.NoOfPanes + 1
                if self.NoOfPanes > 10:
                    print("Limit reached")
                else:
                    paneThree
                    self.NoOfPanes = self.NoOfPanes + 1
                    if self.NoOfPanes > 10:
                        print("Limit reached")
                    else:
                        paneFour
                        self.NoOfPanes = self.NoOfPanes + 1
                        if self.NoOfPanes > 10:
                            print("Limit reached")
                        else:
                            paneFive
                            self.NoOfPanes = self.NoOfPanes + 1
                            if self.NoOfPanes > 10:
                                print("Limit reached")
                            else:
                                paneSix
                                self.NoOfPanes = self.NoOfPanes + 1
                                if self.NoOfPanes > 10:
                                    print("Limit reached")
                                else:
                                    paneSeven
                                    self.NoOfPanes = self.NoOfPanes + 1
                                    if self.NoOfPanes > 10:
                                        print("Limit reached")
                                    else:
                                        paneEight
                                        self.NoOfPanes = self.NoOfPanes + 1
                                        if self.NoOfPanes > 10:
                                            print("Limit reached")
                                        else:
                                            paneNine
                                            self.NoOfPanes = self.NoOfPanes + 1
                                            if self.NoOfPanes > 10:
                                                print("Limit reached")
                                            else:
                                                paneTen
                                                self.NoOfPanes = self.NoOfPanes + 1
    pygame.display.update()

它看起来像什么: 运行时的代码

函数中写的文字是我想要实现的。我已经这样做了,但正如我之前所说,它太长了。这是实现我已经拥有的结果的最佳方法还是有更好的方法?

(稍后我将更改坐标,以便所有十个窗格都可见,但是出于测试目的,我已经这样做了。)

4

2 回答 2

3

我会做这样的事情。

def addPane(self, text_to_display):
  pane_locs = [(175, 75, 200, 100), 
               (0, 0, 200, 100),
               (600, 400, 200, 100),
               (175, 75, 200, 100),
               (175, 75, 200, 100),
               (175, 75, 200, 100),
               (175, 75, 200, 100),
               (175, 75, 200, 100),
               (175, 75, 200, 100),
               (175, 75, 200, 100),
              ]
  for i, pane_loc in enumerate(pane_locs):
    if self.num_panes > 10:
       print("limit reached")
       break
    if i == 1:
      self.screen.blit(self.font.render(text_to_display, True, (black)), (250, 115))
    pygame.draw.rect(self.screen, (black), pane_loc, 2)
    self.num_panes += 1
    pygame.display.update()

我们正在遍历窗格位置,当我们在正确的窗格时添加文本,当我们有足够的窗格时停止。

于 2013-10-17T17:43:55.493 回答
1

也许是这样的?

panes = []
def addPane(self, textToDisplay):
    #starting coordinate location for your frames
    x = 0
    y = 0
    for i in range(10):
        panes.append(pygame.draw.rect(self.screen, (black), (x, y, 200, 100), 2))
        panes[-1]
        #increment coordiantes by desired ammounts
        x += 200
        y += 100
        if len(panes) > 10:
            print("Limit reached")
            break
    pygame.display.update()
于 2013-10-17T17:43:44.267 回答