我最近开始用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


它说在给变量赋值之前先对其进行引用,但是我在构造函数中给了它一个值。

最佳答案

您已经忘记(或尚未阅读)方法中对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的第二行


  Zen of Python

Beautiful is better than ugly.

Explicit is better than implicit.

...



更新:使用Python的类型系统没有公共或私有成员的概念也毫无意义(请阅读文档)。我们通常只用前缀下划线来定义“私有”成员。例如:def _foo(self):作为私有/内部方法。

关于c# - 在分配值之前引用的Python全局变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30230120/

10-14 23:59
查看更多