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 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)
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, 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
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"
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 __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 __init__(self, controller): super(TitleScreen, self).__init__(controller) self.next = "lobby" self.screen_rect = pg.Rect((0, 0), constants.RENDER_SIZE) self.title = constants.GFX["collab_title"] cent_x = self.screen_rect.centerx self.title_rect = self.title.get_rect(centerx=cent_x, y=100) anykey_args = (constants.FONTS["Fixedsys500c"], 30, "[Please Insert Coin]", pg.Color("gold"), {"center" : (cent_x, 650)}, 350) self.anykey = FlashingText(*anykey_args)
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 __init__(self, title, controller): super(AnyKey, 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, 625), "[Press Any Key]", "Fixedsys500c", pg.Color("white"), 50, 350) self.title = Label(prepare.FONTS["Fixedsys500c"], 100, title, pg.Color("white"), { "centerx": cent_x, "y": 50 }) self.screen_copy = None
def make_high_scores_table(self, high_scores, score): font = prog_constants.FONTS["Fixedsys500c"] color = constants.LOW_LIGHT_GREEN left1, left2, top = 350, 500, 100 placed = False self.name_label = None self.flasher = None self.textbox = None for i, info in enumerate(high_scores): name, high_score = info name_label = Label(font, 32, name, color, {"topleft": (left1, top)}) if high_score == score and not placed and not name: placed = True label = FlashingText(font, 32, "{}".format(high_score), color, {"topleft": (left2, top)}, 500) self.flasher = label self.name_label = name_label self.name_index = i validator = lambda x: len(x) == 3 box_size = (96, 32) self.textbox = Textbox2({"topleft": (left1 - 4, top + 2)}, call=self.enter_name, box_size=box_size, validator=validator, font_path=font) else: label = Label(font, 32, "{}".format(high_score), color, {"topleft": (left2, top)}) self.labels.extend([label, name_label]) top += 40 self.high_scores = high_scores
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)