예제 #1
0
 def __init__(self, window, *args, **kwargs):
     State.__init__(self, window, *args, **kwargs)
     self.score = ScoreWidget(window)
     self.mf = MoleculeFactory()
     self.blend = pyglet.resource.image("blend.png")
     self.heart = pyglet.resource.image("heart.png")
     self.eat = pyglet.resource.media("eat.wav", streaming=False)
     self.explode = pyglet.resource.media("explode.wav", streaming=False)
     self.cell = Cell(500, 300)
     self.cell_emitter = PositionalBubbleEmitter(window, self.cell)
     self.game_map = GameMap("level1")
     self.camera = Camera(self.cell, self.window, self.game_map)
     self.life = 9
     self.fps_display = pyglet.clock.ClockDisplay()
예제 #2
0
class Playing(State):
    def __init__(self, window, *args, **kwargs):
        State.__init__(self, window, *args, **kwargs)
        self.score = ScoreWidget(window)
        self.mf = MoleculeFactory()
        self.blend = pyglet.resource.image("blend.png")
        self.heart = pyglet.resource.image("heart.png")
        self.eat = pyglet.resource.media("eat.wav", streaming=False)
        self.explode = pyglet.resource.media("explode.wav", streaming=False)
        self.cell = Cell(500, 300)
        self.cell_emitter = PositionalBubbleEmitter(window, self.cell)
        self.game_map = GameMap("level1")
        self.camera = Camera(self.cell, self.window, self.game_map)
        self.life = 9
        self.fps_display = pyglet.clock.ClockDisplay()

    def update(self, dt):
        self.cell_emitter.update(dt)
        self.cell.update(dt, self.game_map)
        self.update_molecules(dt)
        self.update_camera(dt)

        # game over
        if self.life <= 0:
            self.window.replace_state(GameOver, self)

    def update_molecules(self, dt):
        self.game_map.spawner.update(dt)
        for m in self.game_map.spawner.molecules:
            if self.cell.collides_with(m):
                if sum(m.numbers) == 9:
                    self.eat.play().volume = 0.1
                    self.score.add(300)
                elif sum(m.numbers) == 18:
                    self.eat.play().volume = 0.15
                    self.score.add(1000)
                else:
                    self.explode.play().volume = 0.1
                    self.life -= 1
                m.kill()

            m.update(dt, self.game_map)

    def update_camera(self, dt):
        self.camera.calc_offset(dt)

    def on_draw(self):
        self.window.clear()
        glLoadIdentity()
        glPushMatrix()
        ## move the projection to the cameras offset
        offset = self.camera.get_offset()
        glTranslatef(offset[0], offset[1], 0.0)
        self.game_map.draw()
        self.cell_emitter.draw()
        ## offset the blend to always be on the screen
        self.blend.blit(abs(offset[0]), abs(offset[1]))
        ## Draw player
        self.cell.draw()
        for m in self.game_map.spawner.molecules:
            m.draw()
        glPopMatrix()
        ## Draw the ui stuffs
        self.score.draw()
        # self.fps_display.draw()
        heart_x_offset = 20
        for i in range(self.life):
            self.heart.blit(heart_x_offset, self.window.height - 50)
            heart_x_offset += 45
        return pyglet.event.EVENT_HANDLED

    def on_key_press(self, k, args):
        ## handle arrow key movement
        if k == key.UP:
            self.cell.moving.y = 1
        elif k == key.DOWN:
            self.cell.moving.y = -1
        elif k == key.LEFT:
            self.cell.moving.x = -1
        elif k == key.RIGHT:
            self.cell.moving.x = 1

        ## Turn on debugging mode
        elif k == key.D:
            if self.game_map.debug == False:
                self.game_map.debug = True
            else:
                self.game_map.debug = False
        ## exit the current game
        elif k == key.ESCAPE:
            self.window.pop_state()

        ## pause the game
        elif k == key.ENTER:
            self.window.push_state(Pause, self)

        return pyglet.event.EVENT_HANDLED

    def on_key_release(self, k, args):
        if k == key.UP or k == key.DOWN:
            self.cell.moving.y = 0
        elif k == key.LEFT or k == key.RIGHT:
            self.cell.moving.x = 0

        return pyglet.event.EVENT_HANDLED

    def on_gain_focus(self):
        pyglet.clock.schedule_interval(self.update, 1.0 / 60.0)
        self.music = pyglet.resource.media("play02.mp3")
        self.player = self.music.play()
        self.player.queue(pyglet.resource.media("play03.mp3"))
        self.player.queue(pyglet.resource.media("play01.mp3"))
        self.player.volume = 0.1
        return pyglet.event.EVENT_HANDLED

    def on_lose_focus(self):
        self.player.pause()
        pyglet.clock.unschedule(self.update)
        pyglet.clock.unschedule(self.update_molecules)
        pyglet.clock.unschedule(self.update_camera)
        return pyglet.event.EVENT_HANDLED