def draw(self, win_loss): """Draws all message displays and buttons, calls set_text()""" pygame.mixer.music.fadeout(750) if win_loss == "lose": pygame.mixer.music.load('music/Hero_Down.mp3') pygame.mixer.music.play(-1, start=1.5) if win_loss == "win": pygame.mixer.music.load('music/Amazing_Plan_Silent_Film_Dark.mp3') pygame.mixer.music.play(-1) # Define time for display minutes_elapsed = self.time_elapsed // minutes remaining_seconds = (self.time_elapsed % minutes) // seconds while True: # Check for quit for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # Show background gameDisplay.blit(backgroundImage.image, backgroundImage.rect) # Draw "Defeat/Victory" text if win_loss == "lose": self.set_text(self.center_x, self.game_y, "Defeat!", self.game_font) if win_loss == "win": self.set_text(self.center_x, self.game_y, "Victory!!", self.game_font) # Draw "Score" text self.set_text(self.center_x, self.score_y, "score: {}".format(self.score), self.score_font) # Draw "Time elapsed" text self.set_text( self.center_x, self.time_y, "Time: {0}:{1:02}".format(minutes_elapsed, remaining_seconds), self.time_font) # Draw quit button self.quit_button.draw() # Draw play button play = self.play_button.draw() if play == "play": return play # Draw main button main = self.main_button.draw() if main == "main": return main # Update game pygame.display.update() clock.tick(30)
def intro_loop(): """First loop seen, explains game story with links to other loops Note: Texts stored in gameText.py """ play_button = generalClass.Button( (450, display_height - 250), message="Play", action=game_loop, font_size=40, width=300, height=60) quit_button = generalClass.Button( (450, display_height - 180), message="Quit", action=sys.exit, font_size=40, width=300, height=60, color1=red, color2=bright_red) tower_info_button = generalClass.Button( (100, display_height - 320), message="Tower Types", action=tower_info_loop, font_size=40, width=300, height=60, color1=teal, color2=bright_teal) enemy_info_button = generalClass.Button( (100, display_height - 250), message="Enemy Types", action=enemy_info_loop, font_size=40, width=300, height=60, color1=yellow, color2=bright_yellow) settings_button = generalClass.Button( (100, display_height - 180), message="Difficulty", action=settings_loop, font_size=40, width=300, height=60, color1=orange, color2=bright_orange) font = pygame.font.SysFont('Comic Sans MS', 20, bold=True) title_font = pygame.font.SysFont('Comic Sans MS', 72, bold=True) while True: # Activate quit button for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # Draw background gameDisplay.blit(backgroundImage.image, backgroundImage.rect) # Draw title and intro text helpers.blit_text(gameDisplay, title, (100, 25), title_font, margin=50) helpers.blit_text(gameDisplay, intro_text, (100, 150), font, margin=100) # Draw buttons play_button.draw() quit_button.draw() enemy_info_button.draw() tower_info_button.draw() settings_button.draw() # Update game display pygame.display.update() clock.tick(30)
def settings_loop(): """Allow user to adjust difficulty settings with buttons and explanations Note: Texts located in gameText.py """ # Set buttons for difficulty selection and return to into_loop return_button = generalClass.Button( (50, 50), message="Return", action=intro_loop, font_size=40, width=200, height=60, color1=orange, color2=bright_orange) easy_button = generalClass.Button( (50, 200), message="Easy", action=easy_settings, font_size=40, width=200, height=60, color1=green, color2=bright_green, linked=True) medium_button = generalClass.Button( (50, 285), message="Medium", action=medium_settings, font_size=40, width=200, height=60, color1=yellow, color2=bright_yellow, linked=True) hard_button = generalClass.Button( (50, 370), message="Hard", action=hard_settings, font_size=40, width=200, height=60, color1=red, color2=bright_red, linked=True) # Set font for difficulty explanations font = pygame.font.SysFont('Comic Sans MS', 24, bold=True) while True: # Activate quit button for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # Draw background gameDisplay.blit(backgroundImage.image, backgroundImage.rect) # Draw setting descriptions helpers.blit_text(gameDisplay, easy_text, (275, 190), font, margin=-50) helpers.blit_text(gameDisplay, medium_text, (275, 275), font, margin=50) helpers.blit_text(gameDisplay, hard_text, (275, 360), font, margin=50) # Draw buttons return_button.draw() easy_button.draw(medium_button, hard_button) medium_button.draw(easy_button, hard_button) hard_button.draw(easy_button, medium_button) # Update game display pygame.display.update() clock.tick(30)
def tower_info_loop(): """Shows and explains towers with buttons to navigate Note: Texts located in gameText.py """ # Set buttons return_button = generalClass.Button( (50, 50), message="Return", action=intro_loop, font_size=40, width=200, height=60, color1=orange, color2=bright_orange) previous_button = generalClass.Button( (50, display_height - 100), message="Previous", action="backward", font_size=40, width=200, height=60, color1=red, color2=bright_red) next_button = generalClass.Button( (600, display_height - 100), message="Next", action="forward", font_size=40, width=200, height=60, color1=green, color2=bright_green) # Set font font = pygame.font.SysFont('Comic Sans MS', 18, bold=True) # Set towers basic = towerClass.BasicTower((160, 400), destroy=False) ice1 = towerClass.IceTower1((155, 250), destroy=False) ice2 = towerClass.IceTower2((155, 530), destroy=False) fire1 = towerClass.FireTower1((155, 250), destroy=False) fire2 = towerClass.FireTower2((155, 530), destroy=False) poison1 = towerClass.PoisonTower1((155, 250), destroy=False) poison2 = towerClass.PoisonTower2((155, 530), destroy=False) dark1 = towerClass.DarkTower1((155, 250), destroy=False) dark2 = towerClass.DarkTower2((155, 530), destroy=False) # Define index for navigating pages info_index = 0 while True: # Activate quit button for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # Draw background gameDisplay.blit(backgroundImage.image, backgroundImage.rect) pygame.draw.rect(gameDisplay, white, (100, 125, 650, 525)) # Draw buttons return_button.draw() next_tower = next_button.draw() previous_tower = previous_button.draw() # Define page navigation with index if next_tower: info_index += 1 if previous_tower: info_index -= 1 if info_index > 4: info_index = 0 if info_index < 0: info_index = 4 # Draw towers and tower info, pages indicated by index if info_index == 0: basic.draw() helpers.blit_text(gameDisplay, basic_tower_text, (225, 300), font, margin=125) if info_index == 1: ice1.draw() ice2.draw() helpers.blit_text(gameDisplay, ice1_text, (225, 135), font, margin=125) helpers.blit_text(gameDisplay, ice2_text, (225, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) if info_index == 2: fire1.draw() fire2.draw() helpers.blit_text(gameDisplay, fire1_text, (225, 135), font, margin=125) helpers.blit_text(gameDisplay, fire2_text, (225, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) if info_index == 3: poison1.draw() poison2.draw() helpers.blit_text(gameDisplay, poison1_text, (225, 135), font, margin=125) helpers.blit_text(gameDisplay, poison2_text, (225, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) if info_index == 4: dark1.draw() dark2.draw() helpers.blit_text(gameDisplay, dark1_text, (225, 135), font, margin=125) helpers.blit_text(gameDisplay, dark2_text, (225, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) # Update game display pygame.display.update() clock.tick(30)
def game_loop(): """Play the game This function sets up several buttons and texts to give info for game, then calls several other functions to run the game logic. """ # Game crashes if loading new music while music is paused pygame.mixer.music.unpause() # Start main game music pygame.mixer.music.fadeout(100) pygame.mixer.music.load('music/main_music_mesh2.wav') pygame.mixer.music.play(0) # Set static buttons pause_button = generalClass.Button( (20, 50), message="Pause", width=120, color1=gray, color2=white, action=pause_game) # Set game settings start_cash = settings.starting_gold enemy_spawn_rate = settings.spawn_rate passive_money_rate = settings.gold_generation difficulty = settings.difficulty # Set up game rules with several stat trackers score = generalClass.Tracker( (140, 20), start_stat=0, width=120, height=30, background_color=green, font="Comic Sans MS", font_size=20, text_color=black, prefix="Score: ") game_clock = generalClass.Tracker( (20, 20), start_stat=0, width=120, height=30, background_color=black, font="Comic Sans MS", font_size=20, text_color=white, special="clock") funds = generalClass.Tracker( (20, display_height - 90), start_stat=start_cash, width=100, height=30, background_color=black, font="Comic Sans MS", font_size=20, text_color=white, prefix="$") castle = generalClass.Tracker( (20, display_height - 60), start_stat=20, width=250, height=50, background_color=red, front_color=green, font="Comic Sans MS", font_size=30, text_color=white, special="castle") # Set the end screen end_screen = generalClass.EndScreen() # Set blank enemies list enemies_list = [] # # enemies_list.extend( # [enemies.Lizard(), enemies.Lizard(), enemies.Wolf(), # enemies.Wolf(), enemies.Orc(), enemies.Orc(), # enemies.Turtle(), enemies.Turtle(), enemies.Spider(), # enemies.Spider(), enemies.Spider(), enemies.Spider(), # enemies.Spider(), enemies.Spider()]) # Set towers' and missiles' lists bot_tower_list = [] bot_missile_list = [] top_tower_list = [] top_missile_list = [] # Call function to set up towers and missiles set_towers(bot_tower_locations, bot_tower_list, bot_missile_list) set_towers(top_tower_locations, top_tower_list, top_missile_list) # Set mage (for end game sequence) mage = enemies.Mage() # Actual game loop while True: # Set quit button and pause game listeners for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pause_game() # Update game_clock by 1 per frame game_clock.stat += 1 # Draw background gameDisplay.blit(backgroundImage.image, backgroundImage.rect) # Periodically add money if game_clock.stat % passive_money_rate == 0: funds.adjust(1) # Call function to add enemies if not mage.stop_spawn: add_enemies(game_clock.stat, enemies_list, enemy_spawn_rate, difficulty) # Call function to draw top-side towers draw_towers(top_tower_list, top_missile_list, funds, score, enemies_list) # Call function to draw enemies draw_enemies(enemies_list, castle) # Call function to draw bottom-side towers draw_towers(bot_tower_list, bot_missile_list, funds, score, enemies_list) # Call function to draw mage (end of game sequence) draw_mage(mage, game_clock, score, funds, enemies_list) # Draw game info panels tower_costs_display() funds.draw() castle.draw() score.draw() game_clock.draw() pause_button.draw() # Check for win/lose conditions win_lose(mage, end_screen, score, game_clock, castle) # Update game pygame.display.update() clock.tick(30)
def enemy_info_loop(): """Show and explain enemies with buttons to navigate Note: Texts located in gameText.py """ # Set buttons for page navigation return_button = generalClass.Button( (50, 50), message="Return", action=intro_loop, font_size=40, width=200, height=60, color1=orange, color2=bright_orange) previous_button = generalClass.Button( (50, display_height - 100), message="Previous", action="backward", font_size=40, width=200, height=60, color1=red, color2=bright_red) next_button = generalClass.Button( (600, display_height - 100), message="Next", action="forward", font_size=40, width=200, height=60, color1=green, color2=bright_green) # Set font font = pygame.font.SysFont('Comic Sans MS', 18, bold=True) # Set enemies, indicating stationary=True and destroy=False spider = enemies.Spider((180, 250), (190, 250), True, False) lizard = enemies.Lizard((180, 530), (190, 530), True, False) wolf = enemies.Wolf((180, 250), (190, 250), True, False) turtle = enemies.Turtle((180, 530), (190, 530), True, False) orc = enemies.Orc((180, 250), (190, 250), True, False) dragon = enemies.Dragon((180, 530), (190, 530), True, False) # Define index for page navigation info_index = 0 while True: # Activate quit button for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # Draw background gameDisplay.blit(backgroundImage.image, backgroundImage.rect) pygame.draw.rect(gameDisplay, white, (100, 125, 650, 525)) # Draw buttons return_button.draw() next_enemy = next_button.draw() previous_enemy = previous_button.draw() # Define page navigation with index if next_enemy: info_index += 1 if previous_enemy: info_index -= 1 if info_index > 2: info_index = 0 if info_index < 0: info_index = 2 # Draw enemies and enemy info, pages indicated by index if info_index == 0: spider.draw() lizard.draw() helpers.blit_text(gameDisplay, spider_text, (275, 135), font, margin=125) helpers.blit_text(gameDisplay, lizard_text, (275, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) if info_index == 1: wolf.draw() turtle.draw() helpers.blit_text(gameDisplay, wolf_text, (275, 135), font, margin=125) helpers.blit_text(gameDisplay, turtle_text, (275, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) if info_index == 2: orc.draw() dragon.draw() helpers.blit_text(gameDisplay, orc_text, (275, 135), font, margin=125) helpers.blit_text(gameDisplay, dragon_text, (275, 400), font, margin=125) pygame.draw.rect(gameDisplay, black, (150, 385, 550, 5)) # Update game display pygame.display.update() clock.tick(30)