I\'m new in python. I\'m trying to achieve a simple object movement on canvas.
The idea is to simply update X, Y coordinates and redraw the oval.
I\'ve tried
Instead of character.x += 10
or character.y -= 10
, you need to use move
:
canvas.move(oval, 10, 0) # for x += 10
canvas.move(oval, 0, -10) # for y -= 10
The rest should follow.
Instead of a Character
class, you can just say oval = canvas.create_oval(x, y, x + 40, y + 40, fill=color)
.
**Please note: none of this code will work - it's just here to give you ideas about how to do stuff. :)
I've had objects bound to the keyboard that move around the screen when buttons are pressed.
instead of a loop, you can just change the x and y of an object with config and bind... when you press left on the keyboard the def will be run which moves the thing. (or things)
def move_object_left()...
object.config(move left...)
example of binding something:
entry.bind('<ButtonRelease-1>', lambda event: self.maximise_keyboard(event.widget))
x_var = 5 y_var = 9
**Bind an object to the keyboard here:
*On_key_press('RIGHT'):
x_var = x_var + 5
object.config(x = x_var)
You can move a bunch of stuff at once if you want... (though you will have to do the code yourself lol)
list_of_stuff = [tree, bush, snail]
for entry in list_of_stuff:
...
**Get object X and Y of the object...
** add a number to this X and Y...