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).
On Friday, July 27, 2018 at 1:35:22 PM UTC+9, Alex Nieto wrote:
>
> ok, thanks for your answer but in that case how would the movements that
> the sprite follow the clicks of the mouse?
>
> El jueves, 26 de julio de 2018, 7:12:09 (UTC-5), Benjamin Moran escribió:
>>
>> Hi Alex, there are a few issues I can see. First of all, the "frame_up",
>> "frame_down", etc., are just lists. And ImageGrid just makes it easy to
>> get lists of images. What you want to do, is create an Animation. You can
>> use the `pyglet.image.Animation.from_image_sequence(sequence, period)`
>> method to create an Animation from that.
>> Then, when you move, you want to update the `player.image =
>> new_animation`.
>>
>> To move the sprite, you can update it's x, y, or position attributes. For
>> example "player.x += 5"
>>
>>
>> On Thursday, July 26, 2018 at 2:06:30 AM UTC+9, Alex Nieto wrote:
>>>
>>> Hi Benjamin, thanks for answering. I want to move around the screen as
>>> in an RPG game format, I show you the code that I have. In this code I try
>>> to move it with the keyboard and it gives me an error, but in reality I
>>> want to move it with mouse clicks.
>>>
>>>
>>> <https://lh3.googleusercontent.com/-Kr5wsUjTD7Q/W1ipH18pDbI/AAAAAAAAAN0/p09_xKrE90cTjmPjr75Xrt3frZ4g8TyDgCLcBGAs/s1600/codigo_pyglet.JPG>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> This is the result is a 4x4 grid sprite where the initial position is
>>> [0, 0]
>>>
>>>
>>> <https://lh3.googleusercontent.com/-iiGnP4KT_IQ/W1irJ9vdBtI/AAAAAAAAAOA/EyQeD4W8kFkI3Yr-WNjG2CrGLzxT3d1XwCLcBGAs/s1600/Captura_pantalla.JPG>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> and the error it gives when trying to move the sprite is this.
>>> (AttributeError: 'List' object has not attribute 'y') so on go pressing the
>>> directional keys, but as I mentioned earlier my intention is to move it
>>> with the mouse clicks this was just a test
>>>
>>>
>>> <https://lh3.googleusercontent.com/-knU20g4gF9Y/W1ir91_XkXI/AAAAAAAAAOI/u9tUcUROIq85cKD0BJ18Ddd5Sys68PeYgCLcBGAs/s1600/Captura_error.JPG>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> El miércoles, 25 de julio de 2018, 4:44:22 (UTC-5), Benjamin Moran
>>> escribió:
>>>>
>>>> Hi Alex,
>>>>
>>>> Do you want to move individual sprites, or do you want to scroll the
>>>> entire screen?
>>>>
>>>
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.