My python function won't return or update the

2020-04-14 01:59发布

问题:

def getMove(win,playerX,playerY):

    #Define variables.
    movePos = 75
    moveNeg = -75
    running = 1

    #Run while loop constantly to update mouse's coordinates.
    while(running):

        mouseCoord = win.getMouse()
        mouseX = mouseCoord.getX()
        mouseY = mouseCoord.getY()
        print "Mouse X = ", mouseX
        print "Mouse Y = ", mouseY

        if mouseX >= playerX:
            playerX = movePos + playerX
            running = 0
        elif mouseX <= playerX:
            playerX = moveNeg + playerX           
            running = 0
        elif mouseY >= playerY:
            playerY = movePos + playerY            
            running = 0
        elif mouseY <= playerY:
            playerY = moveNeg + playerY            
            running = 0
    return playerX,playerY

def main():

    #Create game window.
    win = GraphWin("Python Game", 500, 500)
    drawBoard(win)

    #Define variables.
    playerX = 75
    playerY = 125
    keyX = 325
    keyY = 375
    running = 1


    #Create Key and Player objects, draw the key, but don't draw the player yet.
    key = Text(Point(keyX,keyY),"KEY")
    key.draw(win)

    while(running):
        print "player X = ", playerX
        print "Player Y = ", playerY
        drawBoard(win)
        getMove(win,playerX,playerY)
        player = Circle(Point(playerX,playerY),22)
        player.setFill('yellow')
        player.draw(win)
main()

I am using a graphics library to create a game. My player and key are drawn in the correct places. However, when calling the getMove function, my playerX and playerY do not update. I have added debug print statements to find their values while running the game and it is always 75 and 125. Help!

回答1:

In python, integers are immutable - when you assign a new integer value to a variable, you are just making the variable point to a new integer, not changing what the old integer it pointed to's value was.

(An example of a mutable object in python is the list, which you can modify and all variables pointing to that list will notice the change - since the LIST has changed.)

Similarly, when you pass a variable into a method in python and then alter what the variable points to in that method, you do not alter what the variable points to outside of that method because it is a new variable.

To fix this, assigned the returned playerX,playerY to your variables outside the method:

playerX, playerY = getMove(win,playerX,playerY)



回答2:

You should explicitly reassign these variables when you call getMove():

playerX, playerY = getMove(win, playerX, playerY)

Hope this helps!



回答3:

As has been mentioned, the solution is that you're not using the returned values to update your playerX, playerY which can be fixed with the mentioned

playerX, playerY = getMove(win,playerX,playerY)

What I want to address is the logic in your if statements. The way you've constructed your if statements will lead to only an X OR Y being updated, not both. For example, if mouseX, mouseY were both greater than playerX, playerY repectively, you'd get to the first line of your if statement and it would be evaluated as True and update playerX accordingly, BUT because the first statement has been executed, none of the other elif statements will execute, leading to you only updating the playerX variable.

What you want to do is split the if statement into two separate statements (one for X and one for Y) so that the adjustments of X,Y are independent of each other. Something similar to

    if mouseX >= playerX:
        playerX = movePos + playerX
        running = 0
    elif mouseX <= playerX:
        playerX = moveNeg + playerX           
        running = 0

    #By having the second if, it allows you to check Y even if X has changed
    if mouseY >= playerY:
        playerY = movePos + playerY            
        running = 0
    elif mouseY <= playerY:
        playerY = moveNeg + playerY            
        running = 0