1

Ok, so I'm making a little textual maze game for a school project. I define the world obstacles with lists of x positions and y positions, and the positions of the values in the lists can be used to match the two values and create a coördinate. This could probably be more efficiënt but at least it works. Now, I keep track of the player and the enemy by saving their X and Y values in 2 variables. This also works fine.

However, to make movement work I use a function that first checks if there isn't any terrain where the player wants to go. That's all fine and dandy, but the problem is that I don't know how to change the global variables "PlayerX" and "PlayerY" from a function.

I've already tried the "global" keyword but it made writing the code a bit harder since I need to be aware of it whenever I write new stuff with the coördinates. I also read other topics that I thought were tackling the same problem but none seemed to really fit the problem I'm having (or the more likely scenario: I couldn't understand it well enough to the extent that I could apply it to my problem appropriately)

def UpMove():
    reference = []
    listPosition = 0
    movePossible = False

    for x in worldObsY:
        if x == playerY + 1:
            reference.append(listPosition)

        listPosition += 1

    for x in reference:
        if worldObsX[x] == playerX:
            movePossible = False
        else:
            movePossible = True

    if movePossible == True:
      print ("You moved")
      return playerY + 1
    else:
      print ("There's terrain there. Try a different way")
      return playerY

It would be very convenient to just have the X and Y values editted globally. I was thinking of trying to work with putting the variables inside of my Movement loop (which plays untill the player is dead or gets out of the maze), but this seems like a problem that has a very easy solution that I'm just not seeing so before I go trying out all sorts of dumb stuff I figured I'd just put a question up here...

1 Answers1

0

Well, just don't use global variables. You could pass them in like this:

def otherFunction():
    playerX = 0
    playerX = 0
    UpMove(playerX, playerY)
    # other stuff

def UpMove(playerX, playerY):
    reference = []
    # your other stuff
quamrana
  • 37,849
  • 12
  • 53
  • 71