Hi Sophie,
We have a game example in the documentation:
http://pyglet.readthedocs.io/en/latest/programming_guide/examplegame.html
Since you're new to coding it might be a little advanced, but feel free to
ask for help here if you get stuck.
In the mean time, here is a bit lengthy working example:
import pyglet
from pyglet.window import key
# Create the window and the key state handler:
window = pyglet.window.Window(width=960, height=540, caption="My Awesome Game")
key_handler = pyglet.window.key.KeyStateHandler()
window.push_handlers(key_handler)
# Graphics batch for efficiently drawing everything:
batch = pyglet.graphics.Batch()
# Create an image from scratch, rather than loading one:
blue_pattern = pyglet.image.SolidColorImagePattern(color=(0, 0, 255, 255))
blue_image = blue_pattern.create_image(50, 50)
# Set the image "anchor" so that it rotates around it's center:
blue_image.anchor_x = blue_image.width // 2
blue_image.anchor_y = blue_image.height // 2
# Create the player sprite:
player_sprite = pyglet.sprite.Sprite(img=blue_image, x=400, y=200, batch=batch)
# Define a game loop:
def update_game(dt):
# check the current state of the key handler, and update the sprite:
if key_handler[key.LEFT]:
player_sprite.rotation -= 3
if key_handler[key.RIGHT]:
player_sprite.rotation += 3
# !!!! Put additional stuff here:
# Finally, clear the window and draw everything:
window.clear()
batch.draw()
# Schedule the "update_game" function to be called at 60fps,
# and then enter the main application loop:
pyglet.clock.schedule_interval(update_game, 1/60)
pyglet.app.run()
On Monday, November 27, 2017 at 6:41:37 AM UTC+9, [email protected]
wrote:
>
> Hi,
>
> I'm a beginer in coding and I have to create a game for a school projet.
> I don't understand how to do the KeyStateHandler to make my spaceship
> rotate. I watched a lot a videos but I still don't understand how and where
> in my code I'm supposed to used it.
> Can someone explain it to me?
>
> Thank you,
> Sophie
>
--
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.