def main(self): '''(App) -> int Main routine of the game. Returns the return of self.init() if it does not return 0, otherwise returns 0.''' #Starts the game window init_status = self.init() #Failed to initialize properly if init_status != 0: return init_status while self.running: #Checks for events for event in pygame.event.get(): #Exit event if event.type == QUIT: self.exit() #Mouse click event elif event.type == MOUSEBUTTONUP: mouse_pos = pygame.mouse.get_pos() #Elements for ui include button for ui_elem in self.ui_elements: if ui_elem.is_clicked(mouse_pos): ui_elem.execute(self, mouse_pos) if not self.play: for moon in self.moons: if moon.is_clicked(mouse_pos): self.selected_moon = moon elif event.type == KEYDOWN: if self.play or (self.selected_moon == None): continue if event.key == K_LEFT: self.selected_moon.parameter_mod -= 0.1 self.updated_moon_pos = True elif event.key == K_RIGHT: self.selected_moon.parameter_mod += 0.1 self.updated_moon_pos = True if self.play: for moon in self.moons: self.to_update.append(moon.unrender(self)) if self.reset: self.old_time = perf_counter() self.reset = False self.to_update.append( moon.update_parameter(self.window, perf_counter() - self.old_time)) # self.window, moon.parameter + 0.1)) keep = [] i = 0 for i in range(len(self.asteroids_ingame)): asteroid = self.asteroids_ingame[i] self.to_update.append(asteroid.unrender(self)) self.to_update.append( asteroid.update_parameter( self.window, perf_counter() - self.old_time)) # asteroid.parameter + 0.1)) #Checks for collision with each of the moons # TODO optimize ########################################## for moon in self.moons: if asteroid.collides_with(moon.area): FILE = join("data", "audio", "effects", "moon_planet_collision.wav") pygame.mixer.music.load(FILE) pygame.mixer.music.play() self.to_update.append(asteroid.unrender(self)) break else: keep.append(i) # check for planet collision if asteroid.collides_with(self.planet.rect): FILE = join("data", "audio", "effects", "moon_planet_collision.wav") pygame.mixer.music.load(FILE) pygame.mixer.music.play() reset(self) break i += 1 if self.play: # could be false due to planet-asteroid collision self.asteroids_ingame = [ (lambda i: self.asteroids_ingame[i])(i) for i in keep ] for orbit in self.orbits: self.to_update.append(pygame.draw.circle(*orbit)) if self.asteroids_ingame == []: # win! FILE = join("data", "audio", "effects", "win.wav") pygame.mixer.music.load(FILE) pygame.mixer.music.play() render_level_menu_screen(self) if self.updated_moon_pos: # update moons in case user moved them around for moon in self.moons: self.to_update.append(moon.unrender(self)) self.to_update.append(moon.update_parameter( self.window, 0)) for orbit in self.orbits: self.to_update.append(pygame.draw.circle(*orbit)) self.updated_moon_pos = False for rect in self.to_update: if rect == None: pygame.display.update() break pygame.display.update(rect) self.to_update.clear() self.fps_clock.tick(self.fps) return 0
def main(self): """(App) -> int Main routine of the game. Returns the return of self.init() if it does not return 0, otherwise returns 0.""" # Starts the game window init_status = self.init() # Failed to initialize properly if init_status != 0: return init_status while self.running: # Checks for events for event in pygame.event.get(): # Exit event if event.type == QUIT: self.exit() # Mouse click event elif event.type == MOUSEBUTTONUP: mouse_pos = pygame.mouse.get_pos() # Elements for ui include button for ui_elem in self.ui_elements: if ui_elem.is_clicked(mouse_pos): ui_elem.execute(self, mouse_pos) if not self.play: for moon in self.moons: if moon.is_clicked(mouse_pos): self.selected_moon = moon elif event.type == KEYDOWN: if self.play or (self.selected_moon == None): continue if event.key == K_LEFT: self.selected_moon.parameter_mod -= 0.1 self.updated_moon_pos = True elif event.key == K_RIGHT: self.selected_moon.parameter_mod += 0.1 self.updated_moon_pos = True if self.play: for moon in self.moons: self.to_update.append(moon.unrender(self)) if self.reset: self.old_time = perf_counter() self.reset = False self.to_update.append(moon.update_parameter(self.window, perf_counter() - self.old_time)) # self.window, moon.parameter + 0.1)) keep = [] i = 0 for i in range(len(self.asteroids_ingame)): asteroid = self.asteroids_ingame[i] self.to_update.append(asteroid.unrender(self)) self.to_update.append(asteroid.update_parameter(self.window, perf_counter() - self.old_time)) # asteroid.parameter + 0.1)) # Checks for collision with each of the moons # TODO optimize ########################################## for moon in self.moons: if asteroid.collides_with(moon.area): FILE = join("data", "audio", "effects", "moon_planet_collision.wav") pygame.mixer.music.load(FILE) pygame.mixer.music.play() self.to_update.append(asteroid.unrender(self)) break else: keep.append(i) # check for planet collision if asteroid.collides_with(self.planet.rect): FILE = join("data", "audio", "effects", "moon_planet_collision.wav") pygame.mixer.music.load(FILE) pygame.mixer.music.play() reset(self) break i += 1 if self.play: # could be false due to planet-asteroid collision self.asteroids_ingame = [(lambda i: self.asteroids_ingame[i])(i) for i in keep] for orbit in self.orbits: self.to_update.append(pygame.draw.circle(*orbit)) if self.asteroids_ingame == []: # win! FILE = join("data", "audio", "effects", "win.wav") pygame.mixer.music.load(FILE) pygame.mixer.music.play() render_level_menu_screen(self) if self.updated_moon_pos: # update moons in case user moved them around for moon in self.moons: self.to_update.append(moon.unrender(self)) self.to_update.append(moon.update_parameter(self.window, 0)) for orbit in self.orbits: self.to_update.append(pygame.draw.circle(*orbit)) self.updated_moon_pos = False for rect in self.to_update: if rect == None: pygame.display.update() break pygame.display.update(rect) self.to_update.clear() self.fps_clock.tick(self.fps) return 0
def render_level_menu_screen(app, MOUSE_POS=None): '''(App, tuple) -> NoneType''' BLACK = (0, 0, 0) WHITE = (255, 255, 255) if app.play: reset(app) app.moons.clear() # remove any moons from old level app.asteroids.clear() # remove any asteroids from old level app.orbits.clear() # remove any orbits from old level app.selected_moon = None # remove selected moon from old level app.num_orbits = 0 app.ui_elements.clear() # remove old ui elements in old screen, if any FONT_FAMILY = join("data", "font", "Alien-Encounters-Regular.ttf") FONT_SIZE = 30 FONT_COLOUR = WHITE app.background = BLACK app.window.fill(BLACK) # title screen button COORD = (100, 50) WIDTH = 800 HEIGHT = 40 TEXT = "title screen" FOO = render_title_menu_screen title_screen_button = Button(COORD, WIDTH, HEIGHT, TEXT, FONT_FAMILY, FONT_SIZE, COLOUR=FONT_COLOUR, FOO=FOO) app.ui_elements.append(title_screen_button) title_screen_button.render(app.window) # level 1 button COORD = (100, 200) WIDTH = 800 HEIGHT = 40 TEXT = "level 1" FOO = render_level1 level1_button = Button(COORD, WIDTH, HEIGHT, TEXT, FONT_FAMILY, FONT_SIZE, COLOUR=FONT_COLOUR, FOO=FOO) app.ui_elements.append(level1_button) level1_button.render(app.window) # level 2 button COORD = (100, 250) WIDTH = 800 HEIGHT = 40 TEXT = "level 2" FOO = render_level2 level2_button = Button(COORD, WIDTH, HEIGHT, TEXT, FONT_FAMILY, FONT_SIZE, COLOUR=FONT_COLOUR, FOO=FOO) app.ui_elements.append(level2_button) level2_button.render(app.window) # level 3 button COORD = (100, 300) WIDTH = 800 HEIGHT = 40 TEXT = "level 3" FOO = render_level3 level3_button = Button(COORD, WIDTH, HEIGHT, TEXT, FONT_FAMILY, FONT_SIZE, COLOUR=FONT_COLOUR, FOO=FOO) app.ui_elements.append(level3_button) level3_button.render(app.window) app.to_update.append(None) # update whole software display app.status = 1