(这个问题的措辞不是很好,所以如果你认为你可以改写它更好,那么请这样做。)我基本上在 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()
它看起来像什么:
函数中写的文字是我想要实现的。我已经这样做了,但正如我之前所说,它太长了。这是实现我已经拥有的结果的最佳方法还是有更好的方法?
(稍后我将更改坐标,以便所有十个窗格都可见,但是出于测试目的,我已经这样做了。)