I need to use variables created in one function as parameters/arguments in another function. I am not sure how to do so without getting a continuous error signal as I am repeating the function every 10 milliseconds. Here is the code:
def addFish():
x = createRandomX()
y = createRandomY()
Dx = createRandomDx()
Dy = createRandomDy()
fish = canvas.create_image(x, y, image = fishRight)
updateFish(x, y, Dx, Dy, fish)
return x, y, Dx, Dy, fish
fishButton = Button(root, text = "Add a fish", command = addFish)
fishButton.grid(row = 1, column = 0)
def updateFish(x, y, Dx, Dy, fish):
#global x, y, Dx, Dy, fish
canvas.coords(fish, x , y )
x = x + Dx
y = y + Dy
if x < 0 or x > 500:
Dx = Dx * -1
if y < 0 or y > 500:
Dy = Dy * -1
if Dx == 1:
canvas.itemconfig(fish, image=fishRight)
if Dx == -1:
canvas.itemconfig(fish, image=fishLeft)
canvas.after(10, updateFish(x, y, Dx, Dy, fish))
updateFish(x, y, Dx, Dy, fish)
root.mainloop()
So, when I do this i get the error that x is not defined. This is because when I call updateFish, the first argument x is not a global variable, it is only a local variable of addFish(). So, I need to know how to be able to use the variables in addFish() as arguments in the function updateFish.
I'm building a fish tank for class that has the fish bounce around continuously, but I need the updateFish function to work for all of the fish that I add--therefore it needs to be an argument in the function.