On Sun, Oct 19, 2008 at 8:54 AM, Honro <[EMAIL PROTECTED]> wrote:
> 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:

You were updating the sprite position every time it was drawn.  pyglet
1.1 and newer work by calling the draw functions as often as
necessary, depending on how fast events are coming in.  You want to
use the draw functions to just draw things, and call the update()
function separately using pyglet.clock.schedule_interval(func,
timedelta).

The function that pyglet.clock.schedule_interval calls gets an
argument dt that indicates how long has passed since the last call.
In the following code I've scheduled ship.update to be called 30 times
a second.  It then moves the ship according to the dx and dy
variables, in proportion to dt.  That way if your graphics card is
slow and can't handle 30 fps, the sprite will still move at the right
speed.  If your graphics card can handle 200 fps, it will also move at
the right speed.

I also made your background into a kitten.

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")
       pyglet.clock.schedule_interval(ship.update, 1.0 / 30.0)

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

   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, dt):
       self.x, self.y = self.x + self.dx * dt * 30, self.y + self.dy * dt * 30

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("kitten.png")

if __name__ == "__main__":

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

--
Nathan Whitehead

--~--~---------~--~----~------------~-------~--~----~
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