Question

I am trying to create a sprite class and a key handle to move the sprite, but it doesn't seem to be working. When executed the window will appear, but sprite doesn't. What's wrong with it?

import pyglet
import cocos
from pyglet.window import key

window = pyglet.window.Window()
keyboard = key.KeyStateHandler()

class Player(cocos.layer.Layer):

  def __init__(self):
    super(Player, self).__init__()

    img = pyglet.image.load('../game/resources/sprite_1.png')
    self.sprite = pyglet.sprite.Sprite(img)

  def animate(dt, velocity, sprite):
    sprite.position += dt * velocity

  def on_key_press(self, symbol, keyboard, modifiers):
    if symbol == key.RIGHT:
        self.img.x += dt * 10
    elif symbol == key.LEFT:
        self.img.x -= dt * 10
    elif symbol == key.UP:
        self.img.y += dt * 10
    elif symbol == key.DOWN:
        self.img.y -= dt * 10



  def on_key_release(self, smybol, keyboard, modifiers):
    if symbol == key.RIGHT:
        self.sprite.x = dt * 0
    elif symbol == key.LEFT:
        self.sprite.x = dt * 0
    elif symbol == key.DOWN:
        self.sprite.y = dt * 0
    elif symbol == key.UP:
        self.sprite.y = dt * 0


pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)

pyglet.app.run()
Was it helpful?

Solution

There are quite a few problems in your code which are preventing anything from happening:

  1. You're creating a cocos2d entity within pyglet, without telling pyglet how to handle it. You're possibly better off using cocos2d for things like window creation etc.

  2. You're creating a pyglet Sprite when you should probably be using a cocos2d one, and you're not attaching it to the Layer.

  3. animate isn't part of the API for Layer (and even if it was, it would probably not be a static method).

  4. You haven't defined Player as an event handler, so on_key_press etc won't do anything.

  5. You're passing more arguments into on_key_press etc than are required, and are referencing dt without passing it in.

Here is a modified version of your sample code which should help you move forward:

import cocos
import pyglet
from pyglet.window import key

keyboard = key.KeyStateHandler()

class Player(cocos.layer.Layer):
    is_event_handler = True

    def __init__(self):
        super(Player, self).__init__()
        img = pyglet.image.load('../game/resources/sprite_1.png')
        self.sprite = cocos.sprite.Sprite(img)
        self.add(self.sprite)

    def on_key_press(self, symbol, modifiers):
        print 'pressing'
        if symbol == key.RIGHT:
            self.sprite.x += 10
        elif symbol == key.LEFT:
            self.sprite.x -= 10
        elif symbol == key.UP:
            self.sprite.y += 10
        elif symbol == key.DOWN:
            self.sprite.y -= 10

if __name__ == '__main__':
    cocos.director.director.init()

    player = Player()
    main_scene = cocos.scene.Scene( player )

    cocos.director.director.run( main_scene )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top