0

How do you make this code work: Just have pyglet installed and change assassin1.png,assassin2.png and assassin3.png with the name of images stored in the directory where you saved this code to a file.

import pyglet

def sprite_type(type_ = "standing"):
    if type_ == "moving-forward":
        moving_forward_image_list = [pyglet.image.load('assassin2.png'), pyglet.image.load('assassin3.png')]
        moving_forward_animation = pyglet.image.Animation.from_image_sequence(moving_forward_image_list, 0.3) 
        return moving_forward_animation
    if type_ == "standing":
        standing_animation = pyglet.image.load("assassin1.png")
        return standing_animation

class Assassin(pyglet.sprite.Sprite):        
    def __init__(self, batch, img):
        pyglet.sprite.Sprite.__init__(self, img, x = 50, y = 30)

class Game(pyglet.window.Window):
    def __init__(self):
        pyglet.window.Window.__init__(self, width = 315, height = 220)
        self.batch_draw = pyglet.graphics.Batch()
        self.player_dict = {"standing": Assassin(batch = self.batch_draw, img = sprite_type()), "moving-forward": Assassin(batch = self.batch_draw, img = sprite_type(type_ = "moving-forward"))}
        self.player = self.player_dict["standing"]
        self.fps_display = pyglet.clock.ClockDisplay()
        self.keys_held = []      
        self.schedule = pyglet.clock.schedule_interval(func = self.update, interval = 1/60.) 

    def on_draw(self):
        self.clear()
        self.fps_display.draw()
        self.batch_draw.draw()
        self.player.draw()  

    def on_key_press(self, symbol, modifiers):
        self.keys_held.append(symbol)
        if symbol == pyglet.window.key.RIGHT:
            self.player = self.player_dict["moving-forward"]
            print "The 'RIGHT' key was pressed"

    def on_key_release(self, symbol, modifiers):
        self.keys_held.pop(self.keys_held.index(symbol))
        self.player = self.player_dict["standing"]

    def update(self, interval):
        if pyglet.window.key.RIGHT in self.keys_held:
            self.player.x += 50 * interval

if __name__ == "__main__":
    window = Game()
    pyglet.app.run()

This code displays assassin1.png while no key is held at position (50 30), when the right key is held the character moves forward and every 0.3 seconds the 2 images assassin2.png and assassin3.png are switched as to give the illusion of a walking character. Also the fps are shown.

Currently when you release the key the character will return to the original position (50, 30). When you press the key again the character moves from the position where the walking character stopped last.

I understand why this happens because the walking animation character and the standing character are two different instances of the same class. I first thought using class variables I would be able to store the position. But I can't seem to come up with a working solution. Does anyone have any ideas what I could do?

Edit 1 (@Hugh Bothwell)

import pyglet

def sprite_type(type_ = "standing"):
    if type_ == "moving-forward":
        moving_forward_image_list = [pyglet.image.load('assassin2.png'), pyglet.image.load('assassin3.png')]
        moving_forward_animation = pyglet.image.Animation.from_image_sequence(moving_forward_image_list, 0.3) 
        return moving_forward_animation
    if type_ == "standing":
        standing_animation = pyglet.image.load("assassin1.png")
        return standing_animation

class Assassin(pyglet.sprite.Sprite):
    def __init__(self, batch, img):
        pyglet.sprite.Sprite.__init__(self, img, x = 50, y = 30)

    def stand(self, batch, img):
        self.batch = batch
        self.img = img

    def move(self, batch, img):
        self.batch = batch
        self.img = img      

class Game(pyglet.window.Window):
    def __init__(self):
        pyglet.window.Window.__init__(self, width = 315, height = 220)
        self.batch_draw = pyglet.graphics.Batch()
        self.player = Assassin(batch = self.batch_draw, img = sprite_type())
        self.fps_display = pyglet.clock.ClockDisplay()
        self.keys_held = []      
        self.schedule = pyglet.clock.schedule_interval(func = self.update, interval = 1/60.) 

    def on_draw(self): 
        self.clear()         
        self.fps_display.draw()
        self.batch_draw.draw()
        self.player.draw()  

    def on_key_press(self, symbol, modifiers):
        self.keys_held.append(symbol)
        if symbol == pyglet.window.key.RIGHT:
            self.player.move(batch = self.batch_draw, img = sprite_type("moving-forward"))
            # if I put the following line here instead:
            #self.player = self.player.move(batch = self.batch_draw, img = sprite_type("moving-forward"))
            # I get this error when pressing the <right-direction> key:
            #self.player.draw(), AttributeError: 'NoneType' object has no attribute 'draw'
            print "The 'RIGHT' key was pressed"

    def on_key_release(self, symbol, modifiers):
        self.keys_held.pop(self.keys_held.index(symbol))
        self.player.stand(batch = self.batch_draw, img = sprite_type("standing"))

    def update(self, interval):
        if pyglet.window.key.RIGHT in self.keys_held:
            self.player.x += 50 * interval

if __name__ == "__main__":
    window = Game()
    pyglet.app.run()
4

1 回答 1

1

您需要一个具有运动状态的角色,而不是站立角色和行走角色。

于 2012-06-30T15:41:17.383 回答