1

I wanted to create a x-y coordinate system even though this is supposed to be a text RPG as to keep track of where everything is. So, I was experimenting on making a function and test for that function that would let the character move on a x-y grid, however, no matter what I try, I cannot make it work. Here is the code: class Player:

    def movement(charactor_movement):
        proceed = 0
        if charactor_movement == "left":
            character.position_x = character.position_x - 1
            proceed = 1
        elif charactor_movement == "right":
            character.position_x = character.position_x + 1
            proceed = 1
        elif charactor_movement == "forward":
            character.position_y = character.position_y + 1
            proceed = 1
        elif charactor_movement == "backward" or charactor_movement == "back":
            character.position_y = character.position_y - 1
            proceed = 1
charactor = Player()
charactor.position_x = 0
charactor.position_y = 0
proceed = 0
while proceed == 0:
    print "You are at",
    print charactor.position_x,
    print"x and",
    print charactor.position_y,
    print"y."
    global charactor_movement
    charactor_movement = raw_input("Where are you going?")
    charactor.movement()

At this point, it does what it is supposed to do up to changing the coordinates, as it prints "You are at 0 x and 0 y" and "Where are you going?" no matter what I type. I have tried adding an else to the function which it defaulted to no matter what I typed and gave me "Sorry, I cannot understand you." Any comments on fixing or generally improving the code would be appreciated.(Note: For the testing I purposely did not add a way to exit. The class is what i need fixed.)

miradulo
  • 28,857
  • 6
  • 80
  • 93

1 Answers1

0

You are getting the same coordinates with each iteration because your values within your while loop are not changing. Incrementing character.position_x within movement will never change the value of character.position_x within your while loop, as it is outside your function's scope. You have to use the global keyword within your movement function for each variable you are changing should you want your current logic to remain the same. Additionally, why not just pass charactor_movement as a parameter to your movement function, as opposed to using global as you currently are doing.


A minimal example:

Consider the following:

def somefunct(x):
    mycode = x
    
mycode = 'no codez'
while True:
    print mycode
    codez = raw_input('gimme teh codez: ')
    somefunct(codez)

which outputs

>>>[evaluate untitled-1.py]
no codez
gimme teh codez: codez!
no codez

Declaring mycode as global in the function places it in the scope of the while loop when assigned, thus

def somefunct(x):
    global mycode  #make variable global here
    mycode = x
        
mycode = 'no codez'
while True:
    print mycode
    codez = raw_input('gimme teh codez: ')
    somefunct(codez)

results in the output

>>>[evaluate untitled-1.py]
no codez
gimme teh codez: codez!
codez!
Community
  • 1
  • 1
miradulo
  • 28,857
  • 6
  • 80
  • 93