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 main(): screen = initPygame() w = initWorld() highscoore = HighScore() level1 = Level1(w, 'images/level1.png', 0) cutscene1 = Cutscene('images/cutscene.png',0) level2 = Level2(w, 'images/level2.png', 1) sound = SoundManager() music = MusicManager() viewManager.levels.insert(0, level1) viewManager.cutscenes.insert(0, cutscene1) viewManager.levels.insert(1, level2) clock = pygame.time.Clock() viewManager.loadCutscene(0) playtime = 0 while True: screen.fill((100,100,100,0)) milliseconds = clock.tick(constants.FPS) playtime += milliseconds / 1000.0 move = handleInput(highscoore, sound) viewManager.currentView[0].draw(screen) for h in highscoore.entries: label = constants.myfont.render(str(h.score), 1, (255,0,0)) pos = h.screenPos screen.blit(label, (pos[0] - 70, pos[1] - 100)) viewManager.currentView[0].movePlayer(move) viewManager.currentView[0].update(w) render(w, screen) highscoore.update() worldAfterUpdate(w, screen, highscoore) text = "FPS: {0:.2f} Playtime: {1:.2f} ".format(clock.get_fps(), playtime) + "Score: " + str(highscoore.totalScore) pygame.display.set_caption(text) pygame.display.update() pygame.quit()
def endGame(self): self.stopTimer() userName = str(self.ui.btnJugador.text()) seconds = int(self.s) + int(self.m) * 60 + int(self.h) * 60 * 60 difficulty = ['Easy', 'Medium', 'Hard'][self.difficulty - 1] # Check if current score is a high scores highscores = HighScore.loadFromFile() highscores.reverse() if seconds < highscores[0].seconds: msj = QMessageBox() msj.setText( "Puntaje alto" ) msj.exec_() # Put score in highscore highscores.append( HighScore(userName, seconds, difficulty) ) highscores.sort() highscores.pop() HighScore.saveToFile(highscores)
def __init__(self): """Inicializador de la clase Estadistica.""" QtGui.QMainWindow.__init__(self) self.ui=Ui_Estadistica() self.ui.setupUi(self) """Coneccion de senales.""" self.ui.pushButton_7.clicked.connect(self.onBtnInicioClicked) self.lienzo=QtGui.QGraphicsScene() self.ui.graphicsView.setScene(self.lienzo) hs = HighScore.loadFromFile() if len(hs) >= 5: self.graficar(hs)
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()
class SpaceInvadersGame: 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 frame_step(self, simplify=False, inputs=None, name="Player"): init_bullet_count = len(self.bullets) gf.check_events(self.ai_settings, self.screen, self.sounds, self.stats, self.sb, self.scores, self.play_button, self.high_score_button, self.ship, self.aliens, self.ufo, self.bullets, self.bullet_delay, self.barriers, self.alien_bullets, self.smokes, inputs) bullet_count = len(self.bullets) alien_count = len(self.aliens) ufo_state = self.ufo.hit ship_pos = self.ship.rect.left if init_bullet_count < bullet_count: self.bullet_delay = self.ai_settings.bullet_delay elif self.bullet_delay > 0: self.bullet_delay -= 1 if self.stats.game_active: gf.update_timers(self.alien_timer, self.ufo_timer, self.ship_timer, self.smoke_timer) gf.update_ship(self.stats, self.sb, self.scores, self.ship, self.aliens, self.ufo, self.bullets, self.alien_bullets, self.ship_timer, self.alien_timer, simplify) if not self.ship.hit: gf.update_bullets(self.ai_settings, self.screen, self.sounds, self.stats, self.sb, self.ship, self.aliens, self.ufo, self.bullets, self.bullet_delay, self.barriers, self.alien_bullets, self.smokes, self.alien_timer, self.ufo_timer, self.smoke_timer, simplify) gf.update_aliens(self.ai_settings, self.screen, self.sounds, self.ship, self.aliens, self.barriers, self.alien_bullets, self.alien_timer, simplify) gf.update_ufo(self.ufo, self.ufo_timer, simplify) gf.update_smokes(self.smokes, self.smoke_timer) gf.update_screen(self.ai_settings, self.screen, self.stats, self.sb, self.ship, self.aliens, self.ufo, self.bullets, self.menu_bg, self.play_button, self.high_score_button, self.barriers, self.alien_bullets, self.smokes, simplify) pygame.display.update() reward = 0.0 if len(self.aliens) > 0: if len(self.aliens) < alien_count or self.ufo.hit != ufo_state: reward += 0.5 elif len(self.bullets) == 0: reward -= 0.2 if gf.ship_in_invader_range(self.ai_settings, self.ship, self.aliens, self.ufo): reward += 0.1 else: reward -= 0.1 reward += gf.bullet_aim(self.ai_settings, self.bullets, self.aliens, self.ufo) reward += gf.ship_in_bullet_path(self.ship, self.alien_bullets) if reward > 1.0: reward = 1.0 elif reward < -1.0: reward = -1.0 print reward if alien_count < len(self.aliens): game_state = False reward = 1 self.inactive = 240 self.bullet_delay = 0 else: game_state = self.stats.game_active if self.stats.game_active is False or self.inactive <= 0: self.stats.game_active = True reward = -1 self.inactive = 240 self.bullet_delay = 0 self.scores.check_place(int(round(self.stats.score, -1)), name) gf.restart(self.ai_settings, self.screen, self.sounds, self.stats, self.sb, self.ship, self.aliens, self.ufo, self.bullets, self.barriers, self.alien_bullets, self.smokes) image_data = None if inputs is not None: if len(inputs) == 3: img = self.screen image_data = pygame.surfarray.array3d(img) clock.tick(self.ai_settings.fps) # self.inactive += reward return reward, image_data, game_state
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)