def setup_graphics_menu(self): self.graphics_menu_left = self.setup_menu_left() self.graphics_menu_right = self.setup_menu_right() # Setup the buttons and make them "on/off" buttons. shadows_button = textitem.TextItem("Shadows On") shadows_button.setup_is_on_off("Shadows Off", graphics.SHADOWS) self.graphics_menu_left.add(shadows_button, self.shadows) particles_button = textitem.TextItem("Particles On") particles_button.setup_is_on_off("Particles Off", graphics.PARTICLES) self.graphics_menu_left.add(particles_button, self.particles) flashes_button = textitem.TextItem("Flashes On") flashes_button.setup_is_on_off("Flashes Off", graphics.FLASHES) self.graphics_menu_left.add(flashes_button, self.flashes) traces_button = textitem.TextItem("Traces On") traces_button.setup_is_on_off("Traces Off", graphics.TRACES) self.graphics_menu_right.add(traces_button, self.traces) traces_button = textitem.TextItem("Background On") traces_button.setup_is_on_off("Background Off", graphics.BACKGROUND) self.graphics_menu_right.add(traces_button, self.background) # We store the graphics offset so we can offset the logo by this later. self.graphics_menu_offset = (shadows_button.get_height() * 1) self.graphics_menu_left.y = (settings.SCREEN_HEIGHT / 2) - self.graphics_menu_offset self.graphics_menu_right.y = (settings.SCREEN_HEIGHT / 2) - self.graphics_menu_offset self.menu_list.append(self.graphics_menu_left) self.menu_list.append(self.graphics_menu_right)
def setup_sound_menu(self): self.music_item = textitem.TextItem("Music Volume:") self.music_item.x = self.music_item.get_height() self.music_item.y = settings.SCREEN_HEIGHT / 2.0 self.music_volume_menu = gridmenu.GridMenu(6) self.music_volume_menu.add(choiceitem.ChoiceItem(0), self.set_music_volume) self.music_volume_menu.add(choiceitem.ChoiceItem(1), self.set_music_volume) self.music_volume_menu.add(choiceitem.ChoiceItem(2), self.set_music_volume) self.music_volume_menu.add(choiceitem.ChoiceItem(3), self.set_music_volume) self.music_volume_menu.add(choiceitem.ChoiceItem(4), self.set_music_volume) self.music_volume_menu.add(choiceitem.ChoiceItem(5), self.set_music_volume) self.music_volume_menu.x = settings.SCREEN_WIDTH - self.music_volume_menu.get_width( ) - self.music_item.get_height() self.music_volume_menu.y = self.music_item.y - ( abs(self.music_item.get_height() - self.music_volume_menu.get_height()) / 2.0) # Set the button that corresponds to the current volume level to be the chosen item. self.music_volume_menu.items[int( (pygame.mixer.music.get_volume() * (len(self.music_volume_menu.items) - 1)) + 0.5)].chosen = True self.sound_item = self.sound_item = textitem.TextItem("Sound Volume:") self.sound_item.x = self.sound_item.get_height() self.sound_item.y = self.music_item.y + self.sound_item.get_height( ) * 2.0 self.sound_volume_menu = gridmenu.GridMenu(6) self.sound_volume_menu.add(choiceitem.ChoiceItem(0), self.set_sound_volume) self.sound_volume_menu.add(choiceitem.ChoiceItem(1), self.set_sound_volume) self.sound_volume_menu.add(choiceitem.ChoiceItem(2), self.set_sound_volume) self.sound_volume_menu.add(choiceitem.ChoiceItem(3), self.set_sound_volume) self.sound_volume_menu.add(choiceitem.ChoiceItem(4), self.set_sound_volume) self.sound_volume_menu.add(choiceitem.ChoiceItem(5), self.set_sound_volume) self.sound_volume_menu.x = settings.SCREEN_WIDTH - self.sound_volume_menu.get_width( ) - self.sound_item.get_height() self.sound_volume_menu.y = self.sound_item.y - ( abs(self.sound_item.get_height() - self.sound_volume_menu.get_height()) / 2.0) # Set the button that corresponds to the current volume level to be the chosen item. self.sound_volume_menu.items[int( (settings.SOUND_VOLUME * (len(self.music_volume_menu.items) - 1)) + 0.5)].chosen = True self.menu_list.append(self.music_volume_menu) self.menu_list.append(self.sound_volume_menu)
def setup_options_menu(self): self.options_menu = self.setup_menu() self.options_menu.add(textitem.TextItem("Graphics"), self.graphics) self.options_menu.add(textitem.TextItem("Sound"), self.sound) self.options_menu.add(textitem.TextItem("About"), self.about) self.options_menu.add(textitem.TextItem("Back"), self.back) self.options_menu.items[0].selected = True self.menu_list.append(self.options_menu)
def setup_main_menu(self): self.main_menu = listmenu.ListMenu(settings.SCREEN_WIDTH / 2.0, settings.SCREEN_HEIGHT / 2.0) self.main_menu.add(textitem.TextItem("Start"), self.start) self.main_menu.add(textitem.TextItem("Options"), self.options) self.main_menu.add(textitem.TextItem("Help"), self.help) self.main_menu.add(textitem.TextItem("Quit"), self.quit) self.main_menu.items[0].selected = True self.menu_list.append(self.main_menu)
def __init__(self, window_surface, main_clock, player_one, player_two, number_of_rounds, score, winner): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # Tint the window surface and set it as the background surface. self.background_surface = window_surface.copy() useful.tint_surface(self.background_surface) # The next screen to be started when the gameloop ends. self.next_screen = screens.mainmenu.MainMenu # Keep track of the players and the score for displaying. self.player_one = player_one self.player_two = player_two self.score = score # Keep track of the number of rounds for rematch purposes. self.number_of_rounds = number_of_rounds # Store the winner self.winner = winner # Configure the GUI. item_side_padding = textitem.TextItem.font_size # This sets up the winner text, coloring it and everything. self.setup_winner_text() quit_button = textitem.TextItem("Quit") self.quit_menu = listmenu.ListMenu(item_side_padding + (quit_button.get_width() / 2), settings.SCREEN_HEIGHT - item_side_padding - quit_button.get_height()) self.quit_menu.add(quit_button, self.quit) self.quit_menu.items[0].selected = True self.menu_list.append(self.quit_menu) rematch_button = textitem.TextItem("Rematch") self.rematch_menu = listmenu.ListMenu(settings.SCREEN_WIDTH - item_side_padding - (rematch_button.get_width() / 2), settings.SCREEN_HEIGHT - item_side_padding - rematch_button.get_height()) self.rematch_menu.add(rematch_button, self.rematch) self.menu_list.append(self.rematch_menu) # Register all menus with each other. for a_menu in self.menu_list: a_menu.register_other_menus(self.menu_list) # Setup the menu transition. for letter_item in self.winning_player_text: self.transition.setup_single_item_transition(letter_item, True, True, True, True) self.transition.setup_transition(self.quit_menu, True, False, False, True) self.transition.setup_transition(self.rematch_menu, False, True, False, True) self.firework_spawn_time = 300 self.time_passed = 0 # Setup and play music. self.setup_music() self.gameloop()
def setup_pause_menu(self): # Creates and adds the items to the pause menu. self.pause_menu = listmenu.ListMenu() self.pause_menu.add(textitem.TextItem("Resume"), self.resume) self.pause_menu.add(textitem.TextItem("Options"), self.options) self.pause_menu.add(textitem.TextItem("Quit"), self.maybe_quit) self.pause_menu.x = settings.SCREEN_WIDTH / 2 self.pause_menu.y = (settings.SCREEN_HEIGHT - self.pause_menu.get_height()) / 2.0 self.pause_menu.cleanup() self.pause_menu.items[0].selected = True self.menu_list.append(self.pause_menu)
def setup_toast_menu(self): # Creates and adds the items to the toast menu. self.toast_menu = listmenu.ListMenu() self.toast_menu.add(textitem.TextItem("Ok"), self.resume) self.toast_menu.x = settings.SCREEN_WIDTH / 2 self.toast_menu.y = self.message[len(self.message) - 1].y + self.message[0].get_height() + self.toast_menu.get_height() self.toast_menu.cleanup() self.toast_menu.items[0].selected = True self.menu_list.append(self.toast_menu)
def setup_version_message(self): text = settings.GAME_VERSION font_color = (255, 255, 255) alpha_value = 255 self.version_message = textitem.TextItem(text, font_color, alpha_value) self.version_message.x = settings.SCREEN_WIDTH - self.version_message.get_width( ) - self.version_message.font_size self.version_message.y = settings.SCREEN_HEIGHT - self.version_message.get_height( ) - self.version_message.font_size
def __init__(self, main_clock, function_to_call): # We use the main clock to keep track of the time passed. self.main_clock = main_clock # This is the function that is called when the countdown is over. self.function_to_call = function_to_call # When time passed reaches time to countdown, the countdown ends. self.time_passed = 0 self.time_to_countdown = 1500 # This is the amount of time that ready is displayed. self.countdown_ready_time = 2000 # This is the amount of time that go is displayed. self.countdown_go_time = 1250 # Create, position and store the "Ready" textitem. self.countdown_ready = textitem.TextItem("Ready", (255, 255, 255)) self.countdown_ready.x = -self.countdown_ready.get_width() self.countdown_ready.y = (settings.SCREEN_HEIGHT - self.countdown_ready.get_height()) / 2 self.countdown_ready_desired_x = (settings.SCREEN_WIDTH / 2) - self.countdown_ready.get_width() self.countdown_ready_desired_y = (settings.SCREEN_HEIGHT - self.countdown_ready.get_height()) / 2 self.countdown_ready_speed = 780 * settings.GAME_SCALE self.countdown_ready_slow_speed = 60 * settings.GAME_SCALE # Create, position and store the "GO" textitem. self.countdown_go = textitem.TextItem("GO", (255, 255, 255)) self.countdown_go.set_size(18 * settings.GAME_SCALE) self.countdown_go.x = -self.countdown_go.get_width() self.countdown_go.y = (settings.SCREEN_HEIGHT - self.countdown_go.get_height()) / 2 self.countdown_go_desired_x = (settings.SCREEN_WIDTH - self.countdown_go.get_width()) / 2 self.countdown_go_desired_y = (settings.SCREEN_HEIGHT / 2) - self.countdown_go.get_height() self.countdown_go_speed = 780 * settings.GAME_SCALE self.countdown_go_slow_speed = 78 * settings.GAME_SCALE # This is the surface that is actually drawn when the draw method is called. self.active_surface = self.countdown_ready # When this is false, the update_and_draw method displays the countdown. self.done = False
def setup_menu(self): # Setup the textitem. self.confirmation_text = textitem.TextItem("Are you sure?", pygame.Color(255, 255, 255)) self.confirmation_text.x = (settings.SCREEN_WIDTH - self.confirmation_text.get_width()) / 2 # Setup the menu. self.confirmation_menu = listmenu.ListMenu() self.confirmation_menu.add(textitem.TextItem("Yes"), self.accept) self.confirmation_menu.add(textitem.TextItem("No"), self.refuse) self.confirmation_menu.x = settings.SCREEN_WIDTH / 2 # Set the default action to be to refuse. self.confirmation_menu.items[1].selected = True self.menu_list.append(self.confirmation_menu) self.confirmation_text.y = (settings.SCREEN_HEIGHT - ( (2 * self.confirmation_text.get_height()) + self.confirmation_menu.get_height())) / 2.0 self.confirmation_menu.y = self.confirmation_text.y + ( 2 * self.confirmation_text.get_height()) self.confirmation_menu.cleanup()
def __init__(self, window_surface, main_clock, title_logo=None): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # The next screen to be started when the gameloop ends. self.next_screen = None # Setup all the menu buttons. self.setup_sound_menu() # The back button, displayed in the middle-bottom of the screen. back_button = textitem.TextItem("Back") self.back_menu = listmenu.ListMenu() self.back_menu.x = settings.SCREEN_WIDTH / 2 self.back_menu.y = settings.SCREEN_HEIGHT - (2 * back_button.get_height()) self.back_menu.add(back_button, self.back) self.back_menu.items[0].selected = True self.menu_list.append(self.back_menu) # Register all menus with each other. for a_menu in self.menu_list: a_menu.register_other_menus(self.menu_list) # Setup the logo and the variables needed to handle the animation of it. self.setup_logo(title_logo) self.logo_desired_position = ( (settings.SCREEN_WIDTH - self.title_logo.get_width()) / 2, ((settings.SCREEN_HEIGHT - self.title_logo.get_height()) / 4)) self.logo_transition = transition.Transition() self.logo_transition.speed = 2 * settings.GAME_FPS * settings.GAME_SCALE # Setup the menu transitions. self.transition.setup_single_item_transition(self.music_item, True, False, False, False) self.transition.setup_single_item_transition(self.sound_item, True, False, False, False) self.transition.setup_transition(self.music_volume_menu, False, True, False, False) self.transition.setup_transition(self.sound_volume_menu, False, True, False, False) self.transition.setup_transition(self.back_menu, True, True, False, False) # And finally, start the gameloop! self.gameloop()
def __init__(self, window_surface, main_clock, message): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # Tint the window surface and set it as the background surface. self.background_surface = self.window_surface.copy() useful.tint_surface(self.background_surface, 225) distance_from_screen_edge = 6 * settings.GAME_SCALE max_width_of_text_line = (settings.SCREEN_WIDTH - (distance_from_screen_edge * 2)) wrapped_message = useful.wrap_multi_line(message, pygame.font.Font(textitem.TextItem.font_path, textitem.TextItem.font_size), max_width_of_text_line) # We load a TextItem to display the message. self.message = [] odd = True for line in wrapped_message: if odd: color = Toast.text_color else: color = Toast.text_color_odd message = textitem.TextItem(line, color) message.x = (settings.SCREEN_WIDTH - message.get_width()) / 2.0 self.message.append(message) odd = not odd for message in self.message: message.y = (settings.SCREEN_HEIGHT - len(self.message) * message.get_height()) / 2.0 + self.message.index(message) * message.get_height() # Create, store and position the toast menu. self.setup_toast_menu() # Setup the menu transition. self.transition.speed *= 1.5 for message in self.message: if self.message.index(message) == 0: self.transition.setup_single_item_transition(message, True, True, True, False) else: self.transition.setup_single_item_transition(message, True, True, False, False) self.transition.setup_single_item_transition(self.toast_menu.items[0], True, True, False, True) # And finally, start the gameloop! self.gameloop()
def __init__(self, window_surface, main_clock): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # The next screen to be started when the gameloop ends. self.next_screen = None # The back button, displayed in the middle-bottom of the screen. back_button = textitem.TextItem("Back") self.back_menu = listmenu.ListMenu() self.back_menu.x = settings.SCREEN_WIDTH / 2 self.back_menu.y = settings.SCREEN_HEIGHT - (2 * back_button.get_height()) self.back_menu.add(back_button, self.back) self.back_menu.items[0].selected = True self.menu_list.append(self.back_menu) # We choose a smaller font size here for all the credits. font_size = 6 * settings.GAME_SCALE # Create and setup all the textitems. self.pyganim_credits = textitem.TextItem( "Pyganim is used to help animate items in the game") self.pyganim_credits.set_size(font_size) self.pyganim_credits.x = (settings.SCREEN_WIDTH - self.pyganim_credits.get_width()) / 2 self.pyganim_credits.y = 9 * settings.GAME_SCALE self.pyganim_credits_source_code = textitem.TextItem( "Pyganim source code is not included in this game") self.pyganim_credits_source_code.set_size(font_size) self.pyganim_credits_source_code.x = ( settings.SCREEN_WIDTH - self.pyganim_credits_source_code.get_width()) / 2 self.pyganim_credits_source_code.y = self.pyganim_credits.y + self.pyganim_credits_source_code.get_height( ) self.pyganim_credits_author = textitem.TextItem( "Pyganim is made by Al Sweigart", pygame.Color(255, 255, 255)) self.pyganim_credits_author.set_size(font_size) self.pyganim_credits_author.x = ( settings.SCREEN_WIDTH - self.pyganim_credits_author.get_width()) / 2 self.pyganim_credits_author.y = self.pyganim_credits_source_code.y + self.pyganim_credits_author.get_height( ) self.music_credits_title = textitem.TextItem( "Title screen music is sexxxy bit 3.xm") self.music_credits_title.set_size(font_size) self.music_credits_title.x = (settings.SCREEN_WIDTH - self.music_credits_title.get_width()) / 2 self.music_credits_title.y = self.pyganim_credits_author.y + ( 2 * self.music_credits_title.get_height()) self.music_credits_after_game = textitem.TextItem( "Postgame music is october chip.xm") self.music_credits_after_game.set_size(font_size) self.music_credits_after_game.x = ( settings.SCREEN_WIDTH - self.music_credits_after_game.get_width()) / 2 self.music_credits_after_game.y = self.music_credits_title.y + self.music_credits_after_game.get_height( ) self.music_credits_title_author = textitem.TextItem( "Both made by Drozerix", pygame.Color(255, 255, 255)) self.music_credits_title_author.set_size(font_size) self.music_credits_title_author.x = ( settings.SCREEN_WIDTH - self.music_credits_title_author.get_width()) / 2 self.music_credits_title_author.y = self.music_credits_after_game.y + self.music_credits_title_author.get_height( ) self.music_credits_game = textitem.TextItem( "Game music is stardstm.mod") self.music_credits_game.set_size(font_size) self.music_credits_game.x = (settings.SCREEN_WIDTH - self.music_credits_game.get_width()) / 2 self.music_credits_game.y = self.music_credits_title_author.y + ( 2 * self.music_credits_game.get_height()) self.music_credits_game_author = textitem.TextItem( "Made by Jester", pygame.Color(255, 255, 255)) self.music_credits_game_author.set_size(font_size) self.music_credits_game_author.x = ( settings.SCREEN_WIDTH - self.music_credits_game_author.get_width()) / 2 self.music_credits_game_author.y = self.music_credits_game.y + self.music_credits_game_author.get_height( ) self.sound_effect_credits = textitem.TextItem( "Most sound effects made using bfxr") self.sound_effect_credits.set_size(font_size) self.sound_effect_credits.x = ( settings.SCREEN_WIDTH - self.sound_effect_credits.get_width()) / 2 self.sound_effect_credits.y = self.music_credits_game_author.y + ( 2 * self.sound_effect_credits.get_height()) self.sound_effect_credits_author = textitem.TextItem( "Bfxr is made by increpare", pygame.Color(255, 255, 255)) self.sound_effect_credits_author.set_size(font_size) self.sound_effect_credits_author.x = ( settings.SCREEN_WIDTH - self.sound_effect_credits_author.get_width()) / 2 self.sound_effect_credits_author.y = self.sound_effect_credits.y + self.sound_effect_credits_author.get_height( ) self.more_info_and_licenses = textitem.TextItem( "More info and licenses are in the readme", pygame.Color(200, 20, 200)) self.more_info_and_licenses.set_size(font_size) self.more_info_and_licenses.x = ( settings.SCREEN_WIDTH - self.more_info_and_licenses.get_width()) / 2 self.more_info_and_licenses.y = self.sound_effect_credits_author.y + ( 2 * self.more_info_and_licenses.get_height()) self.made_by_author = textitem.TextItem( "Olof Karlsson AKA Pigmassacre", pygame.Color(200, 0, 0)) self.made_by_author.set_size(font_size) self.made_by_author.x = (settings.SCREEN_WIDTH - self.made_by_author.get_width()) / 2 self.made_by_author.y = self.back_menu.y - ( 2 * self.made_by_author.get_height()) self.made_by_info = textitem.TextItem("Everything else made by me", pygame.Color(255, 255, 255)) self.made_by_info.set_size(font_size) self.made_by_info.x = (settings.SCREEN_WIDTH - self.made_by_info.get_width()) / 2 self.made_by_info.y = self.made_by_author.y - self.made_by_info.get_height( ) # The scale of the left and right looking pigs at the bottom of the screen. self.images_current_scale = 1 * settings.GAME_SCALE self.image_left = pygame.image.load( "res/splash/splash_bloody_left.png") self.image_left = pygame.transform.scale( self.image_left, (self.image_left.get_width() * self.images_current_scale, self.image_left.get_height() * self.images_current_scale)) self.image_right = pygame.image.load( "res/splash/splash_bloody_right.png") self.image_right = pygame.transform.scale( self.image_right, (self.image_right.get_width() * self.images_current_scale, self.image_right.get_height() * self.images_current_scale)) # We setup all menu transition. self.transition.speed = 1200 * settings.GAME_SCALE self.transition.setup_transition(self.back_menu, True, True, False, False) self.transition.setup_single_item_transition(self.pyganim_credits, True, True, False, False) self.transition.setup_single_item_transition( self.pyganim_credits_source_code, True, True, False, False) self.transition.setup_single_item_transition( self.pyganim_credits_author, True, True, False, False) self.transition.setup_single_item_transition(self.music_credits_title, True, True, False, False) self.transition.setup_single_item_transition( self.music_credits_after_game, True, True, False, False) self.transition.setup_single_item_transition( self.music_credits_title_author, True, True, False, False) self.transition.setup_single_item_transition(self.music_credits_game, True, True, False, False) self.transition.setup_single_item_transition( self.music_credits_game_author, True, True, False, False) self.transition.setup_single_item_transition(self.sound_effect_credits, True, True, False, False) self.transition.setup_single_item_transition( self.sound_effect_credits_author, True, True, False, False) self.transition.setup_single_item_transition( self.more_info_and_licenses, True, True, False, False) self.transition.setup_single_item_transition(self.made_by_author, True, True, False, False) self.transition.setup_single_item_transition(self.made_by_info, True, True, False, False) # And finally, we start the gameloop! self.gameloop()
def __init__(self, window_surface, main_clock, player_one, player_two, number_of_rounds, score, number_of_rounds_done = 0): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # The next screen to be started when gameloop ends. self.next_screen = gameover.GameOver # Keep track of the number of rounds. self.number_of_rounds = number_of_rounds # Keep track of the number of rounds we've done so far. self.number_of_rounds_done = number_of_rounds_done # When this is true, the game will keep on running for game_over_time milliseconds. self.game_over = False self.game_over_time = 1000.0 self.game_over_time_left = self.game_over_time # The score is kept to be sent to the gameover screen and also to be kept for "best-of" matches. self.score = score # Convert all the objects to a more efficient format. We do this here as a "preloading" sort of measure. block_normal.convert() block_strong.convert() block_weak.convert() paddle.convert() ball.convert() multiball.convert() doublespeed.convert() fire.convert() frost.convert() electricity.convert() reducer.convert() enlarger.convert() rocket.convert() explosion.convert() # Create and store the background. For now, we only have one background so we load that. In the future, the system supports # drawing any sort of background as long as those graphics are setup in the same way as "planks" are. self.game_background = background.Background("planks") # Store player one. self.player_one = player_one # Store player two. self.player_two = player_two # Create and store the level. self.game_level = level.Level(self.player_one, self.player_two, 1, 1, 1) # The list of available powerups to spawn. self.powerup_list = [multiball.Multiball, doublespeed.DoubleSpeed, fire.Fire, frost.Frost, electricity.Electricity, rocket.Rocket, enlarger.Enlarger, reducer.Reducer] # The rate at which powerups will perhaps be spawned. self.powerup_spawn_rate = 4000 # After this amount of time has passed, the powerup spawn chance will be increased. self.powerup_increase_spawn_rate = 3250 # This is the amount that all the spawn chances will increase by every powerup_increase_spawn_rate. self.powerup_spawn_chance_increase = 0.045 # The chance that a powerup will spawn when it should spawn. self.powerup_spawn_chance = 0.375 # The chance that another powerup will spawn if a powerup actually spawns. self.powerup_second_spawn_chance = 0.275 # The chance that a THIRD powerup will spawn if a second powerup actually spawns. self.powerup_third_spawn_chance = 0.175 # If there is already a doublespeed powerup on the gamefield, this is the chance that any further will spawn. self.powerup_second_speed_spawn_chance = 0.05 # Create the score texts. These are only displayed when the amount of rounds is greater than 0. item_side_padding = 25 * settings.GAME_SCALE font_dead_space = 1.5 * settings.GAME_SCALE self.player_one_score_text = textitem.TextItem(str(self.score[self.player_one]), pygame.Color(255, 255, 255)) self.player_one_score_text.set_size(27 * settings.GAME_SCALE) self.player_one_score_text.x = item_side_padding - (self.player_one_score_text.get_width() / 2.0) + font_dead_space self.player_one_score_text.y = (settings.SCREEN_HEIGHT - self.player_one_score_text.get_height()) / 2 self.player_two_score_text = textitem.TextItem(str(self.score[self.player_two]), pygame.Color(255, 255, 255)) self.player_two_score_text.set_size(27 * settings.GAME_SCALE) self.player_two_score_text.x = settings.SCREEN_WIDTH - item_side_padding - (self.player_two_score_text.get_width() / 2.0) + font_dead_space self.player_two_score_text.y = (settings.SCREEN_HEIGHT - self.player_two_score_text.get_height()) / 2 # We setup and play music. self.setup_music() # When this reaches powerup_spawn_rate, a powerup has a chance to spawn. self.powerup_spawn_time = 0 # When this reaches powerup_increase_spawn_rate, the chance for powerups to spawn will be increased. self.powerup_increase_spawn_time = 0 # We start a countdown before the game starts. When the countdown finishes, it calls start_game. self.countdown_screen = countdown.Countdown(self.main_clock, self.start_game) # We also hide the cursor. pygame.mouse.set_visible(False) # And finally, start the gameloop! self.gameloop()
def __init__(self, window_surface, main_clock): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # The next screen to be started when the gameloop ends. self.next_screen = None # This is a dictionary that contains information linked to certain imageitems. self.info = {} # This is a dictionary that maps transition methods to all all items. self.transition_method = {} # This contains the currently active function that displays the currently active information. self.active_info = None # This information is used to format the help texts. self.distance_from_screen_edge = 6 * settings.GAME_SCALE self.font_size = 6 * settings.GAME_SCALE self.max_width_of_text_line = (settings.SCREEN_WIDTH - (self.distance_from_screen_edge * 2)) # We create a gridmenu that allows the player to choose what item they want to read more about. self.help_menu = gridmenu.GridMenu(13) self.help_menu.y = 9 * settings.GAME_SCALE self.menu_list.append(self.help_menu) # The back button, displayed in the middle-bottom of the screen. back_button = textitem.TextItem("Back") self.back_menu = listmenu.ListMenu() self.back_menu.x = settings.SCREEN_WIDTH / 2 self.back_menu.y = settings.SCREEN_HEIGHT - (2 * back_button.get_height()) self.back_menu.add(back_button, self.back) self.back_menu.items[0].selected = True self.menu_list.append(self.back_menu) # We setup and add all the necessary items to the help_menu. root = "res/helpdata" for file in os.listdir(root): if file.endswith(".json"): self.setup_info(os.path.join(root, file)) self.help_menu.x = (settings.SCREEN_WIDTH - self.help_menu.get_width()) / 2 # Register all menus with each other. for a_menu in self.menu_list: a_menu.register_other_menus(self.menu_list) # We setup all menu transition. self.transition.speed = 20 * settings.GAME_FPS * settings.GAME_SCALE self.transition.setup_transition(self.help_menu, True, True, True, False) self.transition.setup_transition(self.back_menu, True, True, False, False) # Set the first item as the active information. if len(self.help_menu.items) > 0: self.view_info(self.help_menu.items[0]) # And finally, we start the gameloop! self.gameloop()
def setup_info(self, file_path): texts = [] # Parse the JSON file. json_file = open(file_path, "r") try: parsed_json = json.load(json_file) except IOError: print("IOError when reading JSON file.") finally: json_file.close() # We try to parse the image tag in the JSON file. If it isn't found, an error is raised. if "image" in parsed_json: try: image_item = imageitem.ImageItem(parsed_json["image"]) except: raise ValueError("The image path could not be read.") else: raise SyntaxError("Image key not found in JSON file.") # We try to parse the color tag in the JSON file. If it isn't found, we simply ignore it. if "color" in parsed_json: try: color_list = parsed_json["color"] except: raise ValueError("The color value could not be read.") try: useful.colorize_image( image_item.image, pygame.Color(color_list[0], color_list[1], color_list[2])) except: raise ValueError( "The given color values cannot be applied: [{0}, {1}, {2}]" .format(color_list[0], color_list[1], color_list[2])) # We add the image item to the help_menu here, so that the text can be be positioned properly. self.help_menu.add(image_item, self.view_info) # We try to parse the title tag in the JSON file. If it isn't found, an error is raised. if "title" in parsed_json: text = textitem.TextItem(parsed_json["title"], pygame.Color(255, 255, 255), 255, self.font_size) text.x = (settings.SCREEN_WIDTH - text.get_width()) / 2 text.y = self.help_menu.y + self.help_menu.get_height( ) + text.get_height() texts.append(text) previous_text = text else: raise SyntaxError("Title key not found in JSON file.") if "body" in parsed_json: body = parsed_json["body"] odd = True first_line = True wrapped_body = useful.wrap_multi_line( body, pygame.font.Font(textitem.TextItem.font_path, self.font_size), self.max_width_of_text_line) for line in wrapped_body: if odd: color = pygame.Color(255, 255, 255) else: color = pygame.Color(150, 150, 150) text = textitem.TextItem(line, color, 255, self.font_size) if first_line: text.x = self.distance_from_screen_edge text.y = previous_text.y + (2 * text.get_height()) first_line = False else: text.x = self.distance_from_screen_edge text.y = previous_text.y + text.get_height() texts.append(text) previous_text = text if line != "": odd = not odd else: raise SyntaxError("Body tag not found in JSON file.") if "quote" in parsed_json: quote = parsed_json["quote"] odd = True quotes = [] font = pygame.font.Font(textitem.TextItem.font_path, self.font_size) wrapped_quote = useful.wrap_multi_line(quote, font, self.max_width_of_text_line) for line in wrapped_quote: if odd: color = pygame.Color(200, 0, 200) else: color = pygame.Color(200, 50, 200) text = textitem.TextItem(line, color, 255, self.font_size) text.x = settings.SCREEN_WIDTH - text.get_width( ) - self.distance_from_screen_edge quotes.append(text) texts.append(text) previous_text = text if line != "": odd = not odd for quote_line in quotes: quote_line.y = ( self.back_menu.y - (2 * quote_line.get_height()) - len(quotes) * quote_line.get_height() ) + quotes.index(quote_line) * quote_line.get_height() self.info[image_item] = texts return image_item
def __init__(self, window_surface, main_clock): # Call the superconstructor. scene.Scene.__init__(self, window_surface, main_clock) # These are the connected and active joysticks. self.joysticks = [ pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count()) ] # The next screen to be started when the gameloop ends. self.next_screen = game.Game self.player_one_color = None self.player_two_color = None self.player_one_ai = None self.player_two_ai = None # Configure the GUI. distance_from_screen_edge = 9 * settings.GAME_SCALE # We create a gridmenu that allows the player to select the number of rounds they want to play. self.number_of_rounds_menu = gridmenu.GridMenu(5) # We set the default number of rounds to 1. temp_item = choiceitem.ChoiceItem(1) self.rounds(temp_item) # Add that item, and the other items to the menu. self.number_of_rounds_menu.add(temp_item, self.rounds) self.number_of_rounds_menu.add(choiceitem.ChoiceItem(3), self.rounds) self.number_of_rounds_menu.add(choiceitem.ChoiceItem(5), self.rounds) self.number_of_rounds_menu.add(choiceitem.ChoiceItem(7), self.rounds) self.number_of_rounds_menu.add(choiceitem.ChoiceItem(9), self.rounds) self.number_of_rounds_menu.x = ( settings.SCREEN_WIDTH - self.number_of_rounds_menu.get_width()) / 2.0 self.number_of_rounds_menu.y = distance_from_screen_edge * 3 self.menu_list.append(self.number_of_rounds_menu) # The text displayed over the rounds menu. self.number_of_rounds_text = textitem.TextItem( "Rounds", pygame.Color(255, 255, 255)) self.number_of_rounds_text.x = ( settings.SCREEN_WIDTH - self.number_of_rounds_text.get_width()) / 2.0 self.number_of_rounds_text.y = self.number_of_rounds_menu.y - ( self.number_of_rounds_text.get_height() * 2) # The color menu for player one. self.color_menu_one = self.setup_color_menu(self.color_one) self.ai_menu_one = self.setup_ai_menu(self.ai_one) ai_menu_offset = self.color_menu_one.offset * 2 self.color_menu_one.x = ( settings.SCREEN_WIDTH - self.color_menu_one.get_width() - self.ai_menu_one.get_width() - ai_menu_offset) / 5.0 self.color_menu_one.y = settings.SCREEN_HEIGHT / 2.0 self.ai_menu_one.x = self.color_menu_one.x + self.color_menu_one.get_width( ) + ai_menu_offset self.ai_menu_one.y = self.color_menu_one.y self.menu_list.append(self.color_menu_one) self.menu_list.append(self.ai_menu_one) # The text above the color menu for player one. self.player_one_text = textitem.TextItem(settings.PLAYER_ONE_NAME, pygame.Color(255, 255, 255)) self.player_one_text.x = self.color_menu_one.x + (( (self.color_menu_one.get_width() + self.ai_menu_one.get_width() + ai_menu_offset) - self.player_one_text.get_width()) / 2.0) self.player_one_text.y = self.color_menu_one.y - ( self.player_one_text.get_height() * 2) # The color menu for player two. self.color_menu_two = self.setup_color_menu(self.color_two) self.ai_menu_two = self.setup_ai_menu(self.ai_two) ai_menu_offset = self.color_menu_two.offset * 2 self.color_menu_two.x = settings.SCREEN_WIDTH - ( (settings.SCREEN_WIDTH - self.color_menu_two.get_width() - self.ai_menu_two.get_width() - ai_menu_offset) / 5.0) - self.color_menu_two.get_width() self.color_menu_two.y = settings.SCREEN_HEIGHT / 2.0 self.ai_menu_two.x = self.color_menu_two.x - self.ai_menu_two.get_width( ) - self.color_menu_two.offset * 2 self.ai_menu_two.y = self.color_menu_two.y self.menu_list.append(self.color_menu_two) self.menu_list.append(self.ai_menu_two) # The text above the color menu for player two. self.player_two_text = textitem.TextItem(settings.PLAYER_TWO_NAME, pygame.Color(255, 255, 255)) self.player_two_text.x = self.ai_menu_two.x + (( (self.color_menu_two.get_width() + self.ai_menu_two.get_width() + ai_menu_offset) - self.player_two_text.get_width()) / 2.0) self.player_two_text.y = self.color_menu_two.y - ( self.player_two_text.get_height() * 2) # The back button, displayed in the bottom-left corner of the screen. back_button = textitem.TextItem("Back") self.back_menu = listmenu.ListMenu() self.back_menu.x = distance_from_screen_edge + ( back_button.get_width() / 2.0) self.back_menu.y = settings.SCREEN_HEIGHT - (2 * back_button.get_height()) self.back_menu.add(back_button, self.back) self.back_menu.items[0].selected = True self.menu_list.append(self.back_menu) # The start button, displayed in the bottom-right corner of the screen. start_button = textitem.TextItem("Start") self.start_menu = listmenu.ListMenu() self.start_menu.x = settings.SCREEN_WIDTH - distance_from_screen_edge - ( start_button.get_width() / 2.0) self.start_menu.y = settings.SCREEN_HEIGHT - ( 2 * start_button.get_height()) self.start_menu.add(start_button, self.start) self.menu_list.append(self.start_menu) # Register all menus with each other. for a_menu in self.menu_list: a_menu.register_other_menus(self.menu_list) # We setup all menu transition. self.transition.setup_transition(self.number_of_rounds_menu, True, True, False, False) self.transition.setup_single_item_transition( self.number_of_rounds_text, True, True, True, False) self.transition.setup_transition(self.color_menu_one, True, False, False, True) self.transition.setup_transition(self.ai_menu_one, True, False, False, True) self.transition.setup_single_item_transition(self.player_one_text, True, False, True, False) self.transition.setup_transition(self.color_menu_two, False, True, False, True) self.transition.setup_transition(self.ai_menu_two, True, False, False, True) self.transition.setup_single_item_transition(self.player_two_text, False, True, True, False) self.transition.setup_transition(self.back_menu, True, False, False, True) self.transition.setup_transition(self.start_menu, False, True, False, True) # And finally, we start the gameloop! self.gameloop()
def setup_menus(self): # This is the distance from the screen edges to the GUI elements item_side_padding = textitem.TextItem.font_size # We setup and position all the needed textitems and menus. self.rounds_left_text = textitem.generate_list_from_string( "Rounds Left") length_of_rounds_left_text = sum( letter_item.get_width() for letter_item in self.rounds_left_text) last_offset = 0 for letter_item in self.rounds_left_text: letter_item.x = ( (settings.SCREEN_WIDTH - length_of_rounds_left_text) / 2.0) + last_offset letter_item.y = item_side_padding last_offset += letter_item.get_width() color = copy.copy(self.winner.color) h = color.hsla[0] + ((360 / (len(self.rounds_left_text) * 2)) * self.rounds_left_text.index(letter_item)) h %= 360 color.hsla = (h, color.hsla[1], color.hsla[2], color.hsla[3]) letter_item.set_color(color) self.passed_time = 0 self.rounds_left_number_text = textitem.TextItem( str(self.number_of_rounds - self.number_of_rounds_done), pygame.Color(255, 255, 255)) self.rounds_left_number_text.set_size(18 * settings.GAME_SCALE) self.rounds_left_number_text.x = ( settings.SCREEN_WIDTH - self.rounds_left_number_text.get_width()) / 2 self.rounds_left_number_text.y = self.rounds_left_text[ 0].y + self.rounds_left_number_text.get_height() self.player_one_score_text = textitem.TextItem( str(self.score[self.player_one]), pygame.Color(255, 255, 255)) self.player_one_score_text.set_size(27 * settings.GAME_SCALE) self.player_one_score_text.x = (settings.SCREEN_WIDTH - self.player_one_score_text.get_width() ) / 4 + 1.5 * settings.GAME_SCALE self.player_one_score_text.y = ( settings.SCREEN_HEIGHT - self.player_one_score_text.get_height()) / 2 self.player_one_score_text.set_color(self.player_one.color) self.player_one_text = textitem.TextItem(self.player_one.name, pygame.Color(255, 255, 255)) self.player_one_text.x = self.player_one_score_text.x + ( (self.player_one_score_text.get_width() - self.player_one_text.get_width()) / 2) self.player_one_text.y = self.player_one_score_text.y - ( 2 * self.player_one_text.get_height()) self.player_one_text.set_color(self.player_one.color) self.player_two_score_text = textitem.TextItem( str(self.score[self.player_two]), pygame.Color(255, 255, 255)) self.player_two_score_text.set_size(27 * settings.GAME_SCALE) self.player_two_score_text.x = 3 * ( (settings.SCREEN_WIDTH - self.player_two_score_text.get_width()) / 4) + 1.5 * settings.GAME_SCALE self.player_two_score_text.y = ( settings.SCREEN_HEIGHT - self.player_two_score_text.get_height()) / 2 self.player_two_score_text.set_color(self.player_two.color) self.player_two_text = textitem.TextItem(self.player_two.name, pygame.Color(255, 255, 255)) self.player_two_text.x = self.player_two_score_text.x + ( (self.player_two_score_text.get_width() - self.player_two_text.get_width()) / 2) self.player_two_text.y = self.player_two_score_text.y - ( 2 * self.player_two_text.get_height()) self.player_two_text.set_color(self.player_two.color) quit_button = textitem.TextItem("Quit") self.quit_menu = listmenu.ListMenu( item_side_padding + (quit_button.get_width() / 2), settings.SCREEN_HEIGHT - item_side_padding - quit_button.get_height()) self.quit_menu.add(quit_button, self.maybe_quit) self.menu_list.append(self.quit_menu) next_match_button = textitem.TextItem("Next Round") self.next_match_menu = listmenu.ListMenu( settings.SCREEN_WIDTH - item_side_padding - (next_match_button.get_width() / 2), settings.SCREEN_HEIGHT - item_side_padding - next_match_button.get_height()) self.next_match_menu.add(next_match_button, self.next_match) self.next_match_menu.items[0].selected = True self.menu_list.append(self.next_match_menu) # Register all menus with each other. This is for gui.traversals sake, so it knows that there are more than one menu to traverse upon. for a_menu in self.menu_list: a_menu.register_other_menus(self.menu_list)