My python function won't return or update the value -
def getmove(win,playerx,playery): #define variables. movepos = 75 moveneg = -75 running = 1 #run while loop 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 , player objects, draw key, don't draw 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 using graphics library create game. player , key drawn in correct places. however, when calling getmove function, playerx , playery not update. have added debug print statements find values while running game , 75 , 125. help!
in python, integers immutable - when assign new integer value variable, making variable point new integer, not changing old integer pointed to's value was.
(an example of mutable object in python list, can modify , variables pointing list notice change - since list has changed.)
similarly, when pass variable method in python , alter variable points in method, not alter variable points outside of method because new variable.
to fix this, assigned returned playerx,playery variables outside method:
playerx, playery = getmove(win,playerx,playery)
Comments
Post a Comment