def scores_page(): pygame.mixer.music.stop() page_title_surf, page_title_rect = text_objects( "High Scores", G.BIG_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.15), ) instructions_surf, instructions_rect = text_objects( "Press space to return to menu", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.9), ) G.SCREEN.fill(G.WHITE) G.SCREEN.blit(G.BACKGROUND_2.image, G.BACKGROUND_2.rect) G.SCREEN.blit(page_title_surf, page_title_rect) G.SCREEN.blit(instructions_surf, instructions_rect) if not os.path.isfile("scores.json"): no_scores_surf, no_scores_rect = text_objects( "No Scores Found", G.BIG_TEXT, G.RED, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.5), ) G.SCREEN.blit(no_scores_surf, no_scores_rect) else: with open('scores.json') as scores_file: score_data = json.load(scores_file) sorted_records = sorted( score_data["scores"], key=lambda k: k["score"], reverse=True ) y_pos = 0.28 for i, record in enumerate(sorted_records[0:10]): record_surf, record_rect = text_objects( f"{i+1}) {record['player']} - {record['score']}", G.SMALL_TEXT, G.GOLD, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * y_pos), ) G.SCREEN.blit(record_surf, record_rect) y_pos += 0.06 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: game_menu.game_menu() pygame.display.update() G.CLOCK.tick(15)
def paused(): """Pause screen function""" pygame.mixer.music.pause() pause_surf_1, pause_rect_1 = text_objects( "PAUSED", G.GIANT_TEXT, G.LIGHT_YELLOW, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.35), ) pause_instructions_surf_1, pause_instructions_rect_1 = text_objects( "Press 'ESC' to resume", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.55), ) pause_instructions_surf_2, pause_instructions_rect_2 = text_objects( "Press 'm' to return to menu", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.65), ) pause_instructions_surf_3, pause_instructions_rect_3 = text_objects( "Press 'q' to quit", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.75), ) G.SCREEN.fill(G.WHITE) G.SCREEN.blit(G.BACKGROUND_2.image, G.BACKGROUND_2.rect) G.SCREEN.blit(pause_surf_1, pause_rect_1) G.SCREEN.blit(pause_instructions_surf_1, pause_instructions_rect_1) G.SCREEN.blit(pause_instructions_surf_2, pause_instructions_rect_2) G.SCREEN.blit(pause_instructions_surf_3, pause_instructions_rect_3) pygame.display.update() while G.PAUSE: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.KEYUP: if event.key == pygame.K_ESCAPE: unpause() if event.key == pygame.K_m: menu.game_menu() if event.key == pygame.K_q: exit_game() G.CLOCK.tick(15)
def about_page(): """The about page of RAstral Rampart""" pygame.mixer.music.stop() credit_surf_1, credit_rect_1 = text_objects( "RAstral Rampart was created by Caleb Werth and", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.35), ) credit_surf_2, credit_rect_2 = text_objects( "Russell Spry. Original idea by Aaron Werth.", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.40), ) instructions_surf, instructions_rect = text_objects( "Press space to return to menu", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.7), ) pygame_powered = pygame.image.load( "assets/pygame_powered.gif").convert_alpha() pygame_powered_rect = pygame_powered.get_rect( center=(G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.52)) G.SCREEN.fill(G.WHITE) G.SCREEN.blit(G.BACKGROUND_2.image, G.BACKGROUND_2.rect) G.SCREEN.blit(credit_surf_1, credit_rect_1) G.SCREEN.blit(credit_surf_2, credit_rect_2) G.SCREEN.blit(pygame_powered, pygame_powered_rect) G.SCREEN.blit(instructions_surf, instructions_rect) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: game_menu() pygame.display.update() G.CLOCK.tick(15)
def draw_controls2(self): control_surf, control_rect = text_objects( "Press 'ESC' to Pause!", G.TINY_TEXT, G.WHITE, ((G.DISPLAY_WIDTH * 0.109), (G.DISPLAY_HEIGHT * 0.94)), ) G.SCREEN.blit(control_surf, control_rect)
def draw_controls(self): control_surf, control_rect = text_objects( "Press 'SPACE' to Fire!", G.TINY_TEXT, G.WHITE, ((G.DISPLAY_WIDTH * 0.11), (G.DISPLAY_HEIGHT * 0.97)), ) G.SCREEN.blit(control_surf, control_rect)
def draw_score(self, score): scoreboard_surf, scoreboard_rect = text_objects( "Score: " + str(score), G.SMALL_TEXT, G.WHITE, ((G.DISPLAY_WIDTH * 0.065), (G.DISPLAY_HEIGHT * 0.025)), ) G.SCREEN.blit(scoreboard_surf, scoreboard_rect)
def message_display(text, color, font): pygame.mixer.Sound.stop(music) largeText = pygame.font.Font('freesansbold.ttf', font) TextSurf, TextRect = text_objects(text, largeText, color) TextRect.center = (screen_width / 2, screen_height / 2) gameDisplay.blit(TextSurf, TextRect) pygame.display.update() time.sleep(6) game_loop()
def draw_ammo(self, ammo): if ammo == 0: ammo_status = "Reloading" self.ammo_text = G.SMALL_ITALIC_TEXT else: ammo_status = str(ammo) self.ammo_text = G.SMALL_TEXT ammo_surf, ammo_rect = text_objects( "Ammo: " + ammo_status, self.ammo_text, G.WHITE, ((G.DISPLAY_WIDTH * 0.87), (G.DISPLAY_HEIGHT * 0.93)), ) G.SCREEN.blit(ammo_surf, ammo_rect)
def new_round(): pygame.mixer.music.stop() round_surf, round_rect = text_objects( f"ROUND {G.DIFFICULTY}", G.GIANT_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.5), ) G.SCREEN.fill(G.WHITE) G.SCREEN.blit(G.BACKGROUND_2.image, G.BACKGROUND_2.rect) G.SCREEN.blit(round_surf, round_rect) pygame.display.update() pygame.time.delay(2500) game.game_loop()
def instructions(): #displays instruction text on new display instructions = True while instructions: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() functions.gameDisplay.fill(white) TextSurf, TextRect = functions.text_objects( "Welcome to Farm Life! A relaxing game about farming!", smallText) functions.gameDisplay.blit(TextSurf, (90, 125)) TextSurf, TextRect = functions.text_objects( "Go to the store and buy some seeds!", smallText) functions.gameDisplay.blit(TextSurf, (90, 140)) TextSurf, TextRect = functions.text_objects( "Plant them and fertilize them to grow!", smallText) functions.gameDisplay.blit(TextSurf, (90, 155)) TextSurf, TextRect = functions.text_objects( "Yellow plants mean they need more fertilizer, green plants mean they are ready to pick!", smallText) functions.gameDisplay.blit(TextSurf, (90, 170)) TextSurf, TextRect = functions.text_objects( "Pick and sell your grown plants back to the store!", smallText) functions.gameDisplay.blit(TextSurf, (90, 185)) TextSurf, TextRect = functions.text_objects( "Use your money to buy more plants and plots!", smallText) functions.gameDisplay.blit(TextSurf, (90, 200)) TextSurf, TextRect = functions.text_objects( "A Note: in the top left corner it shows you what is selected.", smallText, red) functions.gameDisplay.blit(TextSurf, (90, 225)) TextSurf, TextRect = functions.text_objects( "If it says “None” and you try to perform an action (like picking a plant) the game may crash!", smallText, red) functions.gameDisplay.blit(TextSurf, (90, 240)) functions.button("Back", 350, 510, 100, 50, silver, white, game_intro) pygame.display.update() functions.clock.tick(15)
def game_intro(): #start the game_intro loop. generates two buttons, one that runs main game_loop one that runs instructions intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() functions.gameDisplay.fill(white) TextSurf, TextRect = functions.text_objects( 'Farm Life: Live your best farm life!', largeText) TextRect.center = ((functions.display_width / 2), (functions.display_height / 2)) functions.gameDisplay.blit(TextSurf, TextRect) functions.button("Farm!", 350, 450, 100, 50, silver, white, game_loop) functions.button("Instructions!", 350, 510, 100, 50, silver, white, instructions) pygame.display.update() functions.clock.tick(15)
def game_loop(): #main game loop. determines what to display based on what pages are set to True in functions file crashed = False functions.characterSetUp("Bobby") print("GAME START") while crashed == False: functions.gameDisplay.fill(white) functions.rect(0, 400, 800, 200, lightYellow) for event in pygame.event.get(): if event.type == pygame.QUIT: crashed = True if functions.viewField == True: #creates fertilize, plant/pick, and store buttons. runs generateField function. #if Fertilize clicked runs functions.field.furtilize #if plant/pick clicked runs functions.plantPick #if store clicked runs functions.switchStore functions.threeButtons("Fertilize", "Plant/Pick", "Store", functions.field.furtilize, functions.plantPick, functions.switchStoreField, functions.selection, True) functions.generateField() elif functions.viewStore == True: #creates buy, sell, and field buttons. #if buy clicked runs functions.switchStoreBuy #if sell clicked runs functions.switchStoreSell #if field clicked runs functions.switchStoreField TextSurf, TextRect = functions.text_objects( "Welcome to the store!", largeText) functions.gameDisplay.blit( TextSurf, ((190), ((functions.display_height - 400) / 2))) functions.threeButtons("Buy", "Sell", "Field", functions.switchStoreBuy, functions.switchStoreSell, functions.switchStoreField, None, None) elif functions.buy == True: #creates back button. runs functions.generateStore function. #if back clicked runs functions.switchStoreBuy #if an item is selected, creates buy button #if sell button clicked runs functions.purchase functions.button("Back", 166.67, 450, 150, 100, silver, white, functions.switchStoreBuy) if functions.selection != None and functions.player.bank - functions.selection.cost > 0: functions.button("Buy", 483.37, 450, 150, 100, silver, white, functions.purchase, functions.selection, True) functions.generateStore() elif functions.viewSell == True: #creates back button. runs functions.showInv function. #if back clicked runs functions.switchStoreSell #if item is selected creates sell button #if sell is clicked runs functions.sell functions.button("Back", 166.67, 450, 150, 100, silver, white, functions.switchStoreSell) if functions.selection != None: functions.button("Sell", 483.37, 450, 150, 100, silver, white, functions.sell, functions.selection) functions.showInv() elif functions.plant == True: #creates back button. runs functions.showInv function. #if back clicked runs functions.switchFieldPlant #if item is selected creates plant button #if sell is clicked runs functions.plant functions.showInv() functions.button("Back", 166.67, 450, 150, 100, silver, white, functions.switchFieldPlant) if functions.selection != None: functions.button("Plant", 483.37, 450, 150, 100, silver, white, functions.field.plant, functions.selection, True) pygame.display.update() functions.clock.tick(60)
def game_over(): """Game over screen function""" player_name = random.choice(NAMES) final_score = G.SCORE * G.DIFFICULTY score_entry = {"player": player_name, "score": final_score} G.SCORE = 0 if os.path.isfile("scores.json"): with open('scores.json', 'r') as scores_file: score_data = json.load(scores_file) score_data['scores'].append(score_entry) with open('scores.json', 'w') as scores_file: json.dump(score_data, scores_file) else: score_data = {} score_data['scores'] = [] score_data['scores'].append(score_entry) with open('scores.json', 'w') as scores_file: json.dump(score_data, scores_file) pygame.mixer.music.pause() game_over_surf_1, game_over_rect_1 = text_objects( "GAME OVER", G.GIANT_TEXT, G.RED, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.25), ) player_name_surf, player_name_rect = text_objects( f"Thanks for playing, {player_name}!", G.MEDIUM_TEXT, G.LIGHT_YELLOW, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.40), ) player_score_surf, player_score_rect = text_objects( f"Final Score: {final_score}", G.MEDIUM_TEXT, G.LIGHT_YELLOW, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.46), ) game_over_surf_2, game_over_rect_2 = text_objects( "Press 'p' to play again", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.60), ) game_over_surf_3, game_over_rect_3 = text_objects( "Press 'm' to return to menu", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.70), ) game_over_surf_4, game_over_rect_4 = text_objects( "Press 'q' to Quit", G.MEDIUM_TEXT, G.WHITE, (G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.80), ) G.SCREEN.fill(G.WHITE) G.SCREEN.blit(G.BACKGROUND_2.image, G.BACKGROUND_2.rect) G.SCREEN.blit(game_over_surf_1, game_over_rect_1) G.SCREEN.blit(player_name_surf, player_name_rect) G.SCREEN.blit(player_score_surf, player_score_rect) G.SCREEN.blit(game_over_surf_2, game_over_rect_2) G.SCREEN.blit(game_over_surf_3, game_over_rect_3) G.SCREEN.blit(game_over_surf_4, game_over_rect_4) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.KEYUP: if event.key == pygame.K_p: G.DIFFICULTY = 1 G.PERMANENT_POWER_UPS["higher_max_health"] = 0 G.PERMANENT_POWER_UPS["higher_max_ammo"] = 0 new_round.new_round() if event.key == pygame.K_m: menu.game_menu() if event.key == pygame.K_q: exit_game() G.CLOCK.tick(15)
def game_menu(): """The menu for the game""" pygame.mixer.music.stop() pygame.mixer.music.load("assets/audio/bensound-endlessmotion.wav") pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play(-1) start_button = sprites.Button( G.SMALL_TEXT.render("Start", True, G.BLACK), ((G.DISPLAY_WIDTH * 0.16), (G.DISPLAY_HEIGHT * 0.65), 100, 50), G.GREEN, new_round.new_round, ) about_button = sprites.Button( G.SMALL_TEXT.render("About", True, G.BLACK), ((G.DISPLAY_WIDTH * 0.33), (G.DISPLAY_HEIGHT * 0.5), 100, 50), G.LIGHT_YELLOW, about_page, ) scores_button = sprites.Button( G.SMALL_TEXT.render("Scores", True, G.BLACK), ((G.DISPLAY_WIDTH * 0.53), (G.DISPLAY_HEIGHT * 0.5), 100, 50), G.GOLD, scores.scores_page, ) quit_button = sprites.Button( G.SMALL_TEXT.render("Quit", True, G.BLACK), ((G.DISPLAY_WIDTH * 0.70), (G.DISPLAY_HEIGHT * 0.65), 100, 50), G.RED, exit_game, ) text_surf_title, text_rect_title = text_objects( "RAstral Rampart", G.BIG_TEXT, G.WHITE, ((G.DISPLAY_WIDTH * 0.5), (G.DISPLAY_HEIGHT * 0.2)), ) text_surf_space, text_rect_space = text_objects( "Press Space To Shoot!", G.MEDIUM_TEXT, G.WHITE, ((G.DISPLAY_WIDTH * 0.5), (G.DISPLAY_HEIGHT * 0.32)), ) all_sprites_list = pygame.sprite.Group() projectile_list = pygame.sprite.Group() buttons_list = pygame.sprite.Group() all_sprites_list.add(start_button, about_button, quit_button, scores_button) buttons_list.add(start_button, about_button, quit_button, scores_button) gun = sprites.Gun((G.DISPLAY_WIDTH * 0.5, G.DISPLAY_HEIGHT * 0.875)) all_sprites_list.add(gun) G.DIFFICULTY = 1 G.SCORE = 0 G.PERMANENT_POWER_UPS["higher_max_health"] = 0 G.PERMANENT_POWER_UPS["higher_max_ammo"] = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: pygame.mixer.Sound.play(G.SHOOT_FX) projectile = sprites.Projectile( gun.rect.center, gun.angle, gun.image.get_height() * 0.5, ) all_sprites_list.add(projectile) projectile_list.add(projectile) all_sprites_list.update() for projectile in projectile_list: hit_button_list = pygame.sprite.spritecollide( projectile, buttons_list, False) for button in hit_button_list: button.function() if projectile.off_screen(): projectile.kill() G.SCREEN.fill(G.WHITE) G.SCREEN.blit(G.BACKGROUND_2.image, G.BACKGROUND_2.rect) G.SCREEN.blit(text_surf_title, text_rect_title) G.SCREEN.blit(text_surf_space, text_rect_space) all_sprites_list.draw(G.SCREEN) pygame.display.update() G.CLOCK.tick(60)