def take_damage(self, explosion): self.all_sprites.remove(explosion) if self.lives > 0: self.goto_checkpoint() self.lives -= 1 else: gameover = GameOver(self.screen.get_width() * 0.2, self.screen.get_height() * 0.2, int(self.screen.get_width() * 0.6), int(self.screen.get_height() * 0.6), self.highest_score, self.reset) self.all_sprites.add(gameover)
def update_screen(self): for s in self.sprites: s.update_status() if not s.ai: #update player s.update() if not s.status==8 and not self.player.off_screen(): s.check_mapcollisions(self.blocks) trig = s.check_trigcollisions(self.map.triggers) if trig: if trig.name=="exit": self.player.exiting = 1 elif trig.name=="dtrigger": self.show_dialog(trig.params) if trig.params=="tut08": self.player.exiting = -1 self.player.set_image(GLOBALS["nanji"]["idle"][0]) else: player_data["health"] = 9 player_data["lives"] -= 1 if player_data["lives"] > 0: next = Level(self.map_filename, prev=self.prev) else: next = GameOver() self.next = FadeState(self.screen, next.background, next) self.quit() else: #update ai if s.ai_update(self): #throw shuriken? self.bullets.add(Shuriken(s, self.camera)) self.bullets.update() self.camera.update() #draw bg #self.scroll_bg.update(self.player.vel[0]) self.background.blit(self.bg, (0,0)) #draw map and sprites self.lvl_layer.fill((255,0,255)) self.lvl_layer.blit(self.lvl_img, self.camera.pos, [self.camera.pos, GLOBALS["screen_size"]]) self.lvl_layer.set_colorkey((255,0,255)) self.bullets.draw(self.lvl_layer) self.sprites.draw(self.lvl_layer) #pygame.draw.rect(self.lvl_layer, (255,255,0), self.player.colrect, 1) #draw collision rect of nanji if self.player.exiting: #draw semi-black image over dojo entry while entering self.lvl_layer.blit(GLOBALS["objects"]["dojo_entry"], self.map.dojo_pos) self.background.blit(self.lvl_layer, (0,0), [self.camera.pos, GLOBALS["screen_size"]]) self.ui_layer.draw(self.background) self.screen.blit(self.background, (0,0)) pygame.display.flip()
def main(): pygame.init() screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) pygame.display.set_caption("Snowy Slopes") pygame.display.set_icon(load.load_image("sledderFullsize")) init() timer = pygame.time.Clock() while True: timer.tick(60) selection = Menu(screen).run() if selection == Menu.NEWGAME: selection = Menu(screen, gameSelection=True).run() if selection == Menu.GAME: score = Game(screen, timer).run() elif selection == Menu.DRAW: numPlatforms = Draw(screen).run() if isinstance(numPlatforms, list): platformType = numPlatforms score = Game(screen, timer, custom=platformType).run() elif isinstance(numPlatforms, int): score = Game(screen, timer, custom=numPlatforms).run() else: main() elif selection == Menu.BACK: main() elif selection == Menu.HELP: Help(screen, timer).run() score = Game(screen, timer).run() elif selection == Menu.QUIT: pygame.quit() sys.exit() # runs after game is over newSelection = GameOver(screen, score).run() if newSelection == GameOver.QUIT: pygame.quit() sys.exit() elif newSelection == GameOver.REPLAY: main() # resets the game
def __init__(self, ai_settings, screen): self.ai_settings = ai_settings self.screen = screen self.display_start = True self.display_high_score = False self.start_screen = StartScreen(ai_settings=ai_settings, screen=screen) self.stats = GameStats(ai_settings=self.ai_settings) self.sb = Scoreboard(ai_settings=self.ai_settings, screen=self.screen, stats=self.stats) self.ship = Ship(ai_settings=self.ai_settings, screen=self.screen) self.high_score = High_Score(self.ai_settings, self.screen) self.gameover = GameOver(self.ai_settings, self.screen) self.quit = False self.alien1 = Alien1(ai_settings=self.ai_settings, screen=self.screen) self.alien2 = Alien2(ai_settings=self.ai_settings, screen=self.screen) self.alien3 = Alien3(ai_settings=self.ai_settings, screen=self.screen) self.aliens = [self.alien1, self.alien2, self.alien3] self.UFO = UFO(ai_settings=self.ai_settings, screen=self.screen) self.faster = False self.bullets = Group() self.enemy_bullets = Group() self.alien_group = Group() self.play_music = 'sounds/play.mp3' self.play_music_faster = 'sounds/play-faster.mp3' self.missile_sound = pygame.mixer.Sound('sounds/missile.wav') self.bunker = Group() self.create_bunker()
while not run and not man.living and not man.win and not difficultyScreen: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if singlePlayer.collidePt(event.pos): man.living = True run = True elif difficulty.collidePt(event.pos): difficultyScreen = True elif exit.collidePt(event.pos): exit.clicked = True sys.exit() screen.fill([0, 0, 0]) banner = GameOver("rsc/Menus/titlebanner.png", [25, 25], screenSize) screen.blit(banner.surface, banner.rect) screen.blit(singlePlayer.surface, singlePlayer.rect) screen.blit(difficulty.surface, difficulty.rect) screen.blit(exit.surface, exit.rect) pygame.display.flip() #---------------------Difficulty------------------------------------ while not run and not man.living and not man.win and difficultyScreen: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if easy.collidePt(event.pos): maxZombies = 5 maxRobots = 5
def __init__(self,gamestate,score,ammo): # Konfuguracja random.seed() #inicjalizaja pygame.init() self.SCREEN_SIZE = (1280, 720) # grafiki dopasowane do tego self.screen = pygame.display.set_mode(self.SCREEN_SIZE) self.tps_clock = pygame.time.Clock() self.tps_delta = 0.0 self.shot = 0.0 self.supertime = -2 self.player = Player(self) self.aliens = [] self.opp = 10 self.bullets = [] self.alienbulets = [] self.lives = Lives(self) self.lives_number = 3 self.score = Score(self) self.score_number = 0 + score self.ammo = Ammo(self) self.walls = [] self.gameover = GameOver(self) self.gamestate = gamestate self.ammo_number = ammo + 5 self.nextlevel = NextLevel(self) self.tps_max = 300.0 self.superalien = [] self.tooClose = False self.pauseSign = Pause(self) self.pause = 1 for i in range(0,self.opp): self.aliens.append(Alien(self, i * 100 + 100, 100,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 150,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 200,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 250,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 300,self.gamestate-1)) self.aliens.append(Alien(self, i * 100 + 100, 350,self.gamestate-1)) self.rand_opp = 6*self.opp for i in range(0,5): self.walls.append(Wall(self,80+i*340)) channel_game = pygame.mixer.Channel(1) channel_game2 = pygame.mixer.Channel(2) channel_game3 = pygame.mixer.Channel(3) self.background = pygame.image.load("tlo3.jpg") self.text = open("score").readline() while self.gamestate !=0: if self.rand_opp != 0: los = random.randrange(self.rand_opp) else: los = 0 # obsługa zdarzen for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN and event.key == pygame.K_p: self.pause *= -1 elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and self.ammo_number != 0: self.bullets.append(Bullet(self,self.player.pos[0]+23,self.player.pos[1])) channel_game3.play(pygame.mixer.Sound("mygun.wav")) channel_game3.set_volume(0.5) elif (self.lives_number == 0 or self.ammo_number <= 0 or self.tooClose == True) and event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: self.gamestate = 0 elif len(self.aliens) == 0 and event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: self.gamestate += 1 Game(self.gamestate,self.score_number,self.ammo_number) self.text = open("score").readline() self.gamestate = 0 elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and self.pause == -1: self.gamestate = 0 elif event.type == pygame.KEYDOWN and event.key == pygame.K_p and self.pause == -1: self.pause *= -1 #ticking self.tps_delta += self.tps_clock.tick() / 1000.0 self.shot += self.tps_clock.tick()+0.000000003*(self.gamestate-1) / 1.0 self.supertime += self.tps_clock.tick() / 1.0 while self.tps_delta > 1 / self.tps_max: self.tick() self.tps_delta -= 1 / self.tps_max while(self.shot >= 0.001 / self.tps_max and len(self.aliens)!=0 and (self.lives_number != 0 and self.ammo_number > 0) and self.tooClose == False and self.pause == 1): self.shot = 0 channel_game.play(pygame.mixer.Sound("shot.wav")) channel_game.set_volume(0.5) self.alienbulets.append(AlienBullet(self,self.aliens[los].x,self.aliens[los].y)) while self.supertime >= 0.001 / self.tps_max: self.supertime = -2 if(len(self.superalien)==0 and self.tooClose == False and self.lives_number !=0 and self.ammo_number > 0 and self.pause == 1): self.superalien.append(SuperAlien(self)) channel_game2.play(pygame.mixer.Sound("supersound.wav")) channel_game2.set_volume(0.3) #rendering self.screen.fill((0, 0, 0)) self.screen.blit(self.background, (0, 0)) self.draw() pygame.display.flip()
def on_render(self, screen, clock): if self.dead == True: currFrame = self.frame triggerFrame = currFrame + 100 d = "dead" while currFrame != triggerFrame: self.p1.update(d, screen, self.board) self.p2.update(d, screen, self.board) currFrame += 1 pygame.display.flip() pygame.time.delay(700) self.switch_to_scene(GameOver(self.menuObject)) if pygame.key.get_pressed()[pygame.K_w] or pygame.key.get_pressed()[ pygame.K_UP]: direction = 'up' elif pygame.key.get_pressed()[pygame.K_a] or pygame.key.get_pressed()[ pygame.K_LEFT]: direction = 'left' elif pygame.key.get_pressed()[pygame.K_s] or pygame.key.get_pressed()[ pygame.K_DOWN]: direction = 'down' elif pygame.key.get_pressed()[pygame.K_d] or pygame.key.get_pressed()[ pygame.K_RIGHT]: direction = 'right' else: direction = 'idle' screen.fill((255, 255, 255)) screen.blit(self.get_image(self.path + self.background), (0, 0)) screen.blit( self.get_image(self.path + '/animation/label_level' + str(self.level) + '.png'), (0, 560)) for i in range(len(self.tiles)): screen.blit(self.get_image(self.path + self.tiles[i].path), self.tiles[i].getRealXY()) p1_status = self.p1.update(direction, screen, self.board) p2_status = self.p2.update(direction, screen, self.board) if p1_status[0] == "dead" or p2_status[0] == "dead": self.p1 = DeadPlayer(self.path + '/animation/character1/', p1_status[1], p1_status[2]) self.p2 = DeadPlayer(self.path + '/animation/character2/', p2_status[1], p2_status[2]) self.dead = True # self.switch_to_scene(GameOver(self.menuObject)) if p1_status[0] == "victory": print("Victory player 1") self.victory_count += 1 self.p1 = Victor(self.path + '/animation/character3/', p1_status[1], p1_status[2]) if p2_status[0] == "victory": print("Victory player 2") self.victory_count += 1 self.p2 = Victor(self.path + '/animation/character4/', p2_status[1], p2_status[2]) if self.victory_count == 2: pygame.time.delay(500) self.switch_to_scene(WonLevel(self.menuObject)) if p1_status[0] == "moving": fallingCoin = Animator(self.path + '/animation/coin/', p1_status[1], p1_status[2]) fallingCoin.realX += 12.5 fallingCoin.realY += 30 self.fallingCoins.append([fallingCoin, 0]) if p2_status[0] == "moving": fallingCoin = Animator(self.path + '/animation/coin/', p2_status[1], p2_status[2]) fallingCoin.realX += 12.5 fallingCoin.realY += 30 self.fallingCoins.append([fallingCoin, 0]) for i in range(len(self.fallingCoins) - 1, -1, -1): self.fallingCoins[i][1] += 0.3 self.fallingCoins[i][0].realY += self.fallingCoins[i][1] self.fallingCoins[i][0].update(screen) if self.fallingCoins[i][0].realY > 600: del self.fallingCoins[i] if self.frame % 3 == 0: self.particles.append( [random.randrange(0, 800), 610, random.randrange(1, 6)]) for i in range(len(self.particles) - 1, -1, -1): pygame.draw.circle(screen, (255, 255, 255, 100), (self.particles[i][0], self.particles[i][1]), self.particles[i][2], 1) self.particles[i][1] -= self.particles[i][2] if self.particles[i][1] < -10: del self.particles[i] for i in range(len(self.anims)): self.anims[i].update(screen) self.frame += 1 pygame.display.flip()
def game_over(self): gameover = GameOver(self.screen, self.MAP_POS, self.MAP_SIZE) return gameover.main_loop()
def play(self): state = constants.STATE_START score_board = Score(location=(constants.WINDOW_WIDTH - 30, 20)) play_button = Play( location=(int((constants.WINDOW_WIDTH - constants.PLAY_WIDTH) / 2), int((constants.WINDOW_HEIGHT - constants.PLAY_HEIGHT) / 2))) pause_button = Pause(location=(20, 20)) welcome_message = Welcome(location=( int((constants.WINDOW_WIDTH - constants.MESSAGE_WIDTH) / 2), int((constants.WINDOW_HEIGHT - constants.MESSAGE_HEIGHT) / 2))) gameover_message = GameOver(location=( int((constants.WINDOW_WIDTH - constants.GAMEOVER_WIDTH) / 2), int((constants.WINDOW_HEIGHT - constants.GAMEOVER_HEIGHT) / 3))) collided = False done = False while not done: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop elif event.type == pygame.KEYDOWN: if state == constants.STATE_START: if event.key == pygame.K_SPACE: state = constants.STATE_RUN self.nb_games += 1 if self.nb_games > 1: self.reset() elif state == constants.STATE_RUN: if event.key == pygame.K_SPACE: self.agent.flap() elif event.key == pygame.K_ESCAPE: state = constants.STATE_PAUSE elif state == constants.STATE_PAUSE: if event.key == pygame.K_SPACE: if collided: state = constants.STATE_RUN self.nb_games += 1 if self.nb_games > 1: self.reset() else: self.agent.resume(fps=self.fps) state = constants.STATE_RUN elif event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos # gets mouse position if state == constants.STATE_RUN: if pause_button.rect.collidepoint(mouse_pos): state = constants.STATE_PAUSE else: self.agent.flap() elif state == constants.STATE_START and welcome_message.rect.collidepoint( mouse_pos): state = constants.STATE_RUN self.nb_games += 1 if self.nb_games > 1: self.reset() elif state == constants.STATE_PAUSE: if gameover_message.rect.collidepoint(mouse_pos): state = constants.STATE_START collided = False elif play_button.rect.collidepoint(mouse_pos): self.agent.resume(fps=self.fps) state = constants.STATE_RUN if state == constants.STATE_START: self.redraw_screen(move=False) self.screen.blit(welcome_message.image, welcome_message.rect) elif state == constants.STATE_RUN: out_of_bounds = self.agent.move() self.meter_counts[1] += constants.SPEED if self.meter_counts[1] - self.meter_counts[ 0] > constants.PIPE_WIDTH: self.meter_counts[0] = self.meter_counts[1] self.update_pipes() collided, _ = self.check_collision() collided = collided or out_of_bounds if collided: state = constants.STATE_PAUSE score_board.update(score=self.score) self.redraw_screen() self.screen.blit(pause_button.image, pause_button.rect) self.screen.blit(score_board.surface, score_board.rect) elif state == constants.STATE_PAUSE: self.redraw_screen(move=False) if collided: self.screen.blit(gameover_message.image, gameover_message.rect) else: self.screen.blit(play_button.image, play_button.rect) # Limit the number of frames per second fps = self.fps if state == constants.STATE_RUN else int(self.fps / 2) self.clock.tick(fps) # Go ahead and update the screen with what we've drawn. pygame.display.flip() # Be IDLE friendly. If you forget this line, the program will 'hang' # on exit. pygame.quit()