def startup(self, persist: dict): self.players = pygame.sprite.Group() self.player_1 = pygame.sprite.Sprite() self.player_2 = pygame.sprite.Sprite() self.player_1_confirm = False self.player_2_confirm = False self.transition = Transition() self.done = False self.time_left = 20 # Reset p1_ship_images since they haven't indicated they want to play yet self.p1_ship_image: pygame.Surface = None self.p1_ship_rect: pygame.Rect = None self.p2_ship_image: pygame.Surface = None self.p2_ship_rect: pygame.Rect = None self.persist = persist self.choice = self.persist[ 'choice'] # This denotes which player chose to start the game if self.choice['1p']: self.set_player_1() if self.choice['2p']: self.set_player_2() self.controls = self.persist['controls'] self.coins = self.persist['coins'] # Should always be done after controls initialized in "level" state where player actually starts giving input pygame.mixer.music.load(MUSIC['07_-_stars_dont_twinkle_0']) pygame.mixer.music.play(-1)
def __init__(self): super().__init__() self.background = GFX['controls'] self.transition = Transition() self.next = 'TITLE' self.fade_away = False self.labels = pygame.sprite.Group() self.label_list: List[BlinkerLabel] = [] self.hints = [ 'Arrow Keys To Navigate / Enter To Unlock', 'Assign Your Own Key / Enter To Confirm' ] self.hint_1 = Label('Esc to return', {'center': (300, 750)}, self.labels, font_path=GAMER, font_size=25) self.hint_2 = Label(self.hints[0], {'center': (300, 780)}, self.labels, font_path=GAMER, font_size=25) self.changing_label = False self.chosen_control = 0 self.texts = [ '1p_coin', '1p_start', '1p_up', '1p_down', '1p_left', '1p_right', '1p_button_a', '1p_button_b', '2p_coin', '2p_start', '2p_up', '2p_down', '2p_left', '2p_right', '2p_button_a', '2p_button_b' ]
def __init__(self): super().__init__() self.choice: Dict[str, int] = {'1p': 0, '2p': 0} self.controls: Dict[str, int] = {} self.player_1: Player = pygame.sprite.Sprite() self.player_2: Player = pygame.sprite.Sprite() self.players = pygame.sprite.Group() self.labels = pygame.sprite.Group() self.enemy_hitboxes = pygame.sprite.Group() # For showcase weapon self.player_1_confirm = False self.player_2_confirm = False self.next = 'LEVEL 1' self.time_left = 20 self.p1_ship_img: cycle = None # Selecting between the two self.p2_ship_img: cycle = None self.p1_star_img: cycle = None self.p2_star_img: cycle = None self.p1_name_img: cycle = None self.p2_name_img: cycle = None self.p1_ship_img_rects: cycle = None self.p2_ship_img_rects: cycle = None self.p1_ship_sprites: cycle = None self.p2_ship_sprites: cycle = None self.p1_power_image: pygame.Surface = None self.p1_power_rect: pygame.Rect = None self.p1_speed_image: pygame.Surface = None self.p1_speed_rect: pygame.Rect = None self.p1_name_image: pygame.Surface = None self.p1_name_rect: pygame.Rect = None self.p2_power_image: pygame.Surface = None self.p2_power_rect: pygame.Rect = None self.p2_speed_image: pygame.Surface = None self.p2_speed_rect: pygame.Rect = None self.p2_name_image: pygame.Surface = None self.p2_name_rect: pygame.Rect = None self.p1_ship_image: pygame.Surface = None self.p1_ship_rect: pygame.Rect = None self.p2_ship_image: pygame.Surface = None self.p2_ship_rect: pygame.Rect = None self.mask: pygame.Surface = None self.background = Background(3) self.transition = Transition() self.time_label: BlinkerLabel = None self.coin_label: Label = None self.load_images() self.load_labels()
def startup(self, persist: dict): self.labels = pygame.sprite.Group() self.persist = persist self.changing_label = False self.done = False self.controls = self.persist['controls'] self.chosen_control = 0 self.transition = Transition() self.fade_away = False self.init_labels()
def startup(self, persist: dict): self.frame = 0 self.done = False self.persist = persist self.text_input.reset() self.scoreboard = persist['scoreboard'] self.check_done() self.fade_away = False self.transition = Transition() self.event_block = False self.prompt_label = Label('', { 'center': (300, 200)}, font_path=ARCADE_CLASSIC, font_size=40)
def startup(self, persist: dict): self.frame = 0 self.done = False self.transition = Transition() self.fade_away = False self.persist = persist with open(SCORE, 'r') as f: self.scoreboard = self.persist[ 'scoreboard'] if 'scoreboard' in self.persist else json.load(f) self.labels = pygame.sprite.Group() self.init_labels() self.controls = self.persist['controls']
def update(self): self.frame += 1 self.screen_saver += 1 self.transition.fade_in() # If we have waited for 5 seconds, not transitioning if self.screen_saver == 300 and not self.fade_away: self.fade_away = True self.next = 'HIGHSCORE' if self.fade_away: self.done = self.transition.fade_out() elif self.transition.frame > 0: self.transition = Transition() # Reset fadeaway self.update_labels()
def __init__(self): super().__init__() self.background = Background(5) self.labels = pygame.sprite.Group() self.scoreboard: dict = None self.title = GFX['scoreboard'] self.title_rect = self.title.get_rect(center=(300, 100)) self.transition = Transition() self.next = 'TITLE' self.fade_away = False self.images = [ pygame.transform.scale(GFX[f'player{x}'], (40, 60)) for x in range(1, 5) ]
def startup(self, persist: dict): self.done = False self.next = 'SELECT' self.persist = persist self.choice = self.persist.get('choice', {'1p': 0, '2p': 0}) self.coins = self.persist.get('coins', 0) self.frame = 0 self.screen_saver = 0 pygame.mixer.music.load(MUSIC['06_-_space_troopers_0']) pygame.mixer.music.play(-1) # -1 means looping music with open(CONTROLS, 'r') as f: self.controls = self.persist.get('controls', json.load(f)) self.transition = Transition() self.fade_away = False self.event_block = False
def __init__(self): super().__init__() self.next = 'TITLE' self.bg = GFX['warning'] self.timer = 300 self.frame = 0 self.transition: Transition = Transition()
def __init__(self): super().__init__() self.background = Background(1) self.text_input = TextInput(font_family=ARCADE_CLASSIC, font_size=50, text_color=(255, 255, 255), cursor_color=(255, 255, 255), max_length=15) self.next = 'HIGHSCORE' self.scoreboard = {} self.p1_done = False self.p2_done = False self.event_block = False self.p1_idx = 0 self.p2_idx = 0 self.fade_away = False self.transition = Transition() self.title = GFX['entername'] self.title_rect = self.title.get_rect(center=(300, 100)) self.prompt_label: Label = None
def __init__(self): super().__init__() self.background = GFX['title_screen'] self.coins = 0 self.fade_away = False self.event_block = False self.transition = Transition() self.controls: dict = None self.coin_label: Label = None self.control_label: Label = None self.hint_label: BlinkerLabel = None self.labels = pygame.sprite.Group() self.make_labels() self.choice = {'1p': 0, '2p': 0} self.screen_saver = 0
class Control(State): def __init__(self): super().__init__() self.background = GFX['controls'] self.transition = Transition() self.next = 'TITLE' self.fade_away = False self.labels = pygame.sprite.Group() self.label_list: List[BlinkerLabel] = [] self.hints = [ 'Arrow Keys To Navigate / Enter To Unlock', 'Assign Your Own Key / Enter To Confirm' ] self.hint_1 = Label('Esc to return', {'center': (300, 750)}, self.labels, font_path=GAMER, font_size=25) self.hint_2 = Label(self.hints[0], {'center': (300, 780)}, self.labels, font_path=GAMER, font_size=25) self.changing_label = False self.chosen_control = 0 self.texts = [ '1p_coin', '1p_start', '1p_up', '1p_down', '1p_left', '1p_right', '1p_button_a', '1p_button_b', '2p_coin', '2p_start', '2p_up', '2p_down', '2p_left', '2p_right', '2p_button_a', '2p_button_b' ] def startup(self, persist: dict): self.labels = pygame.sprite.Group() self.persist = persist self.changing_label = False self.done = False self.controls = self.persist['controls'] self.chosen_control = 0 self.transition = Transition() self.fade_away = False self.init_labels() def cleanup(self): persist = {'controls': self.controls, 'coins': self.persist['coins']} return persist def init_labels(self): self.label_list: List[BlinkerLabel] = [] label_centers = [(150 + x * 300, 360 + y * 50) for x in range(2) for y in range(8)] for name, center in zip(self.texts, label_centers): self.label_list.append( BlinkerLabel(pygame.key.name(self.controls[name]), {'center': center}, 30, self.labels, font_path=ARCADE_CLASSIC, font_size=25)) self.hint_1 = Label('Esc to return', {'center': (300, 750)}, self.labels, font_path=GAMER, font_size=25) self.hint_2 = Label(self.hints[0], {'center': (300, 780)}, self.labels, font_path=GAMER, font_size=25) def update(self): #print(self.chosen_control) self.transition.fade_in() self.update_labels() if self.fade_away: self.done = self.transition.fade_out() def draw(self, surface): surface.blit(self.background, (0, 0)) self.labels.draw(surface) self.transition.draw(surface) def update_labels(self): for idx, label in enumerate(self.label_list): if idx == self.chosen_control: if self.changing_label: label.fill_colour = (255, 255, 0) label.text_colour = (0, 0, 0) label.blink() else: label.fill_colour = (255, 255, 255) label.text_colour = (0, 0, 0) label.text = label.original_text else: label.fill_colour = None label.text_colour = (255, 255, 255) label.update_img() self.hint_2.update_text(self.hints[self.changing_label]) def update_control(self, key): selected_label = self.label_list[self.chosen_control] name = pygame.key.name(key) selected_label.original_text = name self.controls[self.texts[self.chosen_control]] = key def event_process(self, events: List[pygame.event.Event]): for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: # Save controls with open(CONTROLS, 'w') as f: json.dump(self.controls, f) self.fade_away = True if not self.changing_label: if event.key == pygame.K_RETURN: self.changing_label = True if event.key == pygame.K_UP: if self.chosen_control % 8 > 0: self.chosen_control -= 1 if event.key == pygame.K_DOWN: if self.chosen_control % 8 < 7: self.chosen_control += 1 if event.key == pygame.K_RIGHT: if self.chosen_control < 8: # Left column self.chosen_control += 8 if event.key == pygame.K_LEFT: if self.chosen_control >= 8: self.chosen_control -= 8 else: if event.key == pygame.K_RETURN: self.changing_label = False elif event.key == pygame.K_ESCAPE: pass else: self.update_control(event.key)
class Select(State): showcase_weapon_level = 5 def __init__(self): super().__init__() self.choice: Dict[str, int] = {'1p': 0, '2p': 0} self.controls: Dict[str, int] = {} self.player_1: Player = pygame.sprite.Sprite() self.player_2: Player = pygame.sprite.Sprite() self.players = pygame.sprite.Group() self.labels = pygame.sprite.Group() self.enemy_hitboxes = pygame.sprite.Group() # For showcase weapon self.player_1_confirm = False self.player_2_confirm = False self.next = 'LEVEL 1' self.time_left = 20 self.p1_ship_img: cycle = None # Selecting between the two self.p2_ship_img: cycle = None self.p1_star_img: cycle = None self.p2_star_img: cycle = None self.p1_name_img: cycle = None self.p2_name_img: cycle = None self.p1_ship_img_rects: cycle = None self.p2_ship_img_rects: cycle = None self.p1_ship_sprites: cycle = None self.p2_ship_sprites: cycle = None self.p1_power_image: pygame.Surface = None self.p1_power_rect: pygame.Rect = None self.p1_speed_image: pygame.Surface = None self.p1_speed_rect: pygame.Rect = None self.p1_name_image: pygame.Surface = None self.p1_name_rect: pygame.Rect = None self.p2_power_image: pygame.Surface = None self.p2_power_rect: pygame.Rect = None self.p2_speed_image: pygame.Surface = None self.p2_speed_rect: pygame.Rect = None self.p2_name_image: pygame.Surface = None self.p2_name_rect: pygame.Rect = None self.p1_ship_image: pygame.Surface = None self.p1_ship_rect: pygame.Rect = None self.p2_ship_image: pygame.Surface = None self.p2_ship_rect: pygame.Rect = None self.mask: pygame.Surface = None self.background = Background(3) self.transition = Transition() self.time_label: BlinkerLabel = None self.coin_label: Label = None self.load_images() self.load_labels() def load_images(self): g = lambda key: GFX[key] r = lambda x, y: pygame.Rect(x, y, 70, 105) self.p1_ship_img = cycle((g('p1_1'), g('p1_2'))) self.p2_ship_img = cycle((g('p2_1'), g('p2_2'))) self.p1_star_img = cycle((g('star_r_1'), g('star_r_2'))) self.p2_star_img = cycle((g('star_b_1'), g('star_b_2'))) self.p1_name_img = cycle((g('name_r_1'), g('name_r_2'))) self.p2_name_img = cycle((g('name_b_1'), g('name_b_2'))) self.p1_ship_img_rects = cycle( (r(150 - 75 - 35, 470 + 45 - 52.5), r(150 + 75 - 35, 470 + 45 - 52.5))) # 150 is ship middle,75 offset for ship center, 35 offset for left side. # 470 is small ship top, 45 offset for ship center, 52.5 offset for top side self.p2_ship_img_rects = cycle( (r(450 - 75 - 35, 470 + 45 - 52.5), r(450 + 75 - 35, 470 + 45 - 52.5))) def p(id, x_pos): p = Player(self, id, PVector(x_pos, 400)) p.kill( ) # To avoid drawing multiple ships on top of each other, should kill when they are not active p.weapon_level = self.showcase_weapon_level return p self.p1_ship_sprites = cycle((p(1, 150), p(3, 150))) self.p2_ship_sprites = cycle((p(2, 450), p(4, 450))) # Player centerx for p1 is 150. # This means that centerx for name should also be 150. # So the left of name is 150-70/2=115 # Similar logic for power and speed self.p1_power_image = g('star_r_0') # ????? picture self.p1_power_rect = pygame.Rect(102, 670, 97, 17) self.p1_speed_image = g('star_r_0') # ????? picture self.p1_speed_rect = pygame.Rect(102, 730, 97, 17) self.p1_name_image = g('name_blank') self.p1_name_rect = pygame.Rect(115, 580, 70, 60) self.p2_power_image = g('star_b_0') # ????? picture self.p2_power_rect = pygame.Rect(402, 670, 97, 17) self.p2_speed_image = g('star_b_0') # ????? picture self.p2_speed_rect = pygame.Rect(402, 730, 97, 17) self.p2_name_image = g('name_blank') self.p2_name_rect = pygame.Rect(415, 580, 70, 60) self.mask = g('player_select_gimp') def load_labels(self): self.time_label = BlinkerLabel(str(self.time_left), {'midbottom': (300, 160)}, 30, self.labels, font_path=GAMER, font_size=50) self.coin_label = Label(f'credit {self.coins}', {'midbottom': (300, 780)}, self.labels, font_path=ARCADE_CLASSIC, font_size=30) # self.debug_label = Label(f'ABCDEFGHIJKLMNOPQRSTUVWXYZ', {'center': (300, 400)}, self.labels, # font_path=ANCIENT_MEDIUM, font_size=30) def startup(self, persist: dict): self.players = pygame.sprite.Group() self.player_1 = pygame.sprite.Sprite() self.player_2 = pygame.sprite.Sprite() self.player_1_confirm = False self.player_2_confirm = False self.transition = Transition() self.done = False self.time_left = 20 # Reset p1_ship_images since they haven't indicated they want to play yet self.p1_ship_image: pygame.Surface = None self.p1_ship_rect: pygame.Rect = None self.p2_ship_image: pygame.Surface = None self.p2_ship_rect: pygame.Rect = None self.persist = persist self.choice = self.persist[ 'choice'] # This denotes which player chose to start the game if self.choice['1p']: self.set_player_1() if self.choice['2p']: self.set_player_2() self.controls = self.persist['controls'] self.coins = self.persist['coins'] # Should always be done after controls initialized in "level" state where player actually starts giving input pygame.mixer.music.load(MUSIC['07_-_stars_dont_twinkle_0']) pygame.mixer.music.play(-1) def cleanup(self): pygame.mixer.music.fadeout(500) persist = { 'coins': self.coins, 'controls': self.controls, 'choice': self.choice } return persist def update(self): self.transition.fade_in() # self.fade_away = self.check_done() if self.check_done(): self.done = self.transition.fade_out() self.frame += 1 self.update_time() self.players.update() self.update_labels() self.showcase_weapons() self.background.update() def draw(self, surface): self.background.draw(surface) [player.draw(surface) for player in self.players] # See similar comment in Level file # self.players.draw(surface) surface.blit(self.mask, (0, 0)) if self.p1_ship_image: # Image only gets initialized when player has indicated they want to play surface.blit(self.p1_ship_image, self.p1_ship_rect) surface.blit(self.p1_power_image, self.p1_power_rect) surface.blit(self.p1_speed_image, self.p1_speed_rect) surface.blit(self.p1_name_image, self.p1_name_rect) if self.p2_ship_image: # Image only gets initialized when player has indicated they want to play surface.blit(self.p2_ship_image, self.p2_ship_rect) surface.blit(self.p2_power_image, self.p2_power_rect) surface.blit(self.p2_speed_image, self.p2_speed_rect) surface.blit(self.p2_name_image, self.p2_name_rect) self.labels.draw(surface) self.transition.draw(surface) def set_player_1(self): self.player_1.kill( ) # Kill then add because we want to remove the ship that is to be switched out. self.player_1 = next(self.p1_ship_sprites) self.players.add(self.player_1) self.choice['1p'] = self.player_1.id self.p1_ship_image = next(self.p1_ship_img) self.p1_ship_rect = next(self.p1_ship_img_rects) self.p1_power_image = next(self.p1_star_img) self.p1_speed_image = next(self.p1_star_img) next( self.p1_star_img ) # This is so that the power and speed images alternate 5,4 -> 4,5 -> 5,4 self.p1_name_image = next(self.p1_name_img) def set_player_2(self): self.player_2.kill() self.player_2 = next(self.p2_ship_sprites) self.players.add(self.player_2) self.choice['2p'] = self.player_2.id self.p2_ship_image = next(self.p2_ship_img) self.p2_ship_rect = next(self.p2_ship_img_rects) self.p2_power_image = next(self.p2_star_img) self.p2_speed_image = next(self.p2_star_img) next( self.p2_star_img ) # This is so that the power and speed images alternate 5,4 -> 4,5 -> 5,4 self.p2_name_image = next(self.p2_name_img) def update_time(self): if self.frame % 60 == 0: self.time_left -= 1 def update_labels(self): self.time_label.original_text = str(self.time_left) self.time_label.blink() self.coin_label.update_text(f'credit {self.coins}') def check_done(self): if self.time_left <= 0: return True if not self.player_2.alive() and self.player_1_confirm: return True if not self.player_1.alive() and self.player_2_confirm: return True if self.player_1.alive() and self.player_2.alive( ) and self.player_1_confirm and self.player_2_confirm: return True return False def showcase_weapons(self): if self.frame % 120 >= 60: if self.player_1.alive(): self.player_1.weapon_2 = True if self.player_2.alive(): self.player_2.weapon_2 = True else: if self.frame % 10 == 1: # Fire at 1,11,21,31,41,51 if self.player_1.alive(): PlayerWeapon1(self.player_1) self.player_1.weapon_2 = False if self.player_2.alive(): PlayerWeapon1(self.player_2) self.player_2.weapon_2 = False def event_process(self, events: List[pygame.event.Event]): for event in events: if event.type == pygame.KEYDOWN: if event.key in { self.controls['1p_coin'], self.controls['2p_coin'] }: SFX['coin'].play() self.coins += 1 if event.key == self.controls['1p_start']: if not self.player_1.alive() and self.coins >= 1: self.coins -= 1 self.time_left = 20 self.set_player_1() if event.key == self.controls['2p_start']: if not self.player_2.alive() and self.coins >= 1: self.coins -= 1 self.time_left = 20 self.set_player_2() if event.key in [ self.controls[x] for x in {'1p_up', '1p_down', '1p_left', '1p_right'} ]: if self.player_1.alive() and not self.player_1_confirm: self.set_player_1() SFX['hint'].play() if event.key in [ self.controls[x] for x in {'2p_up', '2p_down', '2p_left', '2p_right'} ]: if self.player_2.alive() and not self.player_2_confirm: self.set_player_2() SFX['hint'].play() if event.key in { self.controls['1p_button_a'], self.controls['1p_button_b'] }: if self.player_1.alive() and not self.player_1_confirm: self.player_1_confirm = True self.p1_ship_image = None # Reset image, so that it is not drawn if event.key in { self.controls['2p_button_a'], self.controls['2p_button_b'] }: if self.player_2.alive() and not self.player_2_confirm: self.player_2_confirm = True self.p2_ship_image = None # Reset image, so that it is not drawn
class Title(State): def __init__(self): super().__init__() self.background = GFX['title_screen'] self.coins = 0 self.fade_away = False self.event_block = False self.transition = Transition() self.controls: dict = None self.coin_label: Label = None self.control_label: Label = None self.hint_label: BlinkerLabel = None self.labels = pygame.sprite.Group() self.make_labels() self.choice = {'1p': 0, '2p': 0} self.screen_saver = 0 def startup(self, persist: dict): self.done = False self.next = 'SELECT' self.persist = persist self.choice = self.persist.get('choice', {'1p': 0, '2p': 0}) self.coins = self.persist.get('coins', 0) self.frame = 0 self.screen_saver = 0 pygame.mixer.music.load(MUSIC['06_-_space_troopers_0']) pygame.mixer.music.play(-1) # -1 means looping music with open(CONTROLS, 'r') as f: self.controls = self.persist.get('controls', json.load(f)) self.transition = Transition() self.fade_away = False self.event_block = False def cleanup(self): if self.next == 'SELECT': pygame.mixer.fadeout(500) persist = { 'controls': self.controls, 'choice': self.choice, 'coins': self.coins } return persist def update(self): self.frame += 1 self.screen_saver += 1 self.transition.fade_in() # If we have waited for 5 seconds, not transitioning if self.screen_saver == 300 and not self.fade_away: self.fade_away = True self.next = 'HIGHSCORE' if self.fade_away: self.done = self.transition.fade_out() elif self.transition.frame > 0: self.transition = Transition() # Reset fadeaway self.update_labels() def draw(self, surface): surface.blit(self.background, (0, 0)) self.labels.draw(surface) self.transition.draw(surface) def update_labels(self): self.coin_label.update_text(f'credit {self.coins}') self.hint_label.original_text = f'Please insert coin' \ if not self.coins else \ f'Press 1p or 2p start button' self.hint_label.blink() def make_labels(self): self.coin_label = Label(f'credit {self.coins}', {'midbottom': (300, 750)}, self.labels, font_path=ARCADE_CLASSIC, font_size=25) # 10 pixel spacing between each one self.control_label = Label('Tab key for control settings', {'midbottom': (300, 780)}, self.labels, font_path=ARCADE_CLASSIC, font_size=15) # This one is kind of the "center" hint, telling them to press what they need to start. self.hint_label = BlinkerLabel( '', # This will be updated in the next loop {'midbottom': (300, 700)}, 30, self.labels, font_path=ARCADE_CLASSIC, font_size=30) def event_process(self, events: List[pygame.event.Event]): if self.event_block: return for event in events: if event.type == pygame.KEYDOWN: self.screen_saver = 0 if event.key == pygame.K_TAB: self.fade_away = True self.event_block = True self.next = 'CONTROL' if event.key in { self.controls['1p_coin'], self.controls['2p_coin'] }: SFX['coin'].play() self.coins += 1 if event.key == self.controls['1p_start']: if self.coins >= 1: self.coins -= 1 self.choice['1p'] = 1 self.fade_away = True self.event_block = True self.next = 'SELECT' if event.key == self.controls['2p_start']: if self.coins >= 1: self.coins -= 1 self.choice['2p'] = 2 self.fade_away = True self.event_block = True self.next = 'SELECT'
class Entername(State): def __init__(self): super().__init__() self.background = Background(1) self.text_input = TextInput(font_family=ARCADE_CLASSIC, font_size=50, text_color=(255, 255, 255), cursor_color=(255, 255, 255), max_length=15) self.next = 'HIGHSCORE' self.scoreboard = {} self.p1_done = False self.p2_done = False self.event_block = False self.p1_idx = 0 self.p2_idx = 0 self.fade_away = False self.transition = Transition() self.title = GFX['entername'] self.title_rect = self.title.get_rect(center=(300, 100)) self.prompt_label: Label = None def startup(self, persist: dict): self.frame = 0 self.done = False self.persist = persist self.text_input.reset() self.scoreboard = persist['scoreboard'] self.check_done() self.fade_away = False self.transition = Transition() self.event_block = False self.prompt_label = Label('', { 'center': (300, 200)}, font_path=ARCADE_CLASSIC, font_size=40) def cleanup(self): return { 'coins' : self.persist['coins'], 'controls' : self.persist['controls'], 'scoreboard': self.scoreboard} def event_process(self, events: List[pygame.event.Event]): if self.event_block: return if self.text_input.update(events): if not self.p1_done: self.p1_done = True self.scoreboard[self.p1_idx][0] = self.text_input.get_text() self.text_input.reset() if not self.p2_done: self.p2_done = True self.scoreboard[self.p2_idx][0] = self.text_input.get_text() self.text_input.reset() def update(self): self.background.update() if self.p1_done and self.p2_done: self.event_block = True self.fade_away = True self.sort_scoreboard() if self.fade_away: self.done = self.transition.fade_out() self.update_labels() def draw(self, surface): self.background.draw(surface) input_surface = self.text_input.get_surface() input_rect = input_surface.get_rect(center=(300, 400)) surface.blit(input_surface, input_rect) surface.blit(self.title, self.title_rect) surface.blit(self.prompt_label.image, self.prompt_label.rect) def check_done(self): self.p1_done = True self.p2_done = True for idx, entry in enumerate(self.scoreboard): if entry[0] == 'player_1': self.p1_idx = idx self.p1_done = False if entry[0] == 'player_2': self.p2_idx = idx self.p2_done = False def sort_scoreboard(self): self.scoreboard = sorted(self.scoreboard, key=lambda entry: entry[2], reverse=True) def update_labels(self): if not self.p1_done: self.prompt_label.update_text('Enter Player 1 Name') elif not self.p2_done: self.prompt_label.update_text('Enter Player 2 Name') else: self.prompt_label.update_text('Thank you for playing')
class Highscore(State): def __init__(self): super().__init__() self.background = Background(5) self.labels = pygame.sprite.Group() self.scoreboard: dict = None self.title = GFX['scoreboard'] self.title_rect = self.title.get_rect(center=(300, 100)) self.transition = Transition() self.next = 'TITLE' self.fade_away = False self.images = [ pygame.transform.scale(GFX[f'player{x}'], (40, 60)) for x in range(1, 5) ] def startup(self, persist: dict): self.frame = 0 self.done = False self.transition = Transition() self.fade_away = False self.persist = persist with open(SCORE, 'r') as f: self.scoreboard = self.persist[ 'scoreboard'] if 'scoreboard' in self.persist else json.load(f) self.labels = pygame.sprite.Group() self.init_labels() self.controls = self.persist['controls'] def cleanup(self): with open(SCORE, 'w') as f: json.dump(self.scoreboard[:5], f) return self.persist.copy() def init_labels(self): ranks = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th'] colours = [(234, 199, 135), (233, 233, 216), (186, 110, 64), (118, 119, 120), (118, 119, 120), (118, 119, 120), (118, 119, 120)] # Gold, silver, bronze, grey, grey, because last place doesn't get anything :) pos_1 = [(150, 250 + y * 80) for y in range(len(self.scoreboard))] pos_2 = [(220, 250 + y * 80) for y in range(len(self.scoreboard))] pos_3 = [(550, 250 + y * 80) for y in range(len(self.scoreboard))] for rank, bottom_left, colour in zip(ranks, pos_1, colours): Label(rank, {'bottomleft': bottom_left}, self.labels, font_path=ANCIENT_MEDIUM, font_size=40, text_colour=colour) for entry, bottom_left, colour in zip(self.scoreboard, pos_2, colours): # Name Label(entry[0], {'bottomleft': bottom_left}, self.labels, font_path=ARCADE_CLASSIC, font_size=40, text_colour=colour) for entry, bottom_left, colour in zip(self.scoreboard, pos_3, colours): # Score Label(str(entry[2]), {'bottomright': bottom_left}, self.labels, font_path=ARCADE_CLASSIC, font_size=40, text_colour=colour) def blit_player_images(self, surface): for idx, entry in enumerate(self.scoreboard): surface.blit(self.images[entry[1] - 1], (50, 200 + idx * 80)) def update(self): self.frame += 1 self.transition.fade_in() if self.frame >= 300: self.fade_away = True if self.fade_away: self.done = self.transition.fade_out() self.background.update() def draw(self, surface): self.background.draw(surface) self.blit_player_images(surface) surface.blit(self.title, self.title_rect) self.labels.draw(surface) self.transition.draw(surface) def event_process(self, events: List[pygame.event.Event]): pass