def run(self): self.credits_running = True # display & update screen, get all the events while self.credits_running: # redraw the credits screen need_update = self._redraw() # count time passed in seconds time_passed_seconds = get_time_sec(self.clock.tick()) # update the screen objects' position self._update(time_passed_seconds) # display the screen surface pygame.display.update(need_update) # get all the events and operate properly for e in pygame.event.get(): # quit when the close button is pressed if e.type == pygame.QUIT: # force caller to terminate the game return False # handle keyboard keys key = pygame.key.get_pressed() if key[K_ESCAPE] or key[K_q]: self.credits_running = False if self.game_opts.sound: play_sound(self.return_sound, SOUND_VOL) # credits screen returns normally return True
def do_actions(self): while self.menu_main_running: bg = self.screen.blit(self.menu_main_bg, (0, 0)) time_passed_seconds = get_time_sec(self.clock.tick(MENU_CLOCK_TICK)) self.sprites.update(time_passed_seconds) self.sprites.draw(self.screen) self.menu_main.draw(self.screen) graphics.handle_mouse_cursor(self.mouse_cursor, self.screen) fr = self.screen.blit(self.window_frame, (0, 0)) need_update = (bg, fr) pygame.display.update(need_update) events = pygame.event.get() self.menu_main.update(events) for e in events: if e.type == pygame.QUIT: self._quit_option() elif e.type == pygame.KEYDOWN: if self.game_opts.sound: if e.key in (pygame.K_p, pygame.K_s, pygame.K_c): sound_mixer.play_sound( self.select_option_snd, SOUND_VOL) if e.key in (pygame.K_ESCAPE, pygame.K_q): self._quit_option() elif e.key == pygame.K_p: self._play_option() elif e.key == pygame.K_s: self._settings_option() elif e.key == pygame.K_c: self._credits_option()
def _settings_option(self): '''Entry point for main menu's settings option.''' # each time we enter in settings # sub menu, set the flag to true self.menu_settings_running = True for s in self.sprites: s.alpha = SPRITE_ALPHA while self.menu_settings_running: bg = self.screen.blit(self.menu_settings_bg, (0, 0)) self.menu_main.draw(self.screen) time_passed_seconds = get_time_sec(self.clock.tick(MENU_CLOCK_TICK)) self.sprites.update(time_passed_seconds) self.sprites.draw(self.screen) menu_bg = self.screen.blit(self.menu_box_bg, ( (constants.SCREEN_WIDTH - self.menu_box_bg.get_width()) / 2.0, (constants.SCREEN_HEIGHT - self.menu_box_bg.get_height()) / 2.0)) self.menu_settings.draw(self.screen) graphics.handle_mouse_cursor(self.mouse_cursor, self.screen) fr = self.screen.blit(self.window_frame, (0, 0)) need_update = (bg, menu_bg, fr) pygame.display.update(need_update) events = pygame.event.get() self.menu_settings.update(events) for e in events: if e.type == pygame.QUIT: self._back_option() self._quit_option() elif e.type == pygame.KEYDOWN: if self.game_opts.sound: if e.key in (pygame.K_f, pygame.K_s, pygame.K_m, pygame.K_b, pygame.K_ESCAPE): sound_mixer.play_sound( self.select_option_snd, SOUND_VOL) if e.key in (pygame.K_ESCAPE, pygame.K_b): self._back_option() elif e.key == pygame.K_f: self._toggle_fullscreen_option() elif e.key == pygame.K_s: self._toggle_sounds_option() elif e.key == pygame.K_m: self._toggle_music_option() for s in self.sprites: s.alpha = MAX_ALPHA
def update(self, events): for e in events: if e.type == pygame.KEYDOWN: # change menu option if e.key == pygame.K_DOWN: self.option += 1 elif e.key == pygame.K_UP: self.option -= 1 # play a sound when a menu option: # is focused if e.key in (pygame.K_UP, pygame.K_DOWN): if self.game_opts.sound: play_sound(self.focus_sound, FOCUS_SOUND_VOL) # is activated elif e.key in (pygame.K_RETURN, pygame.K_SPACE): if self.game_opts.sound: play_sound(self.select_sound, FOCUS_SOUND_VOL) self.options[self.option]['callable']() # mouse controls (if enabled) if self.mouse_enabled: if e.type == pygame.MOUSEMOTION: # keep in mind the previous option self.option_previous = self.option # check the mouse's positions to # know if move focus on a option self._check_mouse_pos_for_focus() # play a sound only when we change between options if self.game_opts.sound: if self.option != self.option_previous: play_sound(self.focus_sound, FOCUS_SOUND_VOL) # if there are any mouse buttons pressed down elif e.type == pygame.MOUSEBUTTONDOWN: # check the mouse's positions to # know if move focus on a option self._check_mouse_pos_for_focus() # get mouse button events lb, _, _ = pygame.mouse.get_pressed() # play when left button is pressed on focused option if lb and self.mouse_focus: if self.game_opts.sound: play_sound(self.select_sound, FOCUS_SOUND_VOL) self.options[self.option]['callable']() # menu options limits (cyclic logic) option_len = len(self.options) - 1 if self.option > option_len: self.option = 0 elif self.option < 0: self.option = option_len
def run(self): # flag to control the credits loop self.credits_running = True # display & update screen, get all the events while self.credits_running: # redraw the credits screen self._redraw() # count time passed in seconds time_passed = self.clock.tick() time_passed_seconds = time_passed / 1000.0 # update the screen objects' position self._update(time_passed_seconds) # display the screen surface pygame.display.update() # get all the events and operate properly for e in pygame.event.get(): # quit when the close button is pressed if e.type == pygame.QUIT: # force caller to terminate the game return False # handle keyboard keys key = pygame.key.get_pressed() # when user presses escape key or 'q'uit key if key[K_ESCAPE] or key[K_q]: self.credits_running = False # play the return sound if self.game_opts.sound: play_sound(self.return_sound, 0.2) # credits screen returns normally return True
def _settings_option(self): # each time we enter in settings # sub menu, set the flag to true self.menu_settings_running = True # decrease the alpha of animated sprites for s in self.anim_sprites: s.image.set_alpha(ANIM_SPRITE_ALPHA) # display & update screen, get all the events while self.menu_settings_running: # draw the background self.screen.blit(self.menu_settings_bg, (0, 0)) # draw the main menu self.menu_main.draw(self.screen) # count time passed in seconds time_passed_seconds = get_time_sec(self.clock.tick(50)) # animate the sprites self.anim_sprites.update(time_passed_seconds) rectlist = self.anim_sprites.draw(self.screen) # draw settings menu background box self.screen.blit(self.menu_box_bg, ( (constants.SCREEN_WIDTH - self.menu_box_bg.get_width()) / 2.0, (constants.SCREEN_HEIGHT - self.menu_box_bg.get_height()) / 2.0)) # draw the settings menu self.menu_settings.draw(self.screen) # draw the custom mouse cursor graphics.handle_mouse_cursor(self.mouse_cursor, self.screen) # draw the frame of the window self.screen.blit(self.window_frame, (0, 0)) # display the screen surface pygame.display.update() # clear the sprites self.anim_sprites.clear(self.screen, self.menu_main_bg) # get all the events events = pygame.event.get() # ..... and update the settings menu # which needs access to those events self.menu_settings.update(events) # settings menu event loop for e in events: # quit when the close button is pressed if e.type == pygame.QUIT: self._back_option() self._quit_option() # handle keyboard keys elif e.type == pygame.KEYDOWN: # play the sound if there was a menu key shortcut if self.game_opts.sound: if e.key in (pygame.K_f, pygame.K_s, pygame.K_m, pygame.K_b, pygame.K_ESCAPE): sound_mixer.play_sound( self.select_option_snd, 0.2) # when user presses escape key or 'b'ack key if e.key in (pygame.K_ESCAPE, pygame.K_b): self._back_option() # when user presses 'f'ullscreen key elif e.key == pygame.K_f: self._toggle_fullscreen_option() # when user presses 's'ounds key elif e.key == pygame.K_s: self._toggle_sounds_option() # when user presses 'm'usic key elif e.key == pygame.K_m: self._toggle_music_option() # restore the alpha of the animated sprites for s in self.anim_sprites: s.image.set_alpha(255)
def do_actions(self): # display & update screen, get all the events while self.menu_main_running: # draw the background self.screen.blit(self.menu_main_bg, (0, 0)) # count time passed in seconds time_passed_seconds = get_time_sec(self.clock.tick(50)) # animate the sprites self.anim_sprites.update(time_passed_seconds) rectlist = self.anim_sprites.draw(self.screen) # draw the main menu self.menu_main.draw(self.screen) # draw the custom mouse cursor graphics.handle_mouse_cursor(self.mouse_cursor, self.screen) # draw the frame of the window self.screen.blit(self.window_frame, (0, 0)) # display the screen surface pygame.display.update() # clear the sprites self.anim_sprites.clear(self.screen, self.menu_main_bg) # get all the events events = pygame.event.get() # ......... and update the main menu # which needs access to those events self.menu_main.update(events) # main menu event loop for e in events: # quit when the close button is pressed if e.type == pygame.QUIT: self._quit_option() # handle keyboard keys elif e.type == pygame.KEYDOWN: # play the sound if there was a menu key shortcut if self.game_opts.sound: if e.key in (pygame.K_p, pygame.K_s, pygame.K_c): sound_mixer.play_sound( self.select_option_snd, 0.2) # when user presses escape or 'q'uit key if e.key in (pygame.K_ESCAPE, pygame.K_q): self._quit_option() # when user presses 'p'lay key elif e.key == pygame.K_p: self._play_option() # when user presses 's'ettings key elif e.key == pygame.K_s: self._settings_option() # when user presses 'c'redits key elif e.key == pygame.K_c: self._credits_option()
def _settings_option(self): # each time we enter in settings # sub menu, set the flag to true self.menu_settings_running = True # decrease the alpha of animated sprites for s in self.anim_sprites: s.image.set_alpha(ANIM_SPRITE_ALPHA) # display & update screen, get all the events while self.menu_settings_running: # draw the background bg = self.screen.blit(self.menu_settings_bg, (0, 0)) # draw the main menu self.menu_main.draw(self.screen) # count time passed in seconds time_passed_seconds = get_time_sec( self.clock.tick(MENU_CLOCK_TICK)) # animate the sprites self.anim_sprites.update(time_passed_seconds) self.anim_sprites.draw(self.screen) # draw settings menu background box menu_bg = self.screen.blit( self.menu_box_bg, ((constants.SCREEN_WIDTH - self.menu_box_bg.get_width()) / 2.0, (constants.SCREEN_HEIGHT - self.menu_box_bg.get_height()) / 2.0)) # draw the settings menu self.menu_settings.draw(self.screen) # draw the custom mouse cursor graphics.handle_mouse_cursor(self.mouse_cursor, self.screen) # draw the frame of the window fr = self.screen.blit(self.window_frame, (0, 0)) need_update = (bg, menu_bg, fr) # display the screen surface pygame.display.update(need_update) # get all the events events = pygame.event.get() # ..... and update the settings menu # which needs access to those events self.menu_settings.update(events) # settings menu event loop for e in events: # quit when the close button is pressed if e.type == pygame.QUIT: self._back_option() self._quit_option() # handle keyboard keys elif e.type == pygame.KEYDOWN: # play the sound if there was a menu key shortcut if self.game_opts.sound: if e.key in (pygame.K_f, pygame.K_s, pygame.K_m, pygame.K_b, pygame.K_ESCAPE): sound_mixer.play_sound(self.select_option_snd, SOUND_VOL) # when user presses escape key or 'b'ack key if e.key in (pygame.K_ESCAPE, pygame.K_b): self._back_option() # when user presses 'f'ullscreen key elif e.key == pygame.K_f: self._toggle_fullscreen_option() # when user presses 's'ounds key elif e.key == pygame.K_s: self._toggle_sounds_option() # when user presses 'm'usic key elif e.key == pygame.K_m: self._toggle_music_option() # restore the alpha of the animated sprites for s in self.anim_sprites: s.image.set_alpha(MAX_ALPHA)
def do_actions(self): # display & update screen, get all the events while self.menu_main_running: # draw the background bg = self.screen.blit(self.menu_main_bg, (0, 0)) # count time passed in seconds time_passed_seconds = get_time_sec( self.clock.tick(MENU_CLOCK_TICK)) # animate the sprites self.anim_sprites.update(time_passed_seconds) # rectlist = self.anim_sprites.draw(self.screen) self.anim_sprites.draw(self.screen) # draw the main menu self.menu_main.draw(self.screen) # draw the custom mouse cursor graphics.handle_mouse_cursor(self.mouse_cursor, self.screen) # draw the frame of the window fr = self.screen.blit(self.window_frame, (0, 0)) need_update = (bg, fr) # display the screen surface pygame.display.update(need_update) # get all the events events = pygame.event.get() # ......... and update the main menu # which needs access to those events self.menu_main.update(events) # main menu event loop for e in events: # quit when the close button is pressed if e.type == pygame.QUIT: self._quit_option() # handle keyboard keys elif e.type == pygame.KEYDOWN: # play the sound if there was a menu key shortcut if self.game_opts.sound: if e.key in (pygame.K_p, pygame.K_s, pygame.K_c): sound_mixer.play_sound(self.select_option_snd, SOUND_VOL) # when user presses escape or 'q'uit key if e.key in (pygame.K_ESCAPE, pygame.K_q): self._quit_option() # when user presses 'p'lay key elif e.key == pygame.K_p: self._play_option() # when user presses 's'ettings key elif e.key == pygame.K_s: self._settings_option() # when user presses 'c'redits key elif e.key == pygame.K_c: self._credits_option()
def update(self, events): for e in events: # keyboard controls if e.type == pygame.KEYDOWN: # change menu option if e.key == pygame.K_DOWN: self.option += 1 elif e.key == pygame.K_UP: self.option -= 1 # play a sound when a menu option: # is focused if e.key in (pygame.K_UP, pygame.K_DOWN): if self.game_opts.sound: play_sound(self.focus_sound, FOCUS_SOUND_VOL) # is activated elif e.key in (pygame.K_RETURN, pygame.K_SPACE): if self.game_opts.sound: play_sound(self.select_sound, FOCUS_SOUND_VOL) self.options[self.option]['callable']() # mouse controls (only if it's enabled) if self.mouse_enabled: # if there is some mouse motion if e.type == pygame.MOUSEMOTION: # keep in mind the previous option self.option_previous = self.option # check the mouse's positions to # know if move focus on a option self._check_mouse_pos_for_focus() # play a sound only when we change between options if self.game_opts.sound: if self.option != self.option_previous: play_sound(self.focus_sound, FOCUS_SOUND_VOL) # if there are any mouse buttons pressed down elif e.type == pygame.MOUSEBUTTONDOWN: # check the mouse's positions to # know if move focus on a option self._check_mouse_pos_for_focus() # get mouse button events lb, _, _ = pygame.mouse.get_pressed() # play when left button is pressed on focused option if lb and self.mouse_focus: if self.game_opts.sound: play_sound(self.select_sound, FOCUS_SOUND_VOL) self.options[self.option]['callable']() # menu options limits (cyclic sense) option_len = len(self.options) - 1 if self.option > option_len: self.option = 0 elif self.option < 0: self.option = option_len