コード例 #1
0
ファイル: gameloop.py プロジェクト: tartley/sole-scion
    def init(self, name, version):
        clock.set_fps_limit(FPS_LIMIT)
        self.fps_display = clock.ClockDisplay()

        self.camera = Camera((0, 0), 800)
        self.renderer = Renderer(self.camera)
        caption = '%s v%s' % (name, version)
        self.window = Window(
            caption=caption, fullscreen=True, visible=False)
        self.window.on_key_press = on_key_press
        self.window.push_handlers(Keyboard.keystate)

        graphics = load_graphics()

        self.world = World()
        builder = LevelBuilder()
        seed(1)
        builder.build(self.world, 75, graphics)

        self.world.player = Player()
        self.world.player.add_to_space(self.world.space, (0, 200), 0)
        self.world.chunks.update(self.world.player.chunks)
コード例 #2
0
ファイル: renderer_test.py プロジェクト: tartley/sole-scion
    def test_draw_calls_subroutines_in_right_order(self):
        camera = Camera((0, 1), 2)
        renderer = Renderer(camera)

        roomColor = (255,0,0)
        verts = [(-1, 0), (2,3), (4, 0)]
        room1 = Room(verts)
        room2 = Room(verts)

        chunk1 = Chunk()
        chunk2 = Chunk()

        world = World()
        world.backColor = (111, 22, 3)
        world.rooms = {1: room1, 2: room2}
        world.chunks = set([chunk1, chunk2])

        listener = Listener()
        renderer.clear = \
            lambda *args: listener("clear", *args)
        camera.world_projection = \
            lambda *args: listener("world_proj", *args)
        renderer.draw_rooms = \
            lambda *args: listener("draw_rooms", *args)
        renderer.draw_chunk = \
            lambda *args: listener("draw_chunk", *args)

        aspect = 1.5
        renderer.draw(world, aspect)

        expected = [
            ("clear", world.material.color),
            ("world_proj", aspect),
            ("draw_rooms", {1: room1, 2: room2}),
            ("draw_chunk", chunk2),
            ("draw_chunk", chunk1),
        ]
        self.assertEquals(listener.args_list, expected,
            "draw didnt call subfns")
コード例 #3
0
ファイル: gameloop.py プロジェクト: tartley/sole-scion
class Gameloop(object):

    instance = None

    def __init__(self):
        Gameloop.instance = self
        self.window = None
        self.camera = None
        self.world = None
        self.renderer = None
        self.paused = False
        self.fps_display = None
        Keyboard.handlers.update({
            key.PAGEUP: lambda: self.camera.zoom(2.0),
            key.PAGEDOWN: lambda: self.camera.zoom(0.5),
            key.ESCAPE: self.quit_game,
            key.PAUSE: self.toggle_pause,
            key.F12: lambda: save_screenshot(self.window),
        })


    def init(self, name, version):
        clock.set_fps_limit(FPS_LIMIT)
        self.fps_display = clock.ClockDisplay()

        self.camera = Camera((0, 0), 800)
        self.renderer = Renderer(self.camera)
        caption = '%s v%s' % (name, version)
        self.window = Window(
            caption=caption, fullscreen=True, visible=False)
        self.window.on_key_press = on_key_press
        self.window.push_handlers(Keyboard.keystate)

        graphics = load_graphics()

        self.world = World()
        builder = LevelBuilder()
        seed(1)
        builder.build(self.world, 75, graphics)

        self.world.player = Player()
        self.world.player.add_to_space(self.world.space, (0, 200), 0)
        self.world.chunks.update(self.world.player.chunks)


    def dispose(self):
        if self.window:
            self.window.close()


    def run(self):
        try:
            self.window.set_visible()
            while not self.window.has_exit:
                self.window.dispatch_events()
                clock.tick()
                if self.world and not self.paused:
                    self.world.tick(1/FPS_LIMIT)
                if self.world and hasattr(self.world, 'player'):
                    player_position = self.world.player.chunks[0].body.position
                    self.camera.x, self.camera.y = player_position
                    self.camera.angle = atan2(
                        player_position.x,
                        player_position.y)

                self.camera.update()
                if self.renderer:
                    aspect = (
                        self.window.width / self.window.height)
                    self.renderer.draw(self.world, aspect)
                self.camera.hud_projection(
                    (self.window.width, self.window.height))
                self.fps_display.draw()
                self.window.flip()
        finally:
            self.dispose()


    def toggle_pause(self):
        self.paused = not self.paused


    def quit_game(self):
        self.window.has_exit = True