def setup(self): """ Initialize the GUI and place it properly. """ # Create the main menu font. WELCOME_FONT_TITLE = pygame.font.SysFont('Consolas', 28) WELCOME_FONT_INSTRUCTIONS = pygame.font.SysFont('Consolas', 20) # Create the title display. self.title_display = Gui.DisplayableText(value='num.type', font=WELCOME_FONT_TITLE, color=Color(rgb=Color.WHITE), align='cc') self.title_display.set_position(640 / 2, 40) self.add_gui(self.title_display) self.title_colors = [ Color(rgb=(200, 20, 20)), Color(rgb=(200, 200, 20)), Color(rgb=(20, 200, 20)), Color(rgb=(20, 200, 200)), Color(rgb=(20, 20, 200)), Color(rgb=(200, 20, 200)) ] self.title_color_current = 0 self.title_color_next = 1 self.title_color_current_began_at = get_ticks() # Create the subtitle display. self.subtitle_display = Gui.DisplayableText( value='a numerical typing game', font=WELCOME_FONT_TITLE, color=Color(rgb=Color.WHITE), align='cc') self.subtitle_display.set_position(640 / 2, 70) self.add_gui(self.subtitle_display) self.subtitle_color_current = 1 self.subtitle_color_next = 2 # Create the instructions display. self.instr_displays = [] instructions = [ 'Attempt to type in numbers as fast as you can.', 'You can only use the numeric keyboard.', 'Every time you "shoot" a target in time, it will grant', 'you points.', 'Every time you miss, you will lose one heart,', 'so type carefully.', 'You can use backspace if you mistype.', 'If a target times out, you will lose points.', 'The game will become progressively harder as you play.', 'Win by accumulating {} points.'.format(VICTORY_POINTS), 'Lose by going below 0 points or losing all hearts.', '', 'Press ENTER to begin.', 'Pressing ESCAPE at any time will end the game in defeat.', 'Good luck!' ] for instruction in instructions: new_display = Gui.DisplayableText(value=instruction, font=WELCOME_FONT_INSTRUCTIONS, color=Color(rgb=Color.WHITE), align='lc') new_display.set_position(20, 120 + 23 * len(self.instr_displays)) self.instr_displays.append(new_display) self.add_gui(new_display)
def setup(self, score_keeper): """ Initialize the GUI and place it properly. """ # Create the scores font. SCORE_FONT = pygame.font.SysFont('Consolas', 28) # Create the main message. self.message = Gui.DisplayableText( value='VICTORY' if score_keeper.victory else 'DEFEAT', font=BASIC_FONT, color=Color(rgb=Color.WHITE), align='cc') self.message.set_position(640 / 2, 40) self.add_gui(self.message) # Create the instructions display. self.score_displays = [] scores = [ 'Final point score: {}'.format(score_keeper.points_gained - score_keeper.points_lost), 'Total points gained: {}'.format(score_keeper.points_gained), 'Total points lost: {}'.format(score_keeper.points_lost), 'Total targets shot: {}'.format(score_keeper.targets_shot), 'Total targets timed out: {}'.format( score_keeper.targets_timed_out), 'Total misses: {}'.format(score_keeper.misses), 'Accuracy: {}%'.format(score_keeper.accuracy), 'Effectiveness: {}%'.format(score_keeper.effectiveness), 'Time played: {}'.format(score_keeper.time_elapsed), '', 'Press ESCAPE to return to main menu.' ] for score in scores: new_display = Gui.DisplayableText(value=score, font=SCORE_FONT, color=Color(rgb=Color.WHITE), align='lc') new_display.set_position(20, 100 + 30 * len(self.score_displays)) self.score_displays.append(new_display) self.add_gui(new_display)
def __init__(self, blueprint, font): """ Set initial values for the instance based on the blueprint. """ BA = blueprint.attributes text = Gui.DisplayableText(value=BA[AT.VALUE], font=font, color=BA[AT.COLORS]['text'], align='cc') super().__init__(width=BA[AT.WIDTH], text=text) self.events = blueprint.events.copy() self.attributes = attr = BA.copy() attr[AT.TIME_CREATED] = get_ticks() attr[AT.GARBAGE] = False if not AT.TIME_TO_BE_SHOWN in attr: attr[AT.TIME_TO_BE_SHOWN] = 0 if AT.FRAME_WIDTH in attr: self.frame_width = attr[AT.FRAME_WIDTH] self.colors = self.attributes[AT.COLORS]
def setup(self): """ Initialize the GUI and place it properly; initialize the variables. """ # Set up the collector. self.collector = Collector() self.collector.update_function = self.update_input_box # Create a key map. keycodes = { 256: ['0', (2, 1), (18, 169, 202)], 257: ['1', (1, 1), (196, 170, 77)], 258: ['2', (1, 1), (242, 210, 72)], 259: ['3', (1, 1), (45, 186, 97)], 260: ['4', (1, 1), (121, 124, 125)], 261: ['5', (1, 1), (133, 145, 115)], 262: ['6', (1, 1), (196, 52, 92)], 263: ['7', (1, 1), (245, 245, 252)], 264: ['8', (1, 1), (206, 224, 182)], 265: ['9', (1, 1), (244, 124, 124)], 266: ['.', (1, 1), (255, 255, 255)], 267: ['/', (1, 1), (255, 255, 255)], 268: ['*', (1, 1), (255, 255, 255)], 269: ['-', (1, 1), (255, 255, 255)], 270: ['+', (1, 2), (255, 255, 255)], 271: ['e', (1, 2), (255, 255, 255)], 300: ['n', (1, 1), (255, 255, 255)] } # Create the key displays. self.keys = {} for code in keycodes: self.keys[code] = Gui.KeyButton(value=keycodes[code][0], font=BASIC_FONT, size=keycodes[code][1], color=Color(rgb=keycodes[code][2])) if code >= 256 and code <= 265: self.keys[code].keyup_function = self.collector.collect layout = ((300, 267, 268, 269), (263, 264, 265, 270), (260, 261, 262), (257, 258, 259, 271), (256, 266)) x = SPACER y = SPACER for row in layout: for col in row: self.keys[col].set_position(x, y) self.add_gui_updatable(self.keys[col]) x += self.keys[col].width + SPACER y += Gui.GUIRect.HEIGHT + SPACER x = SPACER # Create the score display. self.hp_display = Gui.DisplayableText(value='♥♥♥', font=BASIC_FONT, color=Color(rgb=Color.WHITE), align='rc') self.hp_display.set_position(640 - SPACER, 400) self.add_gui(self.hp_display) # Create the HP display. self.score_display = Gui.DisplayableText(value='Score: 0', font=BASIC_FONT, color=Color(rgb=Color.WHITE), align='lc') self.score_display.set_position(SPACER, 400) self.add_gui(self.score_display) # Create the HP lost display. self.hp_lost_display = Gui.FadingText(value='♥', font=BASIC_FONT, color=Color(rgb=Color.RED), align='lc') self.hp_lost_display.deactivate() self.add_gui(self.hp_lost_display) self.add_gui_updatable(self.hp_lost_display) # Create the input box. self.input_box = Gui.InputBox(width=self.owner.window_size[0] - 4 * Gui.KeyButton.WIDTH - 6 * SPACER, font=BASIC_FONT) self.input_box.set_position(x=4 * (Gui.GUIRect.WIDTH + SPACER) + SPACER, y=SPACER, margin_x=5) self.add_gui(self.input_box) # Set up the TargetFactory. func_dict = { AC.REWARD: self.reward, AC.PENALTY: self.penalty, AC.DESPAWN_GOOD: self.despawn_good, AC.DESPAWN_BAD: self.despawn_bad, AC.DESPAWN_NONENTITY: self.despawn_nonentity, AC.SPAWN: self.spawn } self.target_factory = TargetFactory( functions=func_dict, after_adder=self.after_adder, board_position=self.input_box.get_position(), board_width=self.input_box.width, font=BASIC_FONT) # Set up a simple game. self.score = 0 self.hp = 10 for i in range(4): blueprint = TargetBlueprint() blueprint.attributes[AT.TARGET_TYPE] = TargetType.TIMED blueprint.attributes[AT.STRENGTH] = 3 - STRENGTH_INCREASE self.target_factory.create(blueprint) self.owner.score_keeper.begin() # Set up the key events map. for code in keycodes: self.key_events[KEYUP][code] = [self.keys[code].keyup] self.key_events[KEYDOWN][code] = [self.keys[code].keydown] self.key_events[KEYUP][K_ESCAPE] = [self.clear_or_surrender] self.key_events[KEYUP][8] = [self.collector.backspace] # backspace self.key_events[KEYUP][271].append(self.shoot_target) # enter # Below function disabled in the current one-mode-only game. # self.key_events[KEYUP][270].append(self.add_target) # + self.key_events[KEYUP][46] = self.key_events[KEYUP][266] # .=, self.key_events[KEYDOWN][46] = self.key_events[KEYDOWN][266] # .=, # Perform the first update to show GUI properly. self.update_score()