def __init__(self): super(Main, self).__init__(width=1024, height=600, resizable=True, caption='Matching Hearts') self.set_minimum_size(320, 200) # does not work on linux with compiz self.set_fullscreen() self.set_mouse_visible(True) self.set_icon(pyglet.image.load( os.path.join(pyglet.resource.location('MessageHeart.png').path, 'MessageHeart.png'))) self.game = Game() self.high_score = HighScores('hearts.score', self.game.modes) self.setState(self.SCORE) self.fps_display = pyglet.clock.ClockDisplay() self.fps_display.label.y = self.height - 50 self.fps_display.label.x = self.width - 170
class Main(pyglet.window.Window): fps_display = None SCORE = object() PLAYING = object() START = object() def __init__(self): super(Main, self).__init__(width=1024, height=600, resizable=True, caption='Matching Hearts') self.set_minimum_size(320, 200) # does not work on linux with compiz self.set_fullscreen() self.set_mouse_visible(True) self.set_icon(pyglet.image.load( os.path.join(pyglet.resource.location('MessageHeart.png').path, 'MessageHeart.png'))) self.game = Game() self.high_score = HighScores('hearts.score', self.game.modes) self.setState(self.SCORE) self.fps_display = pyglet.clock.ClockDisplay() self.fps_display.label.y = self.height - 50 self.fps_display.label.x = self.width - 170 def on_draw(self): self.clear() if self.state is self.START: self.game.start(self.game.level) self.high_score.mode = self.game.mode self.setState(self.PLAYING) elif self.state is self.SCORE: with gl_matrix(): gl.glTranslatef(window.width / 2, window.height // 2, 0) self.high_score.draw() else: self.game.draw() if self.fps_display: self.fps_display.draw() def run(self): pyglet.app.run() def on_key_press(self, symbol, modifiers): if symbol == key.ESCAPE: if self.state is not self.SCORE: self.setState(self.SCORE) else: self.dispatch_event('on_close') if symbol == key.F: self.set_fullscreen(not self.fullscreen) if symbol == key.PLUS or symbol == key.EQUAL: self.game.start(min(self.game.level + 1, 4)) self.high_score.mode = self.game.mode self.high_score.generate_scores() if symbol == key.MINUS: self.game.start(max(self.game.level - 1, 0)) self.high_score.mode = self.game.mode self.high_score.generate_scores() if symbol == key.ASCIITILDE: self.game.hearts = [] if symbol == key.F: self.set_fullscreen(not self.fullscreen) def on_resize(self, width, height): if self.fps_display: self.fps_display.label.y = self.height - 50 self.fps_display.label.x = self.width - 170 super(Main, self).on_resize(width, height) def setState(self, state): if state is self.SCORE and self.high_score.current_score: self.focus = self.high_score.widget self.focus.caret.visible = True else: self.focus = None self.state = state def on_mouse_release(self, *args): if self.state is self.SCORE: self.setState(self.START) else: state = self.game.on_mouse_release(*args) if state is self.SCORE: self.high_score.set_score(self.game.score) self.setState(state) def on_text(self, text): if self.focus: if text in ['\r', '\n']: self.high_score.saveScore(self.focus.document.text) self.focus.document.text = '' self.focus = None return else: self.focus.caret.on_text(text) def on_text_motion(self, motion): if self.focus: self.focus.caret.on_text_motion(motion) def on_text_motion_select(self, motion): if self.focus: self.focus.caret.on_text_motion_select(motion)