Hi. I'm rather new to Python and very new to Pyglet, but I've been
messing around with it for a couple of days now. It's looking like a
rather awesome multimedia library, but I've got one problem. I've
basically got a sprite that's movable with the keyboard keys but the
movement is a bit slow and sluggish. However, if I keep moving the
mouse over the window at the same time as I'm using the keys, the game
runs fast and exactly the way I want it to run. I'm rather sure I've
messed up something here but I'm not really sure where. Am I using the
event structure correctly or is it something else? Here's the code:

import pyglet
from pyglet import clock
from pyglet.window import key

MOV = 8

class GameWindow(pyglet.window.Window):
    def __init__(self, x, y):
        """Initialising the game window"""
        super(GameWindow, self).__init__(width=x, height=y)
        self.set_caption("Test")

    def on_draw(self):
        self.clear()
        background.draw()
        ship.update()

    def on_key_press(self, symbol, modifiers):
        if symbol == key.UP:
            ship.move_y(MOV)
        elif symbol == key.DOWN:
            ship.move_y(-MOV)
        elif symbol == key.RIGHT:
            ship.move_x(MOV)
        elif symbol == key.LEFT:
            ship.move_x(-MOV)

    def on_key_release(self, symbol, modifiers):
        if symbol == key.UP:
            ship.move_y(-MOV)
        elif symbol == key.DOWN:
            ship.move_y(MOV)
        elif symbol == key.RIGHT:
            ship.move_x(-MOV)
        elif symbol == key.LEFT:
            ship.move_x(MOV)

class BaseSprite(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.dx = 0
        self.dy = 0

    def draw(self):
        self.sprite.blit(self.x, self.y)

    def update(self):
        self.x, self.y = self.x + self.dx, self.y + self.dy
        self.draw()

class Player(BaseSprite):
    def __init__(self, x, y):
        super(Player, self).__init__(x, y)
        self.sprite = pyglet.image.load("ship.png")
    def move_x(self, dx):
        self.dx += dx
        print "x is", self.dx
    def move_y(self, dy):
        self.dy += dy
        print "y is", self.dy

class StaticSprite(BaseSprite):
    def __init__(self, x, y):
        super(StaticSprite, self).__init__(x,y)
        self.sprite = pyglet.image.load("background.jpg")

if __name__ == "__main__":

    background = StaticSprite(0, 0)
    ship = Player(150, 150)
    window = GameWindow(800,600)
    pyglet.app.run()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to