class tester: def __init__(self): self.ui = UI(self) def update(self, screen): self.ui.draw_text("The quick brown fox jumps over the lazy dog.", location=(10, 75)) screen.lock() self.ui.draw_circle((50, 200), 30) self.ui.draw_rect((50, 300), (60, 60)) screen.unlock() def setup(self): self.ui.add_button("New button", self.button_handler) self.ui.add_input("New input", self.button_handler, location=(10, 40)) def handle(self, event): pass def button_handler(self, btn): pass def start(self): self.ui.start()
class GameOver(State): def __init__(self, score): State.__init__(self) self.ui = UI(self, Jules_UIContext) self.nextState = lambda: SplashScreen(current=1) self.eventid = TimerEvents.GameOver self.score = score self.countdown = 5 * 1000 for key, (name, value) in sorted(HighScores.high_scores.items()): if score > value: self.replace = key break else: self.replace = None def start(self): if self.replace == None: TimerEvents().start(self.eventid, self.countdown) State.start(self) def handle(self, event): if event.type == self.eventid: self.transition() def transition(self): TimerEvents().stop(self.eventid) State.transition(self) def input_text(self, text): new_scores = {} text = text.upper() old_scores = sorted(HighScores.high_scores.keys()) index = old_scores.index(self.replace) for key in old_scores[:index]: new_scores[key] = HighScores.high_scores[key] new_scores[self.replace] = (text, self.score) for index in xrange(index + 1, len(HighScores.high_scores)): new_scores[old_scores[index]] = HighScores.high_scores[old_scores[index - 1]] HighScores.high_scores = new_scores high_scores.save() self.transition() def setup(self, screen): if self.replace == None: pass else: size = (75, 50) location = ((W/2) - (size[0] / 2), 6 * H/10) with self.ui.newcontext(UIContext(font_size=30, len_cap=3)): self.ui.add_input(screen, "___", lambda text: self.input_text(text), location=location, size=size) def update(self, screen): if self.replace == None: self.ui.draw_text(screen, "Game Over", (W/2, H/10), align=0) self.ui.draw_text(screen, "Your Score: " + str(self.score), (W/2, 3 * H/10), align=0) else: self.ui.draw_text(screen, "Game Over", (W/2, H/10), align=0) self.ui.draw_text(screen, "New High Score!", (W/2, 3*H/10), align=0) self.ui.draw_text(screen, "Your Score: " + str(self.score), (W/2, 4 * H/10), align=0) self.ui.draw_text(screen, "Enter your initials:", (W/2, 5 * H/10), align=0)
class GameOver(State): def __init__(self, score): State.__init__(self) self.ui = UI(self, Jules_UIContext) self.nextState = lambda: SplashScreen(current=1) self.eventid = TimerEvents.GameOver self.score = score self.countdown = 5 * 1000 for key, (name, value) in sorted(HighScores.high_scores.items()): if score > value: self.replace = key break else: self.replace = None def start(self): if self.replace == None: TimerEvents().start(self.eventid, self.countdown) State.start(self) def handle(self, event): if event.type == self.eventid: self.transition() def transition(self): TimerEvents().stop(self.eventid) State.transition(self) def input_text(self, text): new_scores = {} text = text.upper() old_scores = sorted(HighScores.high_scores.keys()) index = old_scores.index(self.replace) for key in old_scores[:index]: new_scores[key] = HighScores.high_scores[key] new_scores[self.replace] = (text, self.score) for index in xrange(index + 1, len(HighScores.high_scores)): new_scores[old_scores[index]] = HighScores.high_scores[old_scores[ index - 1]] HighScores.high_scores = new_scores high_scores.save() self.transition() def setup(self, screen): if self.replace == None: pass else: size = (75, 50) location = ((W / 2) - (size[0] / 2), 6 * H / 10) with self.ui.newcontext(UIContext(font_size=30, len_cap=3)): self.ui.add_input(screen, "___", lambda text: self.input_text(text), location=location, size=size) def update(self, screen): if self.replace == None: self.ui.draw_text(screen, "Game Over", (W / 2, H / 10), align=0) self.ui.draw_text(screen, "Your Score: " + str(self.score), (W / 2, 3 * H / 10), align=0) else: self.ui.draw_text(screen, "Game Over", (W / 2, H / 10), align=0) self.ui.draw_text(screen, "New High Score!", (W / 2, 3 * H / 10), align=0) self.ui.draw_text(screen, "Your Score: " + str(self.score), (W / 2, 4 * H / 10), align=0) self.ui.draw_text(screen, "Enter your initials:", (W / 2, 5 * H / 10), align=0)