コード例 #1
0
ファイル: main.py プロジェクト: GameOfPython/simplegame
    def __init__(self):
        # Load Assets
        self.load_content()

        self.window = RenderWindow(VideoMode(WIDTH, HEIGHT, BPP), TITLE,
                                   Style.CLOSE | Style.TITLEBAR,
                                   SETTINGS)
        self.window.framerate_limit = 60
        self.window.icon = self.icon.pixels

        # Clock
        self.clock = Clock()
        self.time_since_last_update = Time.ZERO
        self.FPS = seconds(1.0/30.0)

        # View
        self.view = View(Rectangle((0, 0), (WIDTH, HEIGHT)))
        # self.view.reset(Rectangle((0, 0), (WIDTH, HEIGHT)))
        self.view.viewport = (0.0, 0.0, 1.0, 1.0)

        self.hud = View(Rectangle((0, 0), (WIDTH, HEIGHT)))
        self.hud.viewport = (0.0, 0.0, 1, 1.0)

        # attr: font, text, pos, color=Color.GREEN
        self.fps_pos = Vector2(15, 15)
        self.fps_text_content = '...'
        self.fps_text = Text(self.fps_text_content,
                             self.fps_font, 14)

        self.fps = FPS(self.fps_text,
                       self.fps_pos)

        # PLAYER
        self.player_spt = Sprite(self.player_texture)
        self.player = Player(self.window,
                             self.player_spt,
                             self.player_texture, 0, 0)

        self.bg = Sprite(self.bg)
コード例 #2
0
ファイル: main.py プロジェクト: GameOfPython/simplegame
class Game(object):
    """..."""
    def __init__(self):
        # Load Assets
        self.load_content()

        self.window = RenderWindow(VideoMode(WIDTH, HEIGHT, BPP), TITLE,
                                   Style.CLOSE | Style.TITLEBAR,
                                   SETTINGS)
        self.window.framerate_limit = 60
        self.window.icon = self.icon.pixels

        # Clock
        self.clock = Clock()
        self.time_since_last_update = Time.ZERO
        self.FPS = seconds(1.0/30.0)

        # View
        self.view = View(Rectangle((0, 0), (WIDTH, HEIGHT)))
        # self.view.reset(Rectangle((0, 0), (WIDTH, HEIGHT)))
        self.view.viewport = (0.0, 0.0, 1.0, 1.0)

        self.hud = View(Rectangle((0, 0), (WIDTH, HEIGHT)))
        self.hud.viewport = (0.0, 0.0, 1, 1.0)

        # attr: font, text, pos, color=Color.GREEN
        self.fps_pos = Vector2(15, 15)
        self.fps_text_content = '...'
        self.fps_text = Text(self.fps_text_content,
                             self.fps_font, 14)

        self.fps = FPS(self.fps_text,
                       self.fps_pos)

        # PLAYER
        self.player_spt = Sprite(self.player_texture)
        self.player = Player(self.window,
                             self.player_spt,
                             self.player_texture, 0, 0)

        self.bg = Sprite(self.bg)

    def run(self):
        # zoom = 1
        while self.window.is_open:
            for event in self.window.events:
                if type(event) is KeyEvent:
                    if event.pressed:
                        if event.code is Keyboard.ESCAPE:
                            self.window.close()

                # close evt
                if type(event) is CloseEvent:
                    self.window.close()

            dt = self.clock.elapsed_time
            self.clock.restart()

            # self.view.center = self.player_spt.position

            self.fps_text_content = 'FPS: %s' % (1.0/dt.seconds)
            self.fps_text.string = self.fps_text_content

            self.update(dt)
            self.draw()

    def update(self, delta):
        self.player.update(delta)

    def draw(self):
        self.window.clear()
        #
        self.window.view = self.view
        self.window.draw(self.bg)
        self.player.draw(self.window)

        self.window.view = self.hud

        self.fps.draw(self.window)
        self.window.display()

    def load_content(self):
        try:
            self.icon = self.asset('I', asset['icon'])
            self.player_texture = self.asset('S', asset['mario'])
            self.bg = self.asset('S', asset['bg'])
            self.fps_font = self.asset('F', asset['fps_font'])

        except Exception as e:
            alert('Error loading file', e)
            exit(1)

    def asset(self, category, name):
        # M : Music [ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
        # w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64.]
        if category is 'M':
            return Music.from_file(name)

        # F : Font [TrueType, Type 1, CFF, OpenType, SFNT, X11 PCF,
        # Windows FNT, BDF, PFR and Type 42]
        if category is 'F':
            return Font.from_file(name)

        # S : Sprite [bmp, png, tga, jpg, gif, psd, hdr and pic]
        if category is 'S':
            return Texture.from_file(name)

        if category is 'I':
            return Image.from_file(name)