在Python中我的第一個「大」項目,我試圖讓simon says遊戲的克隆與pygame的在python中動態創建變量名?
()方法需要我來檢測實例是什麼顏色(self.color) 和改變顏色的閃爍基於此。
那麼紅到鮮明,藍到brightblue等問題是,我現在的代碼是非常漫長和醜陋的,我知道必須有一個更好的方式來做到這一點。
這是我的醜陋的代碼:
def blink(self):
if self.color == red:
self.color = brightred
elif self.color == blue:
self.color = brightblue
elif self.color == green:
self.color = brightgreen
所定義的顏色:
black = (0,0,0)
white = (255,255,255)
red = (150,0,0)
green = (0,150,0)
blue = (0,0,150)
yellow = (150,150,0)
brightred = (255,0,0)
brightgreen = (0,255,0)
brightblue = (0,0,255)
brightyellow = (255,255,0)
最後包含此方法的類:
class rec:
def __init__(self,left,top,size,color):
self.left = left
self.top = top
self.size = size
self.color = color
self.rect = pygame.Rect(self.left,self.top,self.size,self.size)
pygame.draw.rect(d,self.color,self.rect)
pygame.display.update()
def blink(self):
if self.color == red:
self.color = brightred
elif self.color == blue:
self.color = brightblue
elif self.color == green:
self.color = brightgreen
elif self.color == yellow:
self.color = brightyellow
pygame.draw.rect(d,self.color,self.rect)
pygame.display.update() # Not finished
對不起,我不明白的問題。這裏有什麼問題?你的目標是什麼? –