class TitleScreen(_State): """ Initial state of the game. """ def __init__(self, controller): super(TitleScreen, self).__init__(controller) self.next = "lobby" self.screen_rect = pg.Rect((0, 0), prepare.RENDER_SIZE) self.title = prepare.GFX["collab_title"] cent_x = self.screen_rect.centerx self.title_rect = self.title.get_rect(centerx=cent_x, y=100) self.anykey = FlashingText((cent_x, 650), "[Please Insert Coin]", "Fixedsys500c", pg.Color("gold"), 30, 350) def get_event(self, event, scale): if event.type == pg.QUIT: self.done = True self.quit = True elif event.type == pg.KEYUP: self.done = True if event.key == pg.K_ESCAPE: self.quit = True def update(self, surface, keys, current_time, dt, scale): self.anykey.update(current_time) self.draw(surface) def draw(self, surface): surface.fill(prepare.BACKGROUND_BASE) surface.blit(self.title, self.title_rect) surface.blit(self.anykey.image, self.anykey.rect)
class HighScores(_State): """ Shown by clicking the high scores button in the lobby page. """ def __init__(self, controller): super(HighScores, self).__init__(controller) self.next = None self.screen_rect = pg.Rect((0, 0), prepare.RENDER_SIZE) cent_x = self.screen_rect.centerx self.anykey = FlashingText((cent_x, 650), "[Press Any Key]", "Fixedsys500c", pg.Color("gold"), 30, 350) text = "Under Construction" self.title = Label(prepare.FONTS["Fixedsys500c"], 72, text, pg.Color("white"), {"center": self.screen_rect.center}) def update(self, surface, keys, current_time, dt, scale): """ Updates the highcore screen. """ self.anykey.update(current_time) self.draw(surface) def draw(self, surface): surface.fill(prepare.BACKGROUND_BASE) self.title.draw(surface) surface.blit(self.anykey.image, self.anykey.rect) def get_event(self, event, scale): if event.type == pg.QUIT: self.done = True self.quit = True elif event.type == pg.KEYUP: self.done = True self.next = "lobby"
class Scene(_State): """ This State is updated while our game is running. The game autodetection requires that the name of this class not be changed. """ def __init__(self, controller): super(Scene, self).__init__(controller) self.next = None self.screen_rect = pg.Rect((0, 0), prepare.RENDER_SIZE) cent_x = self.screen_rect.centerx self.anykey = FlashingText((cent_x, 650), "[Press Any Key]", "Fixedsys500c", pg.Color("gold"), 30, 350) self.title = Label(prepare.FONTS["Fixedsys500c"], 72, "Your game here!", pg.Color("white"), {"center": self.screen_rect.center}) def startup(self, persistent): """ This method will be called each time the state resumes. """ self.start_time = pg.time.get_ticks() self.persist = persistent def cleanup(self): """ Add variables that should persist to the self.persist dictionary. Then reset State.done to False. """ self.done = False return self.persist def update(self, surface, keys, current_time, dt, scale): """ Updates the game scene and then draws the screen screen. """ self.anykey.update(current_time) self.draw(surface) def draw(self, surface): """ Put all drawing logic here. Called at the end of the update method. """ surface.fill(prepare.BACKGROUND_BASE) self.title.draw(surface) surface.blit(self.anykey.image, self.anykey.rect) def get_event(self, event, scale): """ Process all events here. States must not have their own embedded event loops as this cuts the rest of the program off from events. If you would like to use mouse position events you will need to scale it with scaled_mouse_pos found in data.core.tools.py. """ if event.type == pg.QUIT: self.done = True self.quit = True elif event.type == pg.KEYUP: self.done = True self.next = "lobby"
class Credits(_State): """ Shown by clicking the credits button in the lobby page. """ def __init__(self, controller): super(Credits, self).__init__(controller) self.next = None self.screen_rect = pg.Rect((0, 0), prepare.RENDER_SIZE) cent_x = self.screen_rect.centerx self.anykey = FlashingText((cent_x, 650), "[Press Any Key]", "Fixedsys500c", pg.Color("gold"), 30, 350) self.titles = [] names = ["/u/mekire", "/u/bitcraft", "/u/iminurnamez"] for i, name in enumerate(names, start=-1): text = "Some stuff by {}".format(name) self.titles.append( Label( prepare.FONTS["Fixedsys500c"], 48, text, pg.Color("white"), { "centerx": self.screen_rect.centerx, "centery": self.screen_rect.centery + i * 80 })) self.titles.append( Label( prepare.FONTS["Fixedsys500c"], 48, "Your Name Here", pg.Color("white"), { "centerx": self.screen_rect.centerx, "centery": self.screen_rect.centery + (i + 1) * 80 })) def startup(self, current_time, persistent): """ This method will be called each time the state resumes. """ self.start_time = current_time self.persist = persistent def update(self, surface, keys, current_time, dt, scale): """ Updates the credit screen. """ self.anykey.update(current_time) self.draw(surface) def draw(self, surface): surface.fill(prepare.BACKGROUND_BASE) for title in self.titles: title.draw(surface) surface.blit(self.anykey.image, self.anykey.rect) def get_event(self, event, scale): if event.type == pg.QUIT: self.done = True self.quit = True elif event.type == pg.KEYUP: self.done = True self.next = "lobby"
class Scene(_State): """ This State is updated while our game is running. The game autodetection requires that the name of this class not be changed. """ def __init__(self, controller): super(Scene, self).__init__(controller) self.next = None self.screen_rect = pg.Rect((0, 0), prepare.RENDER_SIZE) cent_x = self.screen_rect.centerx self.anykey = FlashingText((cent_x, 650), "[Press Any Key]", "Fixedsys500c", pg.Color("gold"), 30, 350) self.title = Label(prepare.FONTS["Fixedsys500c"], 72, "Your game here!", pg.Color("white"), {"center": self.screen_rect.center}) def startup(self, current_time, persistent): """ This method will be called each time the state resumes. """ self.start_time = current_time self.persist = persistent def update(self, surface, keys, current_time, dt, scale): """ Updates the game scene screen. """ self.anykey.update(current_time) self.draw(surface) def draw(self, surface): surface.fill(prepare.BACKGROUND_BASE) self.title.draw(surface) surface.blit(self.anykey.image, self.anykey.rect) def get_event(self, event, scale): if event.type == pg.QUIT: self.done = True self.quit = True elif event.type == pg.KEYUP: self.done = True self.next = "lobby"
class AnyKey(_State): """ A state for the start and death scene. """ def __init__(self, title, controller): super(AnyKey, self).__init__(controller) self.next = None self.screen_rect = pg.Rect((0, 0), prog_consts.RENDER_SIZE) cent_x = self.screen_rect.centerx anykey_args = (prog_consts.FONTS["Fixedsys500c"], 50, "[Press Any Key]", pg.Color("white"), { "center": (cent_x, 625) }, 350) self.anykey = FlashingText(*anykey_args) self.title = Label(prog_consts.FONTS["Fixedsys500c"], 100, title, pg.Color("white"), { "centerx": cent_x, "y": 50 }) self.screen_copy = None def draw(self, surface): if self.screen_copy: surface.blit(self.screen_copy, (0, 0)) else: surface.fill(prog_consts.BACKGROUND_BASE) self.title.draw(surface) surface.blit(self.anykey.image, self.anykey.rect) def update(self, surface, keys, current_time, dt, scale): self.anykey.update(current_time) self.draw(surface) def get_event(self, event, scale): """ Switch to game on keydown. """ if event.type == pg.KEYDOWN: self.done = True self.next = "GAME"
class TitleScreen(_State): def __init__(self, controller): super(TitleScreen, self).__init__(controller) def startup(self, persistent): self.persist = persistent self.labels = pg.sprite.Group() font = prog_constants.FONTS["Fixedsys500c"] sr = constants.SCREEN_RECT self.missiles = pg.sprite.Group() self.all_explosions = pg.sprite.Group() self.explosions = pg.sprite.Group() self.buildings = pg.sprite.Group() left, bottom = 50, 500 for x in "Missiles": label = Label(font, 192, x, constants.LOW_LIGHT_GREEN, {"bottomleft": (left, bottom)}) img = label.image damaged_label = Label(font, 192, x, (166, 0, 33), {"bottomleft": (left, bottom)}) damaged_img = damaged_label.image width = img.get_width() MockCity((left + (width // 2), bottom), img, damaged_img, self.buildings) left += width self.blinker = FlashingText(font, 50, "[Press Any Key]", pg.Color("white"), {"center": (sr.centerx, 625)}, 350) self.missile_speed = .12 self.missile_timer = 0 self.missile_frequency = 200 def add_missile(self): origin = randint(0, constants.SCREEN_SIZE[0]), -20 target = randint( 0, constants.SCREEN_SIZE[0]), constants.SCREEN_SIZE[1] - 20 m = Missile(origin, target, self.missile_speed, 20, .1) self.missiles.add(m) def start_game(self, *args): self.done = True self.next = "LEVEL_START" player = load_player() self.persist["player"] = player self.persist["level"] = Level(player) def get_event(self, event, scale): if event.type == pg.QUIT: self.quit = True elif event.type == pg.KEYDOWN: if event.key == pg.K_ESCAPE: self.quit = True else: self.start_game() elif event.type == pg.MOUSEBUTTONUP: self.start_game() def update(self, surface, keys, current_time, dt, scale): self.blinker.update(current_time) self.missile_timer += dt if self.missile_timer >= self.missile_frequency: self.add_missile() self.missile_timer -= self.missile_frequency self.explosions.update(dt) self.missiles.update(dt, self.all_explosions, self.explosions, self.buildings) self.buildings.update(dt, self.explosions) self.draw(surface) def draw(self, surface): surface.fill(constants.BACKGROUND_BASE) self.buildings.draw(surface) for m in self.missiles: m.draw(surface) self.explosions.draw(surface) self.blinker.draw(surface)