Example #1
0
class App:
    def __init__(self):
        pyxel.init(240, 160, caption='test game')

        self.levelname = 'level1'
        self.assets = os.path.join(
            os.getcwd(),
            os.path.dirname(__file__),
            'assets',
        )

        pyxel.image(0).load(0, 0, os.path.join(self.assets, 'animation.png'))
        pyxel.image(1).load(
            0, 0, os.path.join(self.assets, self.levelname, 'tileset.png'))
        pyxel.image(2).load(
            0, 0, os.path.join(self.assets, self.levelname, 'background.png'))

        self.level = Level(os.path.join(self.assets, self.levelname),
                           'mapfile.txt', 16)
        self.camera = Camera(self.level)
        self.player = Player(self.assets)
        self.sparkle_emitter = ParticleEmitter(self.player)

        self.zero_frame = 0
        pyxel.run(self.update, self.draw)

    def update(self):
        if pyxel.btn(pyxel.KEY_A):
            self.player.run(-1)
        if pyxel.btn(pyxel.KEY_D):
            self.player.run(1)
        if pyxel.btn(pyxel.KEY_W):
            if self.player.on_wall:
                self.player.climb(-1)
        if pyxel.btn(pyxel.KEY_S):
            if self.player.on_wall:
                self.player.climb(1)
        if pyxel.btnp(pyxel.KEY_L):
            self.player.attack()
        if pyxel.btnp(pyxel.KEY_SPACE):
            if self.player.on_wall:
                self.player.wall_jump()
            elif self.player.grounded:
                self.player.jump()
            elif self.player.double_primed:
                self.player.double_jump()
        if pyxel.btn(pyxel.KEY_ESCAPE):
            pyxel.quit()

        self.camera.update_last_offset()
        self.update_player()

    def draw(self):
        pyxel.cls(1)
        pyxel.blt(0, 0, 2, 0, 0, 240, 70, 1)
        # render the city
        # for i in range(2):
        #     pyxel.blt(i * 240 - self.camera.offset_x/2, 39, 2, 0, 82, 240, 82, 1)
        # render the clouds
        for i in range(2):
            pyxel.blt(i * 240 - self.camera.offset_x // 50,
                      10 - self.camera.offset_y // 50, 2, 0, 168, 240, 40, 1)
        self.level.render(self.camera, self.level.background, 1)
        self.level.render(self.camera, self.level.collision, 1)
        self.sparkle_emitter.render_particles()
        self.player.update_anim()
        self.level.render(self.camera, self.level.foreground, 1)

    def update_player(self):
        self.player.x, self.camera.offset_x = update_axis(
            self.player.x,
            self.player.vx,
            self.camera.offset_x,
            self.camera.max_scroll_x,
            pyxel.width,
        )
        self.player.x_collision(self.camera, self.level)

        self.player.y, self.camera.offset_y = update_axis(
            self.player.y,
            self.player.vy,
            self.camera.offset_y,
            self.camera.max_scroll_y,
            pyxel.height,
        )
        self.player.y_collision(self.camera, self.level)

        self.player.update_gravity()
        self.sparkle_emitter.update_position(self.camera.offset_delta())
        self.sparkle_emitter.sparkle(0, 1, 12)
Example #2
0
class App:
    def __init__(self):
        pyxel.init(240, 160, caption='test game')

        self.levelname = 'level2'
        self.assets_dir = 'assets'
        self.assets = os.path.realpath(
            os.path.join(os.getcwd(), os.path.dirname(__file__),
                         self.assets_dir, self.levelname))

        pyxel.image(0).load(0, 0, os.path.join(self.assets,
                                               '../animation.png'))
        pyxel.image(1).load(0, 0, os.path.join(self.assets, 'tileset.png'))
        pyxel.image(2).load(0, 0, os.path.join(self.assets, 'background.png'))

        self.level = Level(self.assets, 'mapfile.txt', 16)
        self.camera = Camera(self.level)
        self.player = Player()
        self.sparkle_emitter = ParticleEmitter(self.player)
        self.sound = Sound()
        self.sound.start_music()

        self.test_val = 0
        pyxel.run(self.update, self.draw)

    def update(self):
        if pyxel.btn(pyxel.KEY_A):
            self.player.run(-1)
        if pyxel.btn(pyxel.KEY_D):
            self.player.run(1)
        if pyxel.btn(pyxel.KEY_I):
            self.test_val += 1
        if pyxel.btn(pyxel.KEY_O):
            self.test_val -= 1
        if pyxel.btnp(pyxel.KEY_S):
            if self.player.grounded:
                self.sound.sfx_buildup()
        if not pyxel.btn(pyxel.KEY_S):
            self.sound.sfx_stop()
        if pyxel.btnp(pyxel.KEY_S, 60, 30):
            print(pyxel.frame_count)
            if self.player.grounded:
                self.player.charge()
        if pyxel.btn(pyxel.KEY_SPACE):
            if self.player.grounded:
                self.player.jump()
                self.sound.sfx_jump()
        if pyxel.btn(pyxel.KEY_ESCAPE):
            pyxel.quit()
        if pyxel.btn(pyxel.KEY_T):
            pyxel.pal(1, 7)
        else:
            pyxel.pal()
        if pyxel.btn(pyxel.KEY_P):
            import pdb
            pdb.set_trace()

        self.camera.last_offset_x, self.camera.last_offset_y = self.camera.offset_x, self.camera.offset_y
        self.update_player()

    def draw(self):
        pyxel.cls(1)
        pyxel.blt(0, 0, 2, 0, 0, 240, 70, 1)
        # render the city
        for i in range(2):
            pyxel.blt(i * 240 - self.camera.offset_x / 2, 39, 2, 0, 82, 240,
                      82, 1)
        # render the clouds
        for i in range(2):
            pyxel.blt(i * 240 - self.camera.offset_x / 50, 10, 2, 0, 168, 240,
                      40, 1)
        self.level.render(self.camera, self.level.background, 1)
        self.level.render(self.camera, self.level.collision, 1)
        self.player.render()
        self.sparkle_emitter.render_particles()
        self.level.render(self.camera, self.level.foreground, 1)

    def update_player(self):
        self.player.x, self.camera.offset_x = update_axis(
            self.player.x,
            self.player.vx,
            self.camera.offset_x,
            self.camera.max_scroll_x,
            pyxel.width,
        )
        self.player.x_collision(self.camera, self.level)

        self.player.y, self.camera.offset_y = update_axis(
            self.player.y,
            self.player.vy,
            self.camera.offset_y,
            self.camera.max_scroll_y,
            pyxel.height,
        )
        self.player.y_collision(self.camera, self.level)

        self.sparkle_emitter.update_position(self.offset_delta())

        self.player.vy = min(self.player.vy + 1, 7)
        if self.player.vx > 0:
            self.player.vx = self.player.vx - 1
        elif self.player.vx < 0:
            self.player.vx = self.player.vx + 1

        if self.player.jump_chg >= 4:
            self.sparkle_emitter.sparkle(self.test_val)
            self.sound.sfx_pickup()

    def offset_delta(self):
        return self.camera.offset_x - self.camera.last_offset_x, self.camera.offset_y - self.camera.last_offset_y