2014-03-03 50 views
0

我使用Pygame和一個按鈕模塊來複制簡單電梯程序的代碼。我遇到的問題是不顯示某個按鈕(由按鈕模塊創建),基於currentFloor的內容。如果陳述適用於其中一個,但不適用於其他

if currentFloor != maxFloor:#Checks if the currentFloor is not equal to the maxFloor 
      self.Button3.create_button(self.setDisplay, colcyan, 550, 100, 200, 100,0,"Up",colblack)#Button to add one to currentLevel 

此代碼提供的是按鈕上去。儘管當我嘗試使用向下按鈕的類似代碼行重新創建它時,它會產生錯誤。

elif currentFloor != startFloor:#Checks if the currentFloor is not equal to the startFloor 
      self.Button1.create_button(self.setDisplay, colred, 550, 400, 200, 100,0,"Down",colblack)#Button to subtract one to currentLevel 

錯誤產生:

Traceback (most recent call last): 
    File "G:\Python APCSP\Elevator\Example.py", line 72, in <module> 
    obj = Elevator() 
    File "G:\Python APCSP\Elevator\Example.py", line 24, in __init__ 
    self.runGame() 
    File "G:\Python APCSP\Elevator\Example.py", line 66, in runGame 
    if self.Button1.pressed(pygame.mouse.get_pos()): 
    File "G:\Python APCSP\Elevator\Buttons.py", line 29, in pressed 
    if mouse[0] > self.rect.topleft[0]: 
AttributeError: Button instance has no attribute 'rect' 

我不確定爲什麼if語句非常相似,第一個將提供這樣的錯誤。

完整的源代碼將在下面提供。

電梯計劃源代碼:

import pygame, Buttons, sys 
from pygame.locals import * 

#Color Options 
colwhite = (255,255,255) 
colblack = (0,0,0) 
colgray = (33,33,33) 
colblue = (0,61,103) 
colred = (103,0,9) 
colyellow = (255,229,9) 
colgreen = (0,103,42) 
colcyan = (0,118,118) 
colpurple = (103,0,103) 

#Initialize pygame 
pygame.init() 

currentFloor = 0 
maxFloor = 5 
startFloor = 0 

class Elevator: 
    def __init__(self): 
     self.runGame() 

    #Create a display 
    def display(self): 
     width = 800 
     height = 600 
     self.setDisplay = pygame.display.set_mode((width,height),0,32) 
     pygame.display.set_caption("Elevator Program") 

    #Update the display and show the button 
    def update_display(self): 
     global currentFloor 
     self.setDisplay.fill(colblue) 
     #Parameters:      surface, color, x, y, length, height, width, text,  text_color 
     self.Button2.create_button(self.setDisplay, colblue, 525, 225, 250, 150,0,"Current Floor: "+str(currentFloor),colwhite)#Not a used button, just to display currentFloor 
     if currentFloor != maxFloor:#Checks if the currentFloor is not equal to the maxFloor 
      self.Button3.create_button(self.setDisplay, colcyan, 550, 100, 200, 100,0,"Up",colblack)#Button to add one to currentLevel 
     elif currentFloor != startFloor:#Checks if the currentFloor is not equal to the startFloor 
      self.Button1.create_button(self.setDisplay, colred, 550, 400, 200, 100,0,"Down",colblack)#Button to subtract one to currentLevel 
     pygame.display.flip() 


    #Run the loop 
    def runGame(self): 
     global currentFloor 
     self.Button2 = Buttons.Button() 
     self.Button1 = Buttons.Button() 
     self.Button3 = Buttons.Button() 
     self.display() 
     while True: 
      self.update_display() 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        sys.exit() 
       elif event.type == MOUSEBUTTONDOWN: 
        if currentFloor != maxFloor: 
         if self.Button3.pressed(pygame.mouse.get_pos()): 
          print "Going Up" 
          currentFloor += 1 
          print currentFloor 
        if currentFloor != startFloor: 
         if self.Button1.pressed(pygame.mouse.get_pos()): 
          print "Going Down" 
          currentFloor -= 1 
          print currentFloor 

if __name__ == '__main__': 
    obj = Elevator() 

按鈕模塊源代碼:

import pygame 
from pygame.locals import * 
pygame.init() 
class Button: 
    def create_button(self, surface, color, x, y, length, height, width, text, text_color): 
     surface = self.draw_button(surface, color, length, height, x, y, width) 
     surface = self.write_text(surface, text, text_color, length, height, x, y) 
     self.rect = pygame.Rect(x,y, length, height) 
     return surface 

    def write_text(self, surface, text, text_color, length, height, x, y): 
     font_size = int(length//len(text)) 
     myFont = pygame.font.SysFont("Calibri", font_size) 
     myText = myFont.render(text, 1, text_color) 
     surface.blit(myText, ((x+length/2) - myText.get_width()/2, (y+height/2) - myText.get_height()/2)) 
     return surface 

    def draw_button(self, surface, color, length, height, x, y, width):   
     for i in range(1,10): 
      s = pygame.Surface((length,height)) 
      s.fill(color) 
      pygame.draw.rect(s, color, (x-i,y-i,length+i,height+i), width) 
      surface.blit(s, (x,y)) 

     pygame.draw.rect(surface, (190,190,190), (x,y,length,height), 1) 
     return surface 

    def pressed(self, mouse): 
     if mouse[0] > self.rect.topleft[0]: 
      if mouse[1] > self.rect.topleft[1]: 
       if mouse[0] < self.rect.bottomright[0]: 
        if mouse[1] < self.rect.bottomright[1]: 
         return True 
        else: return False 
       else: return False 
      else: return False 
     else: return False 

回答

4

,當你調用Button.create_button()rect屬性僅初始化。

但是,運行在runGame()代碼,希望此屬性是存在:你調用按鈕1 Button.create_button()或3

你需要確保的按鈕嘗試之前正確初始化之前調用Button.pressed()使用或查詢它們。或者您的pressed()方法需要檢查按鈕是否已經初始化,如果不是,則返回False。

0

每次更新到currentFloor後調用update_display()函數。

按一次up按鈕按下currentFloor的值不再是0

其結果第二if條件(在runGame的while循環if currentFloor != startFloor)也將被檢查,但是作爲update_diplay()不叫在之間,以便爲down沒有按鈕被初始化。

也在update_display()功能使條件爲if因爲您需要檢查兩個buttons無論如何。
這是你的while循環應該怎麼樣子:

self.update_display() 
while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 
     elif event.type == MOUSEBUTTONDOWN: 
      if currentFloor != maxFloor: 
       if self.Button3.pressed(pygame.mouse.get_pos()): 
        print "Going Up" 
        currentFloor += 1 
        print currentFloor 
        self.update_display() 
      if currentFloor != startFloor: 
       if self.Button1.pressed(pygame.mouse.get_pos()): 
        print "Going Down" 
        currentFloor -= 1 
        print currentFloor 
        self.update_display() 
相關問題