2016-05-20 81 views
0

我正在做一個pygame程序,一本廚師掉下比薩餅的書,你必須用一個平底鍋抓住它們。廚師和鍋精靈只創建一次,而披薩精靈顯然不斷創建。我試圖讓分數變得更高,廚師開始更快地移動(只在x中移動)。我認爲我遇到了類屬性vs實例屬性的麻煩。我一直無法找到從廚師課獲得分數的方法,儘管我嘗試將分數作爲全局變量,甚至只是將它分配給一個虛擬的全局變量(這將允許我在主菜單中更改廚師的速度)更新方法)。另外,我嘗試訪問泛類中的廚師dx,因爲這是評分的地方。即使使用getattr方法,我也無法訪問它。將不勝感激任何建議在這一個。這是泛和廚師課程的代碼。我已經評論了我嘗試過但沒有工作的部分內容。改變pygame中一個不變的精靈對象的速度

from livewires import games, color 
import random 

games.init(screen_width = 640, screen_height = 480, fps = 50) 


class Pan(games.Sprite): 
    """ 
    A pan controlled by player to catch falling pizzas. 
    """ 
    image = games.load_image("pan.bmp") 

    def __init__(self): 
     """ Initialize Pan object and create Text object for score. """ 
     super(Pan, self).__init__(image = Pan.image, 
            x = games.mouse.x, 
            bottom = games.screen.height) 


     self.score = games.Text(value = 0, size = 25, color = color.black, 
           top = 5, right = games.screen.width - 10) 
     games.screen.add(self.score) 

    def update(self): 
     """ Move to mouse x position. """ 
     self.x = games.mouse.x 

     if self.left < 0: 
      self.left = 0 

     if self.right > games.screen.width: 
      self.right = games.screen.width 

     self.check_catch() 
    def check_catch(self): 
     """ Check if catch pizzas. """ 
     for pizza in self.overlapping_sprites: 
      self.score.value += 10 

#increase the speed of the pizza 

#   if self.score.value % 100 == 0: 
#    Pizza.speed += 0.1 
    #   print(Pizza.speed) 

#increase the speed of the chef 


    #   if self.score.value % 100 == 0: 
    #   print(Chef.dx) 
     #   print(x) 
     #  y = int(x)*2 
     #  print(y) 

      self.score.right = games.screen.width - 10 
      pizza.handle_caught() 




class Chef(games.Sprite): 
    """ 
    A chef which moves left and right, dropping pizzas. 
    """ 
    image = games.load_image("chef.bmp") 
    speed = 2 

    def __init__(self, y = 55, odds_change = 200): 
     """ Initialize the Chef object. """ 
     super(Chef, self).__init__(image = Chef.image, 
            x = games.screen.width/2, 
            y = y, 
            dx = Chef.speed) 

     self.odds_change = odds_change 
     self.time_til_drop = 0 

    def update(self): 
     #x = getattr(Pan,"score") 
     #print(x) 
     """ Determine if direction needs to be reversed. """ 
     if self.left < 0 or self.right > games.screen.width: 
      self.dx = -self.dx 
     elif random.randrange(self.odds_change) == 0: 
      self.dx = -self.dx  
     self.check_drop() 


    def check_drop(self): 
     """ Decrease countdown or drop pizza and reset countdown. """ 
     if self.time_til_drop > 0: 
      self.time_til_drop -= 1 
     else: 
      new_pizza = Pizza(x = self.x) 
      games.screen.add(new_pizza) 

      # set buffer to approx 30% of pizza height, regardless of pizza speed 
      self.time_til_drop = int(new_pizza.height * 1.3/Pizza.speed) + 1  


def main(): 
    """ Play the game. """ 
    wall_image = games.load_image("wall.jpg", transparent = False) 
    games.screen.background = wall_image 

    the_chef = Chef() 
    games.screen.add(the_chef) 

    the_pan = Pan() 
    games.screen.add(the_pan) 

    games.mouse.is_visible = False 

    games.screen.event_grab = True 
    games.screen.mainloop() 

# start it up! 
main() 

回答

0

由於廚師速度是一個類變量,您需要添加類名稱來訪問它,如:Chef.speed。比薩班在哪裏?我不知道Livewire,所以無法解釋爲什麼你不能訪問分數,但是肯定你可以以某種方式將它設置爲數字並以這種方式使用它?

0

謝謝你的時間。我能夠通過在Pan類中創建一個屬性標誌來檢查分數,然後在Chef的更新方法中訪問該屬性標誌,然後能夠更改dx來解決此問題。 Chef.speed只是最初的廚師速度,所以改變它並不會更新廚師的dx。