2015-05-14 74 views
2

我最近開始用python編程,到目前爲止我都喜歡它。我以前編程在C#和Java,這可能是我的問題。在c#中,如果你有一個公共變量,它會在每個方法中改變。對不好的解釋,但它會更容易可視化的代碼。在分配值之前引用了Python全局變量

這段代碼是我想在c#窗體中發生的一個例子。代碼實際上並不工作,因爲它只是一個例子。

class Player 
{ 
    public var player; //create the variable 
    public var playerRectangle; 
    public var playerMovement; 

    public Player() 
    { 
     player = pygame.image.load("player.png"); //set the variables value 
     playerRectangle = player.get_rect(); 

     playerMovement = new int[0,0]; 
    } 

    public void Update() 
    { 
     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_w: 
       playerMovement[1] = -2 //use the variables value 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_s: 
       playerMovement[1] = 2 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_a: 
       playerMovement[0] = -2 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_d: 
       playerMovement[0] = 2 

      if event.type == pygame.KEYUP and event.key == pygame.K_w: 
       playerMovement[1] = 0 
      if event.type == pygame.KEYUP and event.key == pygame.K_s: 
       playerMovement[1] = 0 
      if event.type == pygame.KEYUP and event.key == pygame.K_a: 
       playerMovement[0] = 0 
      if event.type == pygame.KEYUP and event.key == pygame.K_d: 
       playerMovement[0] = 0 

     playerRectangle = playerRectangle.move(playerMovement) 
    } 
} 

實際的Python代碼是:

import pygame 

class Player: 
    player = None #create the variable 
    playerRectangle = None 
    playerMovement = None 

    def __init__(): 
     global player 
     player = pygame.image.load("player.png") #set the variables value 
     global playerRectangle 
     playerRectangle = player.get_rect() 

     global playerMovement 
     playerMovement = [0,0] 

    def update(): 
     for event in pygame.event.get(): 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_w: 
       playerMovement[1] = -2 #use the variables !!ERROR!! 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_s: 
       playerMovement[1] = 2 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_a: 
       playerMovement[0] = -2 
      if event.type == pygame.KEYDOWN and event.key == pygame.K_d: 
       playerMovement[0] = 2 

      if event.type == pygame.KEYUP and event.key == pygame.K_w: 
       playerMovement[1] = 0 
      if event.type == pygame.KEYUP and event.key == pygame.K_s: 
       playerMovement[1] = 0 
      if event.type == pygame.KEYUP and event.key == pygame.K_a: 
       playerMovement[0] = 0 
      if event.type == pygame.KEYUP and event.key == pygame.K_d: 
       playerMovement[0] = 0 

     playerRectangle = playerRectangle.move(playerMovement) 

    def returnTexture(): 
     return player 

    def returnRectangle(): 
     return playerRectangle 

它說,它被賦予了值之前變量被引用,但我把它在構造函數的值。

回答

3

您忘記了(或沒有讀取)在您的方法中使用了self

您需要將self放在方法定義 中,並使用self.<attribute>來引用實例的屬性。

實施例:

class Player(object): 

    def __init__(self, name): 
     self.name = name 

    def update_name(self, name): 
     self.name = name 

bob = Player("Bob") 
print bob.name # Bob 

bob.update_name("Bob Jones") 
print bob.name # Bob Jones 

注:即在在Python類方法定義使用self是按照慣例僅僅;你可以使用任何你喜歡的名字,但我們廣泛且典型地(按慣例)使用self

參見:Classes

另請參見:What is the purpose of self?

側面說明:其中一個方面的原因(順便說一句)是在Python我們想更明確的瞭解我們如何編寫代碼。見的Zen of Python

二號線的Zen of Python

Beautiful is better than ugly. 

Explicit is better than implicit. 

... 

更新:這也是一文不值(請閱讀文檔),沒有公共與私有觀念Python類型系統的成員。我們通常只使用前綴單下劃線來定義「私人」成員;例如:def _foo(self):作爲私人/國際方法。

相關問題