Hi Alex,
Sorry for the slow reply. As you can tell, moving with the mouse is not as straightforward as the keyboard. You'll first want to start by adding an event to capture the mouse clicks:
@screen.event
def on_mouse_press(x, y, button, modifiers):
## update something with x and y
You'll now be able to tell where you clicked. This information should be stored in a variable. After that, you will need your character to walk towards this point. You don't want it to happen instantly, so you'll need a function that does the following:
1. Compares the click position with the sprite's current position.
2. Determine how you want to move towards the goal. X axis first, then Y, or both at once?
3. Move towards the goal by a certain amount of pixels per frame.
4. You now know which direction you are moving, so you can update the sprite.image attribute with the appropriate animation when you change directions.
One way you can do this is by creating a function, and then scheduling it with `pyglet.clock.schedule_interval(some_function, interval)`
def update_position(dt):
x_difference = target_x - player.x
y_difference = target_y - player.y
if not x_difference or not y_difference:
return # Already at position
# put logic here
Schedule it at 30fps (or whatever you want).
pyglet.clock.schedule_interval(update_position, 1/30)
(Please note that this simple example assumes you are moving by 1 pixel each time. If you're not, you'll have to do some better checking to determine if the player has reached the target).