我最近开始使用python编程,到目前为止我喜欢它。我以前用c#和java编程,这可能导致我的问题。在c#中,如果你有一个公共变量,它将在每个方法中改变。很抱歉这个错误的解释,但是可视化代码会更容易。
这段代码是我希望在c#form中以python发生的一个例子。代码实际上并不起作用,因为它只是一个例子。
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
它表示变量在给定值之前被引用,但我在构造函数中给它一个值。
答案 0 :(得分: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?
旁注:其中一个原因( btw )是在Python中我们希望更清楚地了解如何编写代码。请参阅Zen of Python
的第二行Beautiful is better than ugly. Explicit is better than implicit. ...
更新:它也没有价值(请阅读文档),没有公共与私人成员使用Python的概念类型系统。我们定义&#34;私人&#34;按惯例,成员通常只带有带前缀的单下划线;例如:def _foo(self):
作为私人/ inernal方法。