2

I recently started to program in python, and I love it so far. I previously programmed in c# and java, which is probably causing my problem. In c#, if you have a public variable, it will change in each method. Sorry for the bad explanation, but it will be easier to visualize the code.

This code is an example of what I want to happen in python in c# form. The code does not actually work because it's only an example.

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)
    }
} 

The actual python code is:

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

It says that the variable is referenced before it was given a value, but I gave it a value in the constructor.

William Lew
  • 485
  • 6
  • 17

1 Answers1

3

You have forgotten (or have not read) the use of self in your methods.

You need to put self in the method definitions and use self.<attribute> to reference attributes on the instance.

Example:

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

Note: That the use of self in method definitions of classes in Python is by convention only; you can use whatever name you like but we widely and typically (by convention) use self.

See: Classes

See Also: What is the purpose of self?

Side Note: One of the reasons for this (btw) is that in Python we like to be more explicit about how we write code. See the 2nd line of the Zen of Python

The Zen of Python

Beautiful is better than ugly.

Explicit is better than implicit.

...

Update: It's also worth nothing (please read the documentation) that there is no notion of public vs. private members with Python's type system. We define "private" members by convention only usually with a prefixed single underscore; e.g: def _foo(self): as a private/inernal method.

Community
  • 1
  • 1
James Mills
  • 18,669
  • 3
  • 49
  • 62