def run_credits(display, images): pygame.mixer.music.load(get_file(fixPath("music/stores.mp3"))) pygame.mixer.music.play() clock = pygame.time.Clock() done = False text = [] time_passed = 0 for e, x in enumerate(credits_text.split("\n")): text.append( list( retro_text((400, 24 * e + 10), display, 14, x, font="sans", anchor="midtop", res=11))) scroll = -600 scroll_rate = 35 while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key in (pygame.K_RETURN, pygame.K_ESCAPE): done = True if scroll > 1000: done = True display.blit(images["background"], (0, 0)) for line in text: if line[1][1] - scroll < 100: if line[0].get_width() <= 3: text.remove(line) continue s1 = line[0].get_width() if line[1][1] - scroll > 90: s1 *= 1.025 else: s1 /= 1.03 line[0] = pygame.transform.scale( line[0], (round(s1), line[0].get_height())) r2 = pygame.Rect((0, 0), line[0].get_size()) r2.center = line[1].center line[1] = r2 display.blit(line[0], (line[1][0], line[1][1] - scroll)) scroll += scroll_rate * time_passed pygame.display.update() time_passed = clock.tick(60) / 1000 pygame.mixer.music.stop()
def __init__(self): self.options = saves.load_options() fullscreen = 0 if self.options["fullscreen"]: fullscreen = pygame.FULLSCREEN self.Display = pygame.display.set_mode( (800, 600), fullscreen | pygame.HWSURFACE | pygame.HWACCEL | pygame.DOUBLEBUF) self.display = pygame.Surface((800, 600)) pygame.display.set_caption("Interplanetary Invaders") pygame.display.set_icon(pygame.image.load(get_file("icon.png"))) t1 = time.time() self.images, num = auto_load.fetch_images() t2 = time.time() print( colorize(f"Loaded {num} images in {round(t2-t1, 2)} seconds", "green")) self.menu() self.cat = []
def confirm_delete(self): stuff_rect = pygame.Rect(0, 0, 300, 400) stuff_rect.center = self.display.get_rect().center options = ["No", "Yes"] sel = 0 done = False while not done: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key in (pygame.K_UP, pygame.K_w): sel = 0 if event.key in (pygame.K_DOWN, pygame.K_s, pygame.K_x): sel = 1 if event.key == pygame.K_TAB: sel = not sel if event.key == pygame.K_RETURN: if sel: os.remove( fixPath( get_file( f"data/profile{self.profile_selected}") )) self.profiles[ self. profile_selected] = f"New Profile #{self.profile_selected + 1}" self.pause_motion = True done = True if not self.options_lock: self.draw_stars() self.draw_menu_box(stuff_rect) self.draw_items(options, sel, stuff_rect) retro_text(stuff_rect.move(0, 5).midtop, self.display, 14, "Are you sure you want", anchor="midtop") retro_text(stuff_rect.move(0, 20).midtop, self.display, 14, f"to erase Profile #{self.profile_selected + 1}?", anchor="midtop") pygame.display.update()
import shelve import os from copy import deepcopy from ii_game.scripts.get_file import get_file from ii_game.scripts.utils import fixPath debugMode = True DATA_PATH = "data/" DATA_PATH = get_file(DATA_PATH) if not os.path.exists(DATA_PATH): os.mkdir(DATA_PATH) if debugMode: print(f"Directory \"{DATA_PATH}\" found missing!") def load_profile(index): profile = {} if debugMode: if not os.path.exists(f"{DATA_PATH}/profile{index}"): print(f"Profile {index} missing!") with shelve.open(f"{DATA_PATH}/profile{index}") as data: profile["money"] = data.get("money", 0) profile["hiscore"] = data.get("hiscore", 0) profile["moons_locked"] = data.get("moons_locked", True) profile["exoplanets_locked"] = data.get("exoplanets_locked", True) profile["planet"] = data.get("planet", planets.Earth) profile["new"] = data.get("new", True)
"""Autoloader gets images from IMAGE_PATH and puts in in a dictionary""" import sys import os import pygame from ii_game.scripts.get_file import get_file from ii_game.scripts.utils import colorize, fixPath IMAGE_PATH = get_file(fixPath("images/bitmap/")) def remove_extension(filename): """Remove the extension from a filename string""" return filename[:filename.index(".")] def fetch_images(): """Collect images from the IMAGE_PATH and return dictionary with pygame.Surface objects as values and their names as keys""" names = [] images = [] num = 0 print("Loading... \n") mx = 0 for data in os.walk(IMAGE_PATH): for collect_file in data[2]: if not collect_file.startswith("."): mx += 1 for data in os.walk(IMAGE_PATH): root = data[0]
def __init__(self, display, images, options=False, simple=False): self.images = images self.display = display self.options_lock = options if options: self.background = display.copy() try: screenshot_count = len( os.listdir(fixPath(get_file("data/screenshots")))) except FileNotFoundError: screenshot_count = 0 s = "s" if screenshot_count == 1: s = "" self.options = [ "Volume", "Cache Screenshots", f"Delete {screenshot_count} Screenshot{s}", "Stretch to Fullscreen", "Save", "Cancel" ] self.DEL = f"Delete {screenshot_count} Screenshot{s}" self.options_dict = saves.load_options() self.profiles = [ "Profile #1", "Profile #2", "Profile #3", "Profile #4", "Profile #5", "Back" ] self.profile_selected = 0 self.pause_motion = False for e, p in enumerate(self.profiles[:-1]): if saves.load_profile(e)["new"]: self.profiles[e] = f"New Profile #{e + 1}" if simple: return if not options: if not os.path.exists(fixPath(get_file("data/menuStarsCache"))): for x in range(300): stars.main() with shelve.open(fixPath( get_file("data/menuStarsCache"))) as data: data["stars"] = stars.stars else: with shelve.open(fixPath( get_file("data/menuStarsCache"))) as data: stars.stars = data["stars"] self.frame = 1 self.frame_rate = 1 / 120 self.frame_time = 0 self.finished = False self.time_passed = 0 self.star_rotation = 0 self.rotation_speed = 20 self.done = False self.item_selected = 0 self.items = ["Play", "Options", "Credits", "Quit"] self.play_mode = False self.options_mode = options self.option_selected = 0 self.text_time = 0 self.text_rate = 0.1 self.text = "Interplanetary Invaders" self.bool_images = [self.images["x"], self.images["check"]] pygame.mixer.music.stop() pygame.mixer.music.load(fixPath(get_file("audio/music/MainMenu.mp3"))) pygame.mixer.music.play(-1)
def events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit() if event.type == pygame.MOUSEBUTTONDOWN: if not self.finished: self.finished = True print(self.text) self.text = "" if event.type == pygame.KEYDOWN: if event.key == pygame.K_F2: screenshot.capture("M", self.display) if not self.finished: self.finished = True print(self.text) self.text = "" else: item = self.item_selected items = self.items if self.options_mode: item = self.option_selected items = self.options if self.play_mode: item = self.profile_selected items = self.profiles if event.key == pygame.K_ESCAPE: self.exit() if event.key == pygame.K_UP: item -= 1 if event.key == pygame.K_DOWN: item += 1 if item < 0: item = 0 if item >= len(items): item = len(items) - 1 if self.options_mode: self.option_selected = item if self.play_mode: self.profile_selected = item elif self.play_mode: pass else: self.item_selected = item if self.options_mode: op = self.options[self.option_selected] if op == "Volume": vol = self.options_dict["volume"] if event.key == pygame.K_LEFT: vol -= .1 if event.key == pygame.K_RIGHT: vol += .1 if vol < 0: vol = 0 if vol > 1: vol = 1 self.options_dict["volume"] = vol if op == "Cache Screenshots": if event.key == pygame.K_RETURN: self.options_dict[ "cache_screen_shots"] = not self.options_dict[ "cache_screen_shots"] if op == "Stretch to Fullscreen": if event.key == pygame.K_RETURN: self.options_dict[ "fullscreen"] = not self.options_dict[ "fullscreen"] if op == self.DEL: if event.key == pygame.K_RETURN: try: for e, x in enumerate( os.listdir( fixPath( get_file( "data/screenshots")))): os.remove( fixPath( get_file("data/screenshots/")) + x) print( colorize(f"Deleted screenshots: {e+1}", "green")) self.options[self.options.index( self.DEL)] = "Deleted screenshots" self.DEL = "Deleted screenshots" except FileNotFoundError: print( colorize( "Failed to delete screenshots!", "fail")) print( colorize("File not found error.", "fail")) if event.key == pygame.K_x: if self.play_mode: if self.profile_selected < 5 and not self.profiles[ self.profile_selected].startswith("New"): self.confirm_delete() if event.key == pygame.K_RETURN: if self.options_mode: sel = self.option_selected if self.options[sel] == "Cancel": self.options_mode = False self.options_dict = saves.load_options() if self.options[sel] == "Save": self.options_mode = False saves.save_data("options", self.options_dict) if not self.options_mode and self.options_lock: self.done = True elif self.play_mode: if self.profiles[self.profile_selected] == "Back": self.play_mode = False else: profile = saves.load_profile( self.profile_selected) profile["new"] = False saves.save_data(self.profile_selected, profile) self.done = True else: sel = self.item_selected if self.items[sel] == "Play": self.play_mode = True if self.items[sel] == "Options": self.options_mode = True if self.items[sel] == "Quit": self.exit() if self.items[sel] == "Credits": run_credits(self.display, self.images)
import os import pygame import time import copy from ii_game.scripts.retro_text import retro_text from ii_game.scripts.sound import Sound from ii_game.scripts.utils import fixPath, colorize from ii_game.scripts.get_file import get_file from ii_game.scripts import saves directory = get_file(fixPath("data/screenshots")) pygame.init() sound = Sound(fixPath("audio/click.wav")) options = saves.load_options() save_for_later = {} def capture(profile_num, surf, ingame = False): surf = copy.copy(surf) data = time.localtime() if not os.path.exists(directory): print(colorize("Screenshot directory missing.", "warning")) os.mkdir(directory) name = f"ii{profile_num}_{data.tm_year}{data.tm_mon}{data.tm_mday}{data.tm_hour}{data.tm_min}{data.tm_sec}" done_this = False p = fixPath(directory+"/"+name+".png") while os.path.exists(p) or name in save_for_later: if not done_this: name += "_2" done_this = True
def __init__(self, display, images, profile, profile_selected, map_point): self.display = display self.images = images self.profile = profile self.profile_selected = profile_selected center = display.get_rect().center self.main_rect = pygame.Rect(0, 0, 750, 550) self.main_rect.center = center self.list_rect = pygame.Rect(0, 0, 730, 300) self.list_rect.midbottom = self.main_rect.move(0, -10).midbottom self.buy_rect = pygame.Rect(0, 0, 355, 290) self.buy_rect.topleft = self.list_rect.move(5, 5).topleft self.inv_rect = self.buy_rect.copy() self.inv_rect.topleft = self.buy_rect.move(5, 0).topright self.item_rects = [self.buy_rect, self.inv_rect] self.items_rect = pygame.Rect(0, 0, 200, 40) self.items_rect.topright = self.main_rect.move(-5, 5).topright self.missiles_rect = self.items_rect.copy() self.missiles_rect.topleft = self.items_rect.move(0, 4).bottomleft self.licenses_rect = self.items_rect.copy() self.licenses_rect.topleft = self.missiles_rect.move(0, 4).bottomleft self.vehicles_rect = self.items_rect.copy() self.vehicles_rect.topleft = self.licenses_rect.move(0, 4).bottomleft self.drones_rect = self.items_rect.copy() self.drones_rect.topleft = self.vehicles_rect.move(0, 4).bottomleft self.money_rect = self.items_rect.copy() self.money_rect.w *= 1.35 self.money_rect.midbottom = self.inv_rect.move(0, -10).midbottom self.data_rect = pygame.Rect(0, 0, 450, 225) self.data_rect.topleft = self.main_rect.move(5, 5).topleft self.rects = [ self.items_rect, self.missiles_rect, self.licenses_rect, self.vehicles_rect, self.drones_rect ] self.rect_names = [ "Items", "Missiles", "Licenses", "Vehicles", "Drones" ] self.rect_sel = 0 self.done = False self.clock = pygame.time.Clock() self.items = store_data.getStuff("items", profile) self.missiles = {} self.licenses = store_data.getStuff("licenses", profile) self.vehicles = store_data.getStuff("vehicles", profile) self.drones = {} self.mapPoint = map_point if not hasattr(self.mapPoint, "store_data"): self.catagories = [ self.items, self.missiles, self.licenses, self.vehicles, self.drones ] self.mapPoint.store_data = self.catagories else: self.catagories = self.mapPoint.store_data self.sel_num = [0, 0, 0, 0, 0] self.purchase_rect = pygame.Rect(0, 0, 175, 50) self.purchase_rect.midbottom = self.buy_rect.move(0, -5).midbottom self.exit_rect = pygame.Rect(0, 0, 275, 50) self.exit_rect.midbottom = self.purchase_rect.move(0, -5).midtop pygame.mixer.music.load(get_file(fixPath("music/stores.mp3"))) pygame.mixer.music.play(-1)
def __init__(self, filename): filename = get_file(filename) super().__init__(filename) self.set_volume(shared["options"]["volume"])