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(fix_path(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(fix_path(get_file("data/menuStarsCache"))): for x in range(300): stars.main() with shelve.open(fix_path(get_file("data/menuStarsCache"))) as data: data["stars"] = stars.stars else: with shelve.open(fix_path(get_file("data/menuStarsCache"))) as data: stars.stars = data["stars"] pygame.mixer.music.stop() pygame.mixer.music.load(fix_path(get_file("audio/music/MainMenu.mp3"))) pygame.mixer.music.play(-1) 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"]]
def __init__(self): self.options = saves.load_options() fullscreen = 0 if self.options["fullscreen"]: fullscreen = pygame.FULLSCREEN pygame.display.set_icon(pygame.image.load(get_file("icon.png"))) self.Display = pygame.display.set_mode((800, 600), fullscreen | pygame.HWACCEL) self.display = pygame.Surface((800, 600)) pygame.display.set_caption("Interplanetary Invaders") t1 = time.time() self.images, num = auto_load.fetch_images(self.Display) t2 = time.time() self.first_time = True print( colorize(f"Loaded {num} images in {round(t2-t1, 2)} seconds", "green")) self.menu() self.cat = []
def events(self): for event in pygame.event.get(): joystick.Update(event) if event.type == pygame.QUIT: self.exit() if event.type == pygame.MOUSEBUTTONDOWN: if not self.finished and not self.options_lock: self.finished = True print(colorize(self.text, 'bold')) self.text = "" if event.type == pygame.KEYDOWN or joystick.WasEvent(): if not hasattr(event, "key"): event.key = None if event.key == pygame.K_F2 or joystick.JustPressedLB(): screenshot.capture("M", self.display) if not self.finished and not self.options_lock: self.finished = True print(colorize(self.text, 'bold')) 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 or joystick.BackEvent(): if self.play_mode: self.play_mode = False elif self.options_mode: self.options_mode = False self.options_dict = saves.load_options() else: self.exit() if event.key == pygame.K_UP or joystick.JustWentUp(): item -= 1 if (event.key == pygame.K_DOWN) or joystick.JustWentDown(): 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 or joystick.JustWentLeft(): vol -= .1 if event.key == pygame.K_RIGHT or joystick.JustWentRight(): 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 or joystick.JustPressedA(): self.options_dict["cache_screen_shots"] = not self.options_dict["cache_screen_shots"] if op == "Stretch to Fullscreen": if event.key == pygame.K_RETURN or joystick.JustPressedA(): self.options_dict["fullscreen"] = not self.options_dict["fullscreen"] if op == self.DEL: if event.key == pygame.K_RETURN or joystick.JustPressedA(): try: DID_SOMETHING = False for e, x in enumerate(os.listdir(fix_path(get_file("data/screenshots")))): os.remove(fix_path(get_file("data/screenshots/")) + x) DID_SOMETHING = True if DID_SOMETHING: 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 or joystick.JustPressedX(): 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 or joystick.JustPressedA() or joystick.JustPressedStart(): if self.options_mode: sel = self.option_selected if self.options[sel] == "Cancel": self.option_selected = 2 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) elif self.play_mode: if self.profiles[self.profile_selected] == "Back": self.play_mode = False else: profile = saves.load_profile(self.profile_selected) if profile["version"] != __version__: if profile["new"]: profile["version"] = __version__ else: print(colorize("Warning: this profile is from a different version \ of Interplanetary Invaders; \nerrors may occur", "warning")) 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) pygame.mixer.music.load(fix_path(get_file("audio/music/MainMenu.mp3"))) pygame.mixer.music.play(-1) if not self.options_mode and self.options_lock: self.done = True
import os import wave import pygame pygame.init() from interplanetary_invaders import __version__ as ver from interplanetary_invaders.scripts import saves from interplanetary_invaders.scripts.get_file import get_file, SOUND_PATH from interplanetary_invaders.scripts.utils import fix_path options = saves.load_options() is_slow = False SOUND_PATH = get_file(SOUND_PATH) if not os.path.exists(SOUND_PATH): os.mkdir(SOUND_PATH) versionFile = SOUND_PATH + "version" if os.path.exists(versionFile): with open(versionFile) as file: if file.read() != ver: for f in os.listdir(SOUND_PATH): if f != "version": os.remove(SOUND_PATH + f) with open(versionFile, "w") as file: file.write(ver)