def create_starscape(self): frame = Frame(self.screen, Rect(0, 0, 800, 600)) self.starscape = StarScape(frame)
class SpaceRaceGame: def __init__(self, screen): self.screen = screen self.tile_factory = TileFactory() self.starscape = None self.level = None self.spaceship = None self.status_box = None self.events = None self.score = 0 self.create_starscape() self.create_level() self.create_status_box() self.update_mode = self.update_ingame self.move_delay = MOVE_DELAY self.delay = MOVE_DELAY def create_starscape(self): frame = Frame(self.screen, Rect(0, 0, 800, 600)) self.starscape = StarScape(frame) def create_level(self): frame = Frame(self.screen, Rect(10, 10, 640, 512)) self.level = SpaceshipLevel(frame, self.tile_factory) self.spaceship = SpaceshipSprite(frame, self.tile_factory, Vector(1, 1), self.level) def create_status_box(self): frame = Frame(self.screen, Rect(660, 20, 200, 50)) self.status_box = DictBox(frame, {'score': 0}) def update_finish_moves(self): """finish movements before Game Over""" if not self.spaceship.is_moving(): pygame.display.update() time.sleep(1) self.events.exit_signalled() def update_ingame(self): self.delay -= 1 if self.delay <= 0: self.delay = self.move_delay self.spaceship.move_forward() if self.spaceship.crashed: self.update_mode = self.update_finish_moves self.score = 0 if self.spaceship.finished: self.update_mode = self.update_finish_moves self.score = 1000 def draw(self): self.update_mode() self.screen.clear() self.starscape.draw() self.level.draw() self.spaceship.draw() self.status_box.draw() pygame.display.update() self.starscape.scroll() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.spaceship.set_direction)) self.events.add_listener(ExitListener(self.events.exit_signalled)) with draw_timer(self, self.events): self.events.event_loop()