def __init__(self): self.scores = HighScore() self.ai_settings = Settings() self.screen = pygame.display.set_mode((self.ai_settings.screen_width, self.ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") self.sounds = Sound() self.play_button = Button(self.screen, pygame.image.load('images/play_btn.png'), 850, 400) self.high_score_button = Button(self.screen, pygame.image.load('images/high_score_btn.png'), 850, 600) self.menu_bg = Button(self.screen, pygame.image.load('images/menu.png'), self.ai_settings.screen_width / 2, self.ai_settings.screen_height / 2) self.stats = GameStats(self.ai_settings, self.scores) self.sb = Scoreboard(self.ai_settings, self.screen, self.sounds, self.stats) self.ship = Ship(self.ai_settings, self.screen, self.sounds) self.bullets = Group() self.alien_bullets = Group() self.aliens = Group() self.ufo = UFO(self.ai_settings, self.screen, self.sounds) self.barriers = Group() self.smokes = Group() self.inactive = 240 gf.create_fleet(self.ai_settings, self.screen, self.sounds, self.aliens) gf.create_barriers(self.ai_settings, self.screen, self.barriers) # timers used for animation and event checking self.alien_timer = Timer(self.ai_settings.alien_frame_factor) self.smoke_timer = Timer(self.ai_settings.smoke_timer) self.ship_timer = Timer(self.ai_settings.ship_timer) self.ufo_timer = Timer(self.ai_settings.alien_frame_factor * 5) self.bullet_delay = 0 self.stats.game_active = True
def __init__(self): self.current_mouse_position = Vector(0, 0) self.playing = True self.frame_rate = 0.01 self.win = GraphWin("Bolinha Game", 800, 600) self.win.bind_all("<KeyRelease>", key_released) self.win.bind_all("<KeyPress>", key_pressed) self.win.bind('<Motion>', motion) self.highscore = HighScore() self.menu_scene = Menu(self.win, self.begin_gameplay, self.credits_switch, self.exit_game, self.credits_switch) self.gameplay_scene = Gameplay(self.win, self.pause, self.back_to_menu, self.exit_game, self.restart, self.highscore) self.menu_scene.draw() self.current_scene = self.menu_scene self.last_key = "" self.current_frame_delta = self.frame_rate
# File holding custom controls CUSTOM_CONTROL_FILENAME = 'compassgame_controls.dat' #Dictionary with messages to show to user for action to carry out action_text = {'north':'Go north', 'south':'Go south', 'east':'Go east', 'west':'Go west', 'duck':'Quick duck!', 'jump':'Check the map'} # Track Status etc game_status = GamePlay(action_text) # Handles key interaction game_controls = GameControls(CUSTOM_CONTROL_FILENAME) # Track high score high_score = HighScore(game_controls, HIGH_SCORE_FILENAME) # These are used for the menu sub commands - must be classes # Must implement show() display() mouse_move() and mouse_click() select() sub_commands = { 'character' : CustomCharacter(game_controls, PLAYER_TEXT_IMG_FORMAT), 'controls' : CustomControls(game_controls, WIDTH, HEIGHT), 'highscore' : high_score } # allows different character looks - must come after the sub_commands are defined (theme, theme_num) = sub_commands['character'].getTheme()
sys.exit() # Collect key events to pass to scene updates if e.type == KEYDOWN or e.type == KEYUP: keys.append(e) # Update scene sceneIndex = scene.update(keys) if sceneIndex == 0: if type(scene) is not GameStart: scene = GameStart(SCR_WIDTH, SCR_HEIGHT) elif sceneIndex == 1: if type(scene) is not GamePlay: scene = GamePlay(SCR_WIDTH, SCR_HEIGHT) elif sceneIndex == 2: if type(scene) is not GameEnd: scene = GameEnd(SCR_WIDTH, SCR_HEIGHT) elif sceneIndex == 3: if type(scene) is not HighScore: scene = HighScore(SCR_WIDTH, SCR_HEIGHT) elif sceneIndex == 4: run = False # Render scene background.fill((0, 0, 0)) scene.render(background) screen.blit(background, (0, 0)) pygame.display.flip() fps_clock.tick(FPS) pygame.quit() sys.exit()
def run_game(): pygame.init() clock = pygame.time.Clock() highscores = HighScore() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") sounds = Sound() play_button = Button(screen, pygame.image.load('images/play_btn.png'), 850, 400) high_score_button = Button(screen, pygame.image.load('images/high_score_btn.png'), 850, 600) menu_bg = Button(screen, pygame.image.load('images/menu.png'), ai_settings.screen_width / 2, ai_settings.screen_height / 2) stats = GameStats(ai_settings, highscores) sb = Scoreboard(ai_settings, screen, sounds, stats) ship = Ship(ai_settings, screen, sounds) bullets = Group() alien_bullets = Group() aliens = Group() ufo = UFO(ai_settings, screen, sounds) barriers = Group() smokes = Group() gf.create_fleet(ai_settings, screen, sounds, aliens) gf.create_barriers(ai_settings, screen, barriers) # timers used for animation and event checking alien_timer = Timer(ai_settings.alien_frame_factor) smoke_timer = Timer(8) ship_timer = Timer(4) ufo_timer = Timer(ai_settings.alien_frame_factor * 5) while True: clock.tick(60) gf.check_events(ai_settings, screen, sounds, stats, sb, highscores, play_button, high_score_button, ship, aliens, bullets, barriers, alien_bullets, smokes) if stats.game_active: gf.update_timers(alien_timer, ufo_timer, ship_timer, smoke_timer) gf.update_ship(stats, sb, highscores, ship, aliens, ufo, bullets, alien_bullets, ship_timer, alien_timer) if not ship.hit: gf.update_bullets(ai_settings, screen, sounds, stats, sb, ship, aliens, ufo, bullets, barriers, alien_bullets, smokes, alien_timer, ufo_timer, smoke_timer) gf.update_aliens(ai_settings, screen, sounds, ship, aliens, barriers, alien_bullets, alien_timer) gf.update_ufo(ufo, ufo_timer) gf.update_smokes(smokes, smoke_timer) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, ufo, bullets, menu_bg, play_button, high_score_button, barriers, alien_bullets, smokes)