def __init__(self): self.state = 'pre' # init pygame pygame.init() pygame.mixer.init(44100, 16, 2, 1024*4) pygame.display.set_caption("MR. TRAPEZIUS ") try: self.screen = pygame.display.set_mode((640, 480), HWSURFACE | SRCALPHA, 32) except: self.screen = pygame.display.set_mode((640, 480), SRCALPHA, 32) try: pygame.display.set_icon(pygame.image.load( util.file_path("Inky.png")).convert_alpha()) except: # some platfom do not allow change icon after shown pass # init fonts and music lists util.init() # init sub states objects self.pre = Pre(self.screen) self.go = MainGame(self.screen) self.level_info = Level(self.screen) self.re = Retry(self.screen) self.next = NextLevel(self.screen) sound.load()
def setUp(self): unittest.TestCase.setUp(self) self.test_main_game = MainGame()
def run(self): self.load_music() self.running = True self.speed = 2 self.fps = 30 self.clock = pygame.time.Clock() self.logo = Logo(self.logo_group, (self.width // 2 - 130, self.height // 4 - 80)) self.draw_logo = True bg = BackgroundStars(self, 500, (1, 5)) while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.USEREVENT: if event.user_type == pygame_gui.UI_BUTTON_PRESSED: if event.ui_element == self.btn_start: # ship choice if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() self.hide_all_btns() self.draw_logo = False self.btn_next.show() self.btn_previous.show() self.feature_text.show() self.dropdownmenu_difficulty.show() self.feature_describe_text.show() self.label_difficulty.show() self.start_trigger.show() self.feature_recomendation_text.show() self.label_ship_name.show() self.go_to_menu.show() self.ship = self.ships[0](self.ships_group, self.ship_pos) elif event.ui_element == self.btn_settings: # open settings if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() self.hide_all_btns() self.dropdownmenu_screen_size.show() self.btn_return.show() self.dropdownmenu_sound.show() self.label_change_screen_size.show() self.label_change_music_on.show() elif event.ui_element == self.start_trigger: # start game game = MainGame( screen_size=self.screen_size, music_on=self.music_on, difficulty=self.difficulty, ship=self.ship_names[self.now_ship]) self.menu_init() self.load_music() elif event.ui_element == self.btn_quit: # quit game if self.music_on: pygame.mixer.Sound( 'sounds/ButtonQuit.mp3').play() pygame.quit() sys.exit(-1) elif event.ui_element == self.btn_return: # close settings if self.music_on: pygame.mixer.Sound( 'sounds/ButtonQuit.mp3').play() self.hide_all_btns() self.btn_start.show() self.btn_settings.show() self.btn_quit.show() self.label_change_screen_size.hide() self.label_change_music_on.hide() self.save_settings() elif event.ui_element in self.dropdownmenu_screen_size.get_focus_set( ): # change screen size data if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() if event.ui_element.text != '▼': self.screen_size = list( map(int, event.ui_element.text.split('*'))) elif event.ui_element in self.dropdownmenu_difficulty.get_focus_set( ): # change difficulty if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() if event.ui_element.text != '▼': self.difficulty = event.ui_element.text elif event.ui_element in self.dropdownmenu_sound.get_focus_set( ): # change music data if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() if event.ui_element.text == 'On': self.music_on = True pygame.mixer.music.play() else: self.music_on = False pygame.mixer.music.pause() if event.ui_element == self.btn_next: # next ship if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() if self.now_ship + 1 < len(self.ships): self.now_ship += 1 self.ship.kill() self.ship = self.ships[self.now_ship]( self.ships_group, self.ship_pos) self.feature_describe_text.set_text( self.ship_describe[self.now_ship]) self.feature_recomendation_text.set_text( self.ship_recommended[self.now_ship]) elif event.ui_element == self.btn_previous: # previous ship if self.music_on: pygame.mixer.Sound( 'sounds/ButtonMenu.mp3').play() if self.now_ship - 1 >= 0: self.now_ship -= 1 self.ship.kill() self.ship = self.ships[self.now_ship]( self.ships_group, self.ship_pos) self.feature_describe_text.set_text( self.ship_describe[self.now_ship]) self.feature_recomendation_text.set_text( self.ship_recommended[self.now_ship]) elif event.ui_element == self.go_to_menu: if self.music_on: pygame.mixer.Sound( 'sounds/ButtonQuit.mp3').play() self.menu_init() text_pos = (self.width // 2 - self.label_ship_name.rect[2] // 2, self.height // 5) self.label_ship_name.rect = pygame.Rect( text_pos, (self.label_ship_name.rect[2], self.label_ship_name.rect[3])) self.label_ship_name.set_text( self.ships[self.now_ship].get_name(False)) self.manager.process_events(event) self.manager.update(time_delta=self.clock.tick(self.fps) / 1000.0) bg.draw() self.manager.draw_ui(self.screen) self.ships_group.draw(self.screen) if self.draw_logo: self.logo_group.draw(self.screen) pygame.display.flip()
class Game(object): """main loop, switching from different states""" def __init__(self): self.state = 'pre' # init pygame pygame.init() pygame.mixer.init(44100, 16, 2, 1024*4) pygame.display.set_caption("MR. TRAPEZIUS ") try: self.screen = pygame.display.set_mode((640, 480), HWSURFACE | SRCALPHA, 32) except: self.screen = pygame.display.set_mode((640, 480), SRCALPHA, 32) try: pygame.display.set_icon(pygame.image.load( util.file_path("Inky.png")).convert_alpha()) except: # some platfom do not allow change icon after shown pass # init fonts and music lists util.init() # init sub states objects self.pre = Pre(self.screen) self.go = MainGame(self.screen) self.level_info = Level(self.screen) self.re = Retry(self.screen) self.next = NextLevel(self.screen) sound.load() def loop(self): while self.state != 'quit': print self.state if self.state == 'pre': self.state = self.pre.run() elif self.state == 'pretending': self.state = self.pre.show_pretend_loading() elif self.state.startswith('level'): info_mode = int(self.state[-1]) print self.state, info_mode self.state = self.level_info.run(info_mode) elif self.state.startswith('game'): mode = int(self.state[-1]) self.state = self.go.run(mode) elif self.state.startswith('next'): nextlevel = int(self.state[-1]) self.state = self.next.run(nextlevel) elif self.state.startswith('retry'): retrylevel = int(self.state[-1]) self.state = self.re.run(retrylevel) # pygame.display.update() pygame.quit() exit()