def game_loop(): # CRIANDO O LOP global spaceship_x global spaceship_y global spaceship_alive global spaceship_hit_player global warning # MENSAGEM DE ATENÇÃO global warning_counter global warning_once global bullets global moving global highscore_file # ARMAZENAMENTO DA CONTAGEM DE PONTOS global highscore_int global score # PONTOS global cloud_x #NUVENS global cloud_y global enemy_heli_alive # O NOSSO INIMIGO global boat_alive # A VIDA DO NAVIO game_exit = False game_over = False gamer_over_selected = "JOGAR NOVAMENTE" while not game_exit: while game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: pygame.mixer.Sound.play(select) gamer_over_selected = "VOLTA AO JOGO" elif event.key == pygame.K_s or event.key == pygame.K_DOWN: pygame.mixer.Sound.play(select) gamer_over_selected = "quit" if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: pygame.mixer.Sound.play(select2) if gamer_over_selected == "VOLTA AO JOGO": if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() game_over = False score = 0 #PASSANDO O TAMANHO DO BALÃO balloon_x = 800 enemy_heli.x = -100 enemy_heli_alive = False # VIDAS DO INIMIGOS enemy_heli.bullets = [ ] # BALAS DO INIMIGOS boat.x = -110 boat_alive = False boat.bullets = [] spaceship_x = 800 spaceship_alive = False warning = False warning_counter = 0 # CONTADOR warning_counter = 0 # FUNÇÃO DO PLAY player.wreck_start = False player.y = display_height / 2 - 40 player.x = 100 player.wrecked = False player.health = 3 # VIDAS DO PLAYER bullets = [] game_loop() if gamer_over_selected == "quit": pygame.quit() quit() # MENSAGENS gamer_over_text = message_to_screen("JOGO OVER", font, 100, black) your_score = message_to_screen("SUA PONTUAÇÃO FOI: " + str(score), font, 50, black) # MOSTRA A PONTUAÇÃO if gamer_over_selected == "JOGAR NOVAMENTE": player_again = message_to_screen("JOGAR NOVAMENTE", font, 75, white) else: player_again = message_to_screen("JOGAR NOVAMENTE", font, 75, black) if gamer_over_selected == "quit": game_quit = message_to_screen("QUIT", font, 75, white) else: gamer_quit = message_to_screen("QUIT", font, 75, black) game_over_rect = gamer_over_text.get_rect() your_score_rect = your_score.get_rect() play_again_rect = play_again.get_rect() gamer_quit_rect = gamer_quit.get_rect() # JANELA DO GAMER OVER game_display.blit(gamer_over_text, (display_width / 2 - game_over_rect[2] / 2, 40)) game_display.blit( your_score, (display_width / 2 - your_score_rect[2] / 2 + 5, 100)) game_display.blit( player_again, (display_width / 2 - play_again_rect[2] / 2, 200)) game_display.blit( gamer_quit, (display_width / 2 - gamer_quit_rect[2] / 2, 260)) pygame.display.update() pygame.display.set_caption("VELOCIDADE DO HELICOPTER" + str(int(clock.get_fps())) + "POR SEGUNDO") clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if moving: # PARA FAZER O MEU BARCO ANDA if event.type == pygame.KEYDOWN: if event.key == pygame.K_w: player.moving_up = True if event.key == pygame.K_a: player.moving_left = True if event.key == pygame.K_s: player.moving_down = True if event.key == pygame.K_d: player.moving_right = True if event.key == pygame.K_SPACE: if not player.wreck_start: pygame.mixer.Sound.play( shoot) # COLOCANDO O SOM DO TIRO bullets.append([ player.x, player.y ]) # PARA FAZER AS BALAS APARECEREM if event.key == pygame.K_LSHIFT: if not player.wreck_start: pygame.mixer.Sound.play(bomb) bombs.append([player.x, player.y]) if event.key == pygame.K_p: pygame.mixer.Sound.play(select) pause() if event.type == pygame.KEYUP: if event.key == pygame.K_w: player.moving_up = False if event.key == pygame.K_a: player.moving_left = False if event.key == pygame.K_s: player.moving_down = False if event.key == pygame.K_d: player.moving_right = False #VIDA DO PLAYER if player.health < 1: # SE SOR ATINGIDO É MENOS 1 VIDA pygame.mixer.Sound.play( explosion) # se eu for atirando o som de explosãp player.wrecked() # CONTAGEM DE PERCA DE VIDA if player.wrecked: #SE EU FOR ATINGIDO VAI PASSA A VARIAVEL TRUE E VAI SUBTRAIR AS VIDAS ATE 0 game_over = True #CRIANDO O FUNDO COM AS NUVENS game_display.blit(sprites.background, (0, 0)) game_display.blit(sprites.cloud, (cloud_x, cloud_y)) if cloud_x <= 800 - 1100: cloud_x = 800 cloud_y = random.randint(0, 400) else: if not player.wreck_start: cloud_x -= 5 #CRIAR O NOSSO HELICOPITERO game_display.blit(player.current, (player.x, player.y)) # PLAYER game_display.blit(enemy_heli.current, (enemy_heli.x, enemy_heli.y)) # INIMIGO game_display.blit(sprites.spaceship, (spaceship_x, spaceship_y)) game_display.blit(sprites.boat, (boat.x, boat.y)) #BARCO player.player_init() enemy_heli.init() boat.init() if not player.wreck_start and not player.wrecked: for draw_bullet in bullets: # BALAS pygame.draw.rect(game_display, black, (draw_bullet[0] + 90, draw_bullet[1] + 40, 10, 10)) #DESENHANDO AS BALAS for move_bullet in range(len(bullets)): bullets[move_bullet][0] += 40 for del_bullet in bullets: if del_bullet[0] >= 800: bullets.remove(del_bullet) if not player.wreck_start and not player.wrecked: # DESENHANDO AS BOMBAS for draw_bomb in bombs: pygame.draw.rect(game_display, black, (draw_bomb[0] + 55, del_bomb[1] + 70, 20, 20)) for move_bomb in range(len(bombs)): bombs[move_bomb][1] += 20 for del_bomb in bombs: if del_bomb[1] >= 600: bombs.remove(del_bomb) if not player.wreck_start and not player.wrecked and not game_over: for draw_bullet in enemy_heli.bullets: #ATRIBUIR AO INIMIGO pygame.draw.rect(game_display, gray, (draw_bullet[0], draw_bullet[1] + 40, 40, 10)) # QUANDO O INIMIGO ATIRA pygame.draw.rect(game_display, red, (draw_bullet[0] + 30, draw_bullet[1] + 40, 10, 10)) # QUANDO O INIMIGO ACERTA O PLAYER for move_bullet in range(len(enemy_heli.bullets)): enemy_heli.bullets[move_bullet][0] -= 15 for del_bullet in enemy_heli.bullets: if del_bullet[0] <= -40: enemy_heli.bullets.remove(del_bullet) if not player.wrecked_start and not player.wrecked and not game_over: for draw_bullet in boat.bullets: pygame.draw.rect( game_display, gray, (draw_bullet[0] + 40, draw_bullet[1] + 30, 20, 20)) for move_bullet in range(len(boat.bullets)): boat.bullets[move_bullet][0] -= 10 boat.bullets[move_bullet][1] -= 10 for del_bullet in boat.bullets: if del_bullet[1] <= -40: boat.bullets.remove(del_bullet) for pop_balloon in bullets: if balloon_x < pop_balloon[ 0] + 90 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 40 < balloon_y + 100: pygame.mixer.Sound.play(pop) bullets.remove(pop_balloon) balloon_x = 800 - 870 # MEDIDAS DO BALAO score += 50 #PONTOS POR BALÃO elif balloon_x < pop_balloon[ 0] + 100 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 50 < balloon_y + 100: pygame.mixer.Sound.play(pop) bullets.remove(pop_balloon) balloon_x = 800 - 870 score += 50 for pop_balloon in bombs: if balloon_x < pop_balloon[ 0] + 55 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 70 < balloon_y + 100: pygame.mixer.Sound.play(pop) bombs.remove(pop_balloon) balloon_x = 800 - 870 score += 50 elif balloon_x < pop_balloon[ 0] + 75 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 90 < balloon_y + 100: pygame.mixer.Sound.play(pop) bombs.remove(pop_balloon) balloon_x = 800 - 870 score += 50 spaceship_spaw_num = random.randint(0, 100) if spaceship_spaw_num == 50 and not spaceship_alive and score > 450: warning = True if warning: if warning_once: pygame.mixer.Sound.play(alert) warning_once = False game_display.blit(warning_message, (750, spaceship_y - 15)) if warning_counter > 45: pygame.mixer.Sound.play(whoosh) spaceship_alive = True warning_counter = 0 warning = False warning_once = True else: warning_counter += 1 # PLAY if spaceship_alive: # SE ELE AINDA ESTIVER COM VIDA spaceship_x -= 30 if spaceship_x < 0 - 100: spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) #INIMIGO HELICOP enemy__spawn_num = random.randint(0, 100) if not enemy_heli_alive and score > 250 and enemy__spawn_num == 50: enemy_heli_alive = True enemy_heli.x = 800 #INIMIGO BARCO boat_spawn_num = random.randint(0, 200) if score > 700 and boat_spawn_num == 100 and not boat_alive: boat.x = 800 boat_alive = True if boat.x <= -110: boat_alive = False #TRABALHANDO AS COLISÕES #INIMIGO BALAS for hit_enemy_heli in bullets: if enemy_heli.x < hit_enemy_heli[0]+90 < enemy_heli.x+100\ or enemy_heli.x < hit_enemy_heli[0]+100 < enemy_heli.x+100: if enemy_heli.y < hit_enemy_heli[1]+40 < enemy_heli.y+80\ or enemy_heli.y < hit_enemy_heli[1]+50 < enemy_heli.y+80: if not enemy_heli.x > 600: pygame.mixer.Sound.play(explosion2) score += 150 bullets.remove(hit_enemy_heli) enemy_heli.x = -100 enemy_heli_alive = False for hit_spaceship in bullets: if spaceship_x < hit_spaceship[0]+90 < spaceship_x+100\ or spaceship_x < hit_spaceship[0]+100 < spaceship_x+100: if spaceship_y < hit_spaceship[1]+40 < spaceship_y +80\ or spaceship_y < hit_spaceship[1]+50 < spaceship_y+80: if not spaceship_x > 700: pygame.mixer.Sound.play(explosion2) bullets.remove(hit_spaceship) score += 200 spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) #BOMBAS for hit_spaceship in bombs: if spaceship_x < hit_spaceship[0]+55 < spaceship_x+100\ or spaceship_x < hit_spaceship[0]+65 < spaceship_x+100: if spaceship_y < hit_spaceship[1]+70 < spaceship_y +80\ or spaceship_y < hit_spaceship[1]+80 < spaceship_y+80: if not spaceship_x > 700: pygame.mixer.Sound.play(explosion2) bombs.remove(hit_spaceship) score += 200 spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) for hit_boat in bullets: if boat.x < hit_boat[ 0] + 90 < boat.x + 110 or boat.x < hit_spaceship[ 0] + 100 < boat.x + 110: if boat.y < hit_boat[ 1] + 40 < boat.y + 70 or boat.y < hit_boat[ 1] + 50 < boat.y + 70: if not boat.x > 780: pygame.mixer.Sound.play(explosion2) bullets.remove(hit_boat) score += 200 boat_alive = False boat.x = -110 # BOMBAS DO BOAT for hit_boat in bombs: if boat.x < hit_boat[ 0] + 55 < boat.x + 110 or boat.x < hit_spaceship[ 0] + 75 < boat.x + 110: if boat.y < hit_boat[ 1] + 70 < boat.y + 70 or boat.y < hit_boat[ 1] + 90 < boat.y + 70: if not boat.x > 780: pygame.mixer.Sound.play(explosion2) bombs.remove(hit_boat) score += 200 boat_alive = False boat.x = -110 #BALOES if balloon_x < player.x < balloon_x + 70 or balloon_x < player.x + 100 < balloon_x + 70: if balloon_y < player.y < balloon_y + 80 or balloon_y < player.y + 80 < balloon_y + 80: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 balloon_x = 800 - 870 #PLAYER for hit_player in enemy_heli.bullets: if player.x < hit_player[ 0] < player.x + 100 or player.x < hit_player[ 0] + 40 < player.x + 100: if player.y < hit_player[ 1] + 40 < player.y + 80 or player.y < hit_player[ 1] + 50 < player.y + 80: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 enemy_heli.bullets.remove(hit_player) # ATAQUES DO BOAT for hit_player in boat.bullets: if player.x < hit_player[ 0] < player.x + 100 or player.x < hit_player[ 0] + 20 < player.x + 100: if player.y < hit_player[ 1] < player.y + 80 or player.y < hit_player[ 1] + 20 < player.y + 80: pygame.mixer.Sound.play(explosion) if not boat.boat_hit_player: player.damaged = True player.health -= 1 boat.bullets.remove(hit_player) #CONDIÇÃO DO BOAT if boat.x < player.x < boat.x + 110 or boat.x < player.x + 100 < boat.x + 110: if boat.y < player.y < boat.y + 70 or boat.y < player.y + 80 < boat.y + 70: if not boat.boat_hit_player: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 boat.boat_hit_player = True if spaceship_x < player.x < spaceship_x + 100 or spaceship_x < player.x + 100 < spaceship_x + 100: if spaceship_y < player.y < spaceship_y + 88 or spaceship_y < player.y + 80 < spaceship_y + 88: if not spaceship_hit_player: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 spaceship_hit_player = True game_display.blit(sprites.balloon, (balloon_x, balloon_y)) if balloon_x <= 800 - 870: balloon_x = 800 # TAMANHA DO BALÃO balloon_y = random.randint(0, 400) else: if not player.wreck_start: balloon_x -= 7 #PARA APARECER A NOSSA PONTUAÇÃO game_display.blit( message_to_screen("PONTOS : {}".format(score), font, 50, black), (10, 10)) if score < highscore_int: hi_score_message = message_to_screen( "CONTAGEM : {}".format(highscore_int), font, 50, black) else: highscore_file = open('Pontuacao Maxima.dat', "w") highscore_file.write(str(score)) highscore_file.close() highscore_file = open('Pontuacao MAxima.dat', "r") highscore_int = int(highscore_file.read()) highscore_file.close() hi_score_message = message_to_screen( "CONTAGEM : {}".format(highscore_int), font, 50, yellow) hi_score_message_rect = hi_score_message.get_rect() game_display.blit(hi_score_message, (800 - hi_score_message_rect[2] - 10, 10)) #COLOCANDO NA TELA O SCORE #DESENHANDO AS VIDAS if player.health >= 1: game_display.blit(sprites.icon, (10, 50)) #ICONES DE VIDA if player.health >= 2: game_display.blit(sprites.icon, (10 + 32 + 10, 50)) if player.health >= 3: game_display.blit(sprites.icon, (10 + 32 + 10 + 32 + 10, 50)) # COMEÇO DO JOGO if godmode: score = 1000 # COMEÇA COM 1000 PONTOS player.health = 3 # COMEÇA COM 3 VIDAS #MAR ou OCEANO pygame.draw.rect(game_display, blue, (0, 500, 800, 100)) pygame.display.update() #PARA ATUALIZAR pygame.display.set_caption("VELOCIDADE DO HELICOPTER" + str(int(clock.get_fps())) + "POR SEGUNDOS") clock.tick(FPS)
def game_loop(): global spaceship_x global spaceship_y global spaceship_alive global spaceship_hit_player global warning global warning_counter global warning_once global bullets global moving global highscore_file global highscore_int global score global cloud_x global cloud_y global balloon_x global balloon_y global enemy_heli_alive global boat_alive game_exit = False game_over = False game_over_selected = "play again" while not game_exit: while game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: pygame.mixer.Sound.play(select) game_over_selected = "play again" elif event.key == pygame.K_s or event.key == pygame.K_DOWN: pygame.mixer.Sound.play(select) game_over_selected = "quit" if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: pygame.mixer.Sound.play(select2) if game_over_selected == "play again": if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() game_over = False score = 0 balloon_x = 800 enemy_heli.x = -100 enemy_heli_alive = False enemy_heli.bullets = [] boat.x = -110 boat_alive = False boat.bullets = [] spaceship_x = 800 spaceship_alive = False warning = False warning_counter = 0 warning_counter = 0 player.wreck_start = False player.y = display_height/2-40 player.x = 100 player.wrecked = False player.health = 3 bullets = [] game_loop() if game_over_selected == "quit": pygame.quit() quit() game_over_text = message_to_screen("GAME OVER", font, 100, black) your_score = message_to_screen("YOUR SCORE WAS: " + str(score), font, 50, black) if game_over_selected == "play again": play_again = message_to_screen("PLAY AGAIN", font, 75, white) else: play_again = message_to_screen("PLAY AGAIN", font, 75, black) if game_over_selected == "quit": game_quit = message_to_screen("QUIT", font, 75, white) else: game_quit = message_to_screen("QUIT", font, 75, black) game_over_rect = game_over_text.get_rect() your_score_rect = your_score.get_rect() play_again_rect = play_again.get_rect() game_quit_rect = game_quit.get_rect() game_display.blit(game_over_text, (display_width/2 - game_over_rect[2]/2, 40)) game_display.blit(your_score, (display_width/2 - (your_score_rect[2]/2+5), 100)) game_display.blit(play_again, (display_width/2 - play_again_rect[2]/2, 200)) game_display.blit(game_quit, (display_width/2 - game_quit_rect[2]/2, 260)) pygame.display.update() pygame.display.set_caption("HELICOPTER running at " + str(int(clock.get_fps())) + " frames per second.") clock.tick(10) # event handler for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if moving: if event.type == pygame.KEYDOWN: if event.key == pygame.K_w: player.moving_up = True if event.key == pygame.K_a: player.moving_left = True if event.key == pygame.K_s: player.moving_down = True if event.key == pygame.K_d: player.moving_right = True if event.key == pygame.K_SPACE: if not player.wreck_start: pygame.mixer.Sound.play(shoot) bullets.append([player.x, player.y]) if event.key == pygame.K_LSHIFT: if not player.wreck_start: pygame.mixer.Sound.play(bomb) bombs.append([player.x, player.y]) if event.key == pygame.K_p: pygame.mixer.Sound.play(select) pause() if event.type == pygame.KEYUP: if event.key == pygame.K_w: player.moving_up = False if event.key == pygame.K_a: player.moving_left = False if event.key == pygame.K_s: player.moving_down = False if event.key == pygame.K_d: player.moving_right = False if player.health < 1: pygame.mixer.Sound.play(explosion) player.wreck() if player.wrecked: game_over = True # draw background and randomly positioned clouds game_display.blit(sprites.background, (0, 0)) game_display.blit(sprites.cloud, (cloud_x, cloud_y)) if cloud_x <= 800 - 1100: cloud_x = 800 cloud_y = random.randint(0, 400) else: if not player.wreck_start: cloud_x -= 5 # drawing player game_display.blit(player.current, (player.x, player.y)) # drawing enemy helicopter game_display.blit(enemy_heli.current, (enemy_heli.x, enemy_heli.y)) # drawing spaceship game_display.blit(sprites.spaceship, (spaceship_x, spaceship_y)) # drawing boat game_display.blit(sprites.boat, (boat.x, boat.y)) # enabling movement and animations player.player_init() enemy_heli.init() boat.init() # rendering bullets if not player.wreck_start and not player.wrecked: for draw_bullet in bullets: pygame.draw.rect(game_display, black, (draw_bullet[0]+90, draw_bullet[1]+40, 10, 10)) for move_bullet in range(len(bullets)): bullets[move_bullet][0] += 40 for del_bullet in bullets: if del_bullet[0] >= 800: bullets.remove(del_bullet) # rendering bombs if not player.wreck_start and not player.wrecked: for draw_bomb in bombs: pygame.draw.rect(game_display, black, (draw_bomb[0]+55, draw_bomb[1]+70, 20, 20)) for move_bomb in range(len(bombs)): bombs[move_bomb][1] += 20 for del_bomb in bombs: if del_bomb[1] > 600: bombs.remove(del_bomb) # rendering enemy bullets if not player.wreck_start and not player.wrecked and not game_over: for draw_bullet in enemy_heli.bullets: pygame.draw.rect(game_display, gray, (draw_bullet[0], draw_bullet[1]+40, 40, 10)) pygame.draw.rect(game_display, red, (draw_bullet[0]+30, draw_bullet[1]+40, 10, 10)) for move_bullet in range(len(enemy_heli.bullets)): enemy_heli.bullets[move_bullet][0] -= 15 for del_bullet in enemy_heli.bullets: if del_bullet[0] <= -40: enemy_heli.bullets.remove(del_bullet) # rendering boat bullets if not player.wreck_start and not player.wrecked and not game_over: for draw_bullet in boat.bullets: pygame.draw.rect(game_display, gray, (draw_bullet[0]+40, draw_bullet[1]+30, 20, 20)) for move_bullet in range(len(boat.bullets)): boat.bullets[move_bullet][0] -= 10 boat.bullets[move_bullet][1] -= 10 for del_bullet in boat.bullets: if del_bullet[1] < -40: boat.bullets.remove(del_bullet) # draw randomly positioned balloons, pop if they hit any bullet or bombs for pop_balloon in bullets: if balloon_x < pop_balloon[0]+90 < balloon_x+70 and balloon_y < pop_balloon[1]+40 < balloon_y+100: pygame.mixer.Sound.play(pop) bullets.remove(pop_balloon) balloon_x = 800-870 score += 50 elif balloon_x < pop_balloon[0]+100 < balloon_x+70 and balloon_y < pop_balloon[1]+50 < balloon_y+100: pygame.mixer.Sound.play(pop) bullets.remove(pop_balloon) balloon_x = 800-870 score += 50 for pop_balloon in bombs: if balloon_x < pop_balloon[0]+55 < balloon_x+70 and balloon_y < pop_balloon[1]+70 < balloon_y+100: pygame.mixer.Sound.play(pop) bombs.remove(pop_balloon) balloon_x = 800-870 score += 50 elif balloon_x < pop_balloon[0]+75 < balloon_x+70 and balloon_y < pop_balloon[1]+90 < balloon_y+100: pygame.mixer.Sound.play(pop) bombs.remove(pop_balloon) balloon_x = 800-870 score += 50 # spawn spaceship randomly spaceship_spawn_num = random.randint(0, 100) if spaceship_spawn_num == 50 and not spaceship_alive and score > 450: warning = True # show warning before spaceship spawning if warning: if warning_once: pygame.mixer.Sound.play(alert) warning_once = False game_display.blit(warning_message, (750, spaceship_y-15)) if warning_counter > 45: pygame.mixer.Sound.play(whoosh) spaceship_alive = True warning_counter = 0 warning = False warning_once = True else: warning_counter += 1 # spaceship movement if spaceship_alive: spaceship_x -= 30 if spaceship_x < 0-100: spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) # spawn enemy helicopter randomly enemy_spawn_num = random.randint(0, 100) if not enemy_heli_alive and score > 250 and enemy_spawn_num == 50: enemy_heli_alive = True enemy_heli.x = 800 # spawn boat randomly boat_spawn_num = random.randint(0, 200) if score > 700 and boat_spawn_num == 100 and not boat_alive: boat.x = 800 boat_alive = True if boat.x <= -110: boat_alive = False # enemy-player bullet collision detection for hit_enemy_heli in bullets: if enemy_heli.x < hit_enemy_heli[0]+90 < enemy_heli.x+100 \ or enemy_heli.x < hit_enemy_heli[0]+100 < enemy_heli.x+100: if enemy_heli.y < hit_enemy_heli[1]+40 < enemy_heli.y+80 \ or enemy_heli.y < hit_enemy_heli[1]+50 < enemy_heli.y+80: if not enemy_heli.x > 600: pygame.mixer.Sound.play(explosion2) score += 150 bullets.remove(hit_enemy_heli) enemy_heli.x = -100 enemy_heli_alive = False # spaceship-player bullet/bomb collision detection for hit_spaceship in bullets: if spaceship_x < hit_spaceship[0]+90 < spaceship_x+100 \ or spaceship_x < hit_spaceship[0]+100 < spaceship_x+100: if spaceship_y < hit_spaceship[1]+40 < spaceship_y+80 \ or spaceship_y < hit_spaceship[1]+50 < spaceship_y+80: if not spaceship_x > 700: pygame.mixer.Sound.play(explosion2) bullets.remove(hit_spaceship) score += 200 spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) for hit_spaceship in bombs: if spaceship_x < hit_spaceship[0]+55 < spaceship_x+100 \ or spaceship_x < hit_spaceship[0]+65 < spaceship_x+100: if spaceship_y < hit_spaceship[1]+70 < spaceship_y+80 \ or spaceship_y < hit_spaceship[1]+80 < spaceship_y+80: if not spaceship_x > 700: pygame.mixer.Sound.play(explosion2) bombs.remove(hit_spaceship) score += 200 spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) # boat-player bullet/bomb collision detection for hit_boat in bullets: if boat.x < hit_boat[0]+90 < boat.x+110 or boat.x < hit_spaceship[0]+100 < boat.x+110: if boat.y < hit_boat[1]+40 < boat.y+70 or boat.y < hit_boat[1]+50 < boat.y+70: if not boat.x > 780: pygame.mixer.Sound.play(explosion2) bullets.remove(hit_boat) score += 200 boat_alive = False boat.x = -110 for hit_boat in bombs: if boat.x < hit_boat[0]+55 < boat.x+110 or boat.x < hit_spaceship[0]+75 < boat.x+110: if boat.y < hit_boat[1]+70 < boat.y+70 or boat.y < hit_boat[1]+90 < boat.y+70: if not boat.x > 780: pygame.mixer.Sound.play(explosion2) bombs.remove(hit_boat) score += 200 boat_alive = False boat.x = -110 # player-ballon collision detection if balloon_x < player.x < balloon_x+70 or balloon_x < player.x+100 < balloon_x+70: if balloon_y < player.y < balloon_y+80 or balloon_y < player.y+80 < balloon_y+80: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 balloon_x = 800-870 # player-enemy rocket collision detection for hit_player in enemy_heli.bullets: if player.x < hit_player[0] < player.x+100 or player.x < hit_player[0]+40 < player.x+100: if player.y < hit_player[1]+40 < player.y+80 or player.y < hit_player[1]+50 < player.y+80: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 enemy_heli.bullets.remove(hit_player) # player-boat bullet collision detection for hit_player in boat.bullets: if player.x < hit_player[0] < player.x+100 or player.x < hit_player[0]+20 < player.x+100: if player.y < hit_player[1] < player.y+80 or player.y < hit_player[1]+20 < player.y+80: pygame.mixer.Sound.play(explosion) if not boat.boat_hit_player: player.damaged = True player.health -= 1 boat.bullets.remove(hit_player) # player-boat collision detection if boat.x < player.x < boat.x+110 or boat.x < player.x+100 < boat.x+110: if boat.y < player.y < boat.y+70 or boat.y < player.y+80 < boat.y+70: if not boat.boat_hit_player: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 boat.boat_hit_player = True # player-spaceship collision detection if spaceship_x < player.x < spaceship_x+100 or spaceship_x < player.x+100 < spaceship_x+100: if spaceship_y < player.y < spaceship_y+88 or spaceship_y < player.y+80 < spaceship_y+88: if not spaceship_hit_player: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 spaceship_hit_player = True game_display.blit(sprites.balloon, (balloon_x, balloon_y)) if balloon_x <= 800 - 870: balloon_x = 800 balloon_y = random.randint(0, 400) else: if not player.wreck_start: balloon_x -= 7 # draw score game_display.blit(message_to_screen("SCORE: {0}".format(score), font, 50, black), (10, 10)) # draw high score if score < highscore_int: hi_score_message = message_to_screen("HI-SCORE: {0}".format(highscore_int), font, 50, black) else: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() highscore_file = open('highscore.dat', "r") highscore_int = int(highscore_file.read()) highscore_file.close() hi_score_message = message_to_screen("HI-SCORE: {0}".format(highscore_int), font, 50, yellow) hi_score_message_rect = hi_score_message.get_rect() game_display.blit(hi_score_message, (800-hi_score_message_rect[2]-10, 10)) # draw health if player.health >= 1: game_display.blit(sprites.icon, (10, 50)) if player.health >= 2: game_display.blit(sprites.icon, (10+32+10, 50)) if player.health >= 3: game_display.blit(sprites.icon, (10+32+10+32+10, 50)) # god-mode (for quicker testing) if godmode: score = 1000 player.health = 3 # drawing ocean pygame.draw.rect(game_display, blue, (0, 500, 800, 100)) pygame.display.update() pygame.display.set_caption("HELICOPTER running at " + str(int(clock.get_fps())) + " frames per second.") clock.tick(FPS)
def game_loop(): global spaceship_x global spaceship_y global spaceship_alive global spaceship_hit_player global warning global warning_counter global warning_once global bullets global moving global highscore_file global highscore_int global score global cloud_x global cloud_y global balloon_x global balloon_y global enemy_heli_alive global boat_alive game_exit = False game_over = False game_over_selected = "Zagraj Ponownie" while not game_exit: while game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: pygame.mixer.Sound.play(select) game_over_selected = "Zagraj Ponownie" elif event.key == pygame.K_s or event.key == pygame.K_DOWN: pygame.mixer.Sound.play(select) game_over_selected = "Wyjdz" if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: pygame.mixer.Sound.play(select2) if game_over_selected == "Zagraj Ponownie": if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() game_over = False score = 0 balloon_x = 800 enemy_heli.x = -100 enemy_heli_alive = False enemy_heli.bullets = [] boat.x = -110 boat_alive = False boat.bullets = [] spaceship_x = 800 spaceship_alive = False warning = False warning_counter = 0 warning_counter = 0 player.wreck_start = False player.y = display_height / 2 - 40 player.x = 100 player.wrecked = False player.health = 3 bullets = [] game_loop() if game_over_selected == "Wyjdz": pygame.quit() quit() game_over_text = message_to_screen("GAME OVER", font, 100, black) your_score = message_to_screen("Twoj Wynik: " + str(score), font1, 50, black) if game_over_selected == "Zagraj Ponownie": play_again = message_to_screen("Zagraj Ponownie", font, 75, white) else: play_again = message_to_screen("Zagraj Ponownie", font, 75, black) if game_over_selected == "Wyjdz": game_quit = message_to_screen("Wyjdz", font, 75, white) else: game_quit = message_to_screen("Wyjdz", font, 75, black) game_over_rect = game_over_text.get_rect() your_score_rect = your_score.get_rect() play_again_rect = play_again.get_rect() game_quit_rect = game_quit.get_rect() game_display.blit(game_over_text, (display_width / 2 - game_over_rect[2] / 2, 40)) game_display.blit(your_score, (display_width / 2 - (your_score_rect[2] / 2 + 5), 100)) game_display.blit( play_again, (display_width / 2 - play_again_rect[2] / 2, 200)) game_display.blit(game_quit, (display_width / 2 - game_quit_rect[2] / 2, 260)) pygame.display.update() pygame.display.set_caption("Sky Fight " + str(int(clock.get_fps())) + " FPS.") clock.tick(10) # event handler for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if moving: if event.type == pygame.KEYDOWN: if event.key == pygame.K_w: player.moving_up = True if event.key == pygame.K_a: player.moving_left = True if event.key == pygame.K_s: player.moving_down = True if event.key == pygame.K_d: player.moving_right = True if event.key == pygame.K_SPACE: if not player.wreck_start: pygame.mixer.Sound.play(shoot) bullets.append([player.x, player.y]) if event.key == pygame.K_LSHIFT: if not player.wreck_start: pygame.mixer.Sound.play(bomb) bombs.append([player.x, player.y]) if event.key == pygame.K_p: pygame.mixer.Sound.play(select) pause() if event.type == pygame.KEYUP: if event.key == pygame.K_w: player.moving_up = False if event.key == pygame.K_a: player.moving_left = False if event.key == pygame.K_s: player.moving_down = False if event.key == pygame.K_d: player.moving_right = False if player.health < 1: pygame.mixer.Sound.play(explosion) player.wrecked = True if player.wrecked: game_over = True # rysownaie tła i losowe rozmieszczanie chmur game_display.blit(sprites.background, (0, 0)) game_display.blit(sprites.cloud, (cloud_x, cloud_y)) if cloud_x <= 800 - 1100: cloud_x = 800 cloud_y = random.randint(0, 400) else: if not player.wreck_start: cloud_x -= 5 # gracz game_display.blit(player.current, (player.x, player.y)) # wrogi helikopter game_display.blit(enemy_heli.current, (enemy_heli.x, enemy_heli.y)) # szybki-statek game_display.blit(sprites.spaceship, (spaceship_x, spaceship_y)) # statek game_display.blit(sprites.boat, (boat.x, boat.y)) # inicjalizacja poruszania i animacji player.player_init() enemy_heli.init() boat.init() # pociski if not player.wreck_start and not player.wrecked: for draw_bullet in bullets: pygame.draw.rect( game_display, black, (draw_bullet[0] + 90, draw_bullet[1] + 40, 10, 10)) for move_bullet in range(len(bullets)): bullets[move_bullet][0] += 40 for del_bullet in bullets: if del_bullet[0] >= 800: bullets.remove(del_bullet) # bomby if not player.wreck_start and not player.wrecked: for draw_bomb in bombs: pygame.draw.rect( game_display, black, (draw_bomb[0] + 55, draw_bomb[1] + 70, 20, 20)) for move_bomb in range(len(bombs)): bombs[move_bomb][1] += 20 for del_bomb in bombs: if del_bomb[1] > 600: bombs.remove(del_bomb) # wrogie pociski if not player.wreck_start and not player.wrecked and not game_over: for draw_bullet in enemy_heli.bullets: pygame.draw.rect(game_display, gray, (draw_bullet[0], draw_bullet[1] + 40, 40, 10)) pygame.draw.rect( game_display, red, (draw_bullet[0] + 30, draw_bullet[1] + 40, 10, 10)) for move_bullet in range(len(enemy_heli.bullets)): enemy_heli.bullets[move_bullet][0] -= 15 for del_bullet in enemy_heli.bullets: if del_bullet[0] <= -40: enemy_heli.bullets.remove(del_bullet) # pociski wrogiego statku if not player.wreck_start and not player.wrecked and not game_over: for draw_bullet in boat.bullets: pygame.draw.rect( game_display, gray, (draw_bullet[0] + 40, draw_bullet[1] + 30, 20, 20)) for move_bullet in range(len(boat.bullets)): boat.bullets[move_bullet][0] -= 10 boat.bullets[move_bullet][1] -= 10 for del_bullet in boat.bullets: if del_bullet[1] < -40: boat.bullets.remove(del_bullet) # definicja zachowanie balonow for pop_balloon in bullets: if balloon_x < pop_balloon[ 0] + 90 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 40 < balloon_y + 100: pygame.mixer.Sound.play(pop) bullets.remove(pop_balloon) balloon_x = 800 - 870 score += 50 elif balloon_x < pop_balloon[ 0] + 100 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 50 < balloon_y + 100: pygame.mixer.Sound.play(pop) bullets.remove(pop_balloon) balloon_x = 800 - 870 score += 50 for pop_balloon in bombs: if balloon_x < pop_balloon[ 0] + 55 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 70 < balloon_y + 100: pygame.mixer.Sound.play(pop) bombs.remove(pop_balloon) balloon_x = 800 - 870 score += 50 elif balloon_x < pop_balloon[ 0] + 75 < balloon_x + 70 and balloon_y < pop_balloon[ 1] + 90 < balloon_y + 100: pygame.mixer.Sound.play(pop) bombs.remove(pop_balloon) balloon_x = 800 - 870 score += 50 # definicja statkow wroga spaceship_spawn_num = random.randint(0, 100) if spaceship_spawn_num == 50 and not spaceship_alive and score > 450: warning = True # ostrzezenie o szybkim-statku if warning: if warning_once: pygame.mixer.Sound.play(alert) warning_once = False game_display.blit(warning_message, (750, spaceship_y - 15)) if warning_counter > 45: pygame.mixer.Sound.play(whoosh) spaceship_alive = True warning_counter = 0 warning = False warning_once = True else: warning_counter += 1 # poruszanie wrogiego szybkiego-statku if spaceship_alive: spaceship_x -= 30 if spaceship_x < 0 - 100: spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) # zrzut wrogich statkow enemy_spawn_num = random.randint(0, 100) if not enemy_heli_alive and score > 250 and enemy_spawn_num == 50: enemy_heli_alive = True enemy_heli.x = 800 # zrzut wrogiej łodzi boat_spawn_num = random.randint(0, 200) if score > 700 and boat_spawn_num == 100 and not boat_alive: boat.x = 800 boat_alive = True if boat.x <= -110: boat_alive = False # wykrywanie trafien we wrogi statek for hit_enemy_heli in bullets: if enemy_heli.x < hit_enemy_heli[0]+90 < enemy_heli.x+100 \ or enemy_heli.x < hit_enemy_heli[0]+100 < enemy_heli.x+100: if enemy_heli.y < hit_enemy_heli[1]+40 < enemy_heli.y+80 \ or enemy_heli.y < hit_enemy_heli[1]+50 < enemy_heli.y+80: if not enemy_heli.x > 600: pygame.mixer.Sound.play(explosion2) score += 150 bullets.remove(hit_enemy_heli) enemy_heli.x = -100 enemy_heli_alive = False # wykrywanie trafienia w szybki-statek for hit_spaceship in bullets: if spaceship_x < hit_spaceship[0]+90 < spaceship_x+100 \ or spaceship_x < hit_spaceship[0]+100 < spaceship_x+100: if spaceship_y < hit_spaceship[1]+40 < spaceship_y+80 \ or spaceship_y < hit_spaceship[1]+50 < spaceship_y+80: if not spaceship_x > 700: pygame.mixer.Sound.play(explosion2) bullets.remove(hit_spaceship) score += 200 spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) for hit_spaceship in bombs: if spaceship_x < hit_spaceship[0]+55 < spaceship_x+100 \ or spaceship_x < hit_spaceship[0]+65 < spaceship_x+100: if spaceship_y < hit_spaceship[1]+70 < spaceship_y+80 \ or spaceship_y < hit_spaceship[1]+80 < spaceship_y+80: if not spaceship_x > 700: pygame.mixer.Sound.play(explosion2) bombs.remove(hit_spaceship) score += 200 spaceship_hit_player = False spaceship_alive = False spaceship_x = 800 spaceship_y = random.randint(0, 400) # wykrywanie trafienia w statek for hit_boat in bullets: if boat.x < hit_boat[ 0] + 90 < boat.x + 110 or boat.x < hit_spaceship[ 0] + 100 < boat.x + 110: if boat.y < hit_boat[1] + 40 < boat.y + 70 or boat.y < hit_boat[ 1] + 50 < boat.y + 70: if not boat.x > 780: pygame.mixer.Sound.play(explosion2) bullets.remove(hit_boat) score += 200 boat_alive = False boat.x = -110 for hit_boat in bombs: if boat.x < hit_boat[ 0] + 55 < boat.x + 110 or boat.x < hit_spaceship[ 0] + 75 < boat.x + 110: if boat.y < hit_boat[1] + 70 < boat.y + 70 or boat.y < hit_boat[ 1] + 90 < boat.y + 70: if not boat.x > 780: pygame.mixer.Sound.play(explosion2) bombs.remove(hit_boat) score += 200 boat_alive = False boat.x = -110 # wykrywanie kolizji z balonem if balloon_x < player.x < balloon_x + 70 or balloon_x < player.x + 100 < balloon_x + 70: if balloon_y < player.y < balloon_y + 80 or balloon_y < player.y + 80 < balloon_y + 80: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 balloon_x = 800 - 870 # wykrywanie trafienia przez pocisk wrogiego statku for hit_player in enemy_heli.bullets: if player.x < hit_player[ 0] < player.x + 100 or player.x < hit_player[ 0] + 40 < player.x + 100: if player.y < hit_player[ 1] + 40 < player.y + 80 or player.y < hit_player[ 1] + 50 < player.y + 80: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 enemy_heli.bullets.remove(hit_player) # trafienie przez wrogi statek for hit_player in boat.bullets: if player.x < hit_player[ 0] < player.x + 100 or player.x < hit_player[ 0] + 20 < player.x + 100: if player.y < hit_player[ 1] < player.y + 80 or player.y < hit_player[ 1] + 20 < player.y + 80: pygame.mixer.Sound.play(explosion) if not boat.boat_hit_player: player.damaged = True player.health -= 1 boat.bullets.remove(hit_player) # wykrywanie kolizji gracza ze statkiem if boat.x < player.x < boat.x + 110 or boat.x < player.x + 100 < boat.x + 110: if boat.y < player.y < boat.y + 70 or boat.y < player.y + 80 < boat.y + 70: if not boat.boat_hit_player: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 boat.boat_hit_player = True # wykrywanie kolizji z szybkim-statkiem if spaceship_x < player.x < spaceship_x + 100 or spaceship_x < player.x + 100 < spaceship_x + 100: if spaceship_y < player.y < spaceship_y + 88 or spaceship_y < player.y + 80 < spaceship_y + 88: if not spaceship_hit_player: pygame.mixer.Sound.play(explosion) player.damaged = True player.health -= 1 spaceship_hit_player = True game_display.blit(sprites.balloon, (balloon_x, balloon_y)) if balloon_x <= 800 - 870: balloon_x = 800 balloon_y = random.randint(0, 400) else: if not player.wreck_start: balloon_x -= 7 # wyswietlanie wyniku game_display.blit( message_to_screen("Wynik: {0}".format(score), font1, 50, black), (10, 10)) # wyswietlanie najwyzszego wyniku if score < highscore_int: hi_score_message = message_to_screen( "HI-SCORE: {0}".format(highscore_int), font1, 50, black) else: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() highscore_file = open('highscore.dat', "r") highscore_int = int(highscore_file.read()) highscore_file.close() hi_score_message = message_to_screen( "HI-SCORE: {0}".format(highscore_int), font, 50, yellow) hi_score_message_rect = hi_score_message.get_rect() game_display.blit(hi_score_message, (800 - hi_score_message_rect[2] - 10, 10)) # definicja zyc gracza if player.health >= 1: game_display.blit(sprites.icon, (10, 50)) if player.health >= 2: game_display.blit(sprites.icon, (10 + 32 + 10, 50)) if player.health >= 3: game_display.blit(sprites.icon, (10 + 32 + 10 + 32 + 10, 50)) # tryb god mode do testow if godmode: score = 1000 player.health = 3 # rysowanie oceanu game_display.blit(sprites.water, (0, 500, 800, 100)) pygame.display.update() pygame.display.set_caption("Sky FIght " + str(int(clock.get_fps())) + " FPS.") clock.tick(FPS)
def game_loop(): global background_width global missile_x global missile_y global missile_alive global missile_hit_player global warning global warning_counter global warning_once global bullets global moving global red_heart_x global red_heart_y global highscore_file global highscore_int global score global hard_mode global cloud_x global cloud_y global enemy_heli_alive game_exit = False game_over = False game_over_selected = "play again" while not game_exit: while game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: game_over_selected = "play again" elif event.key == pygame.K_s or event.key == pygame.K_DOWN: game_over_selected = "quit" if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: if game_over_selected == "play again": player.moving_up = False player.moving_down = False if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() game_over = False score = 0 red_heart_x = 800 enemy_heli.x = -100 enemy_heli_alive = False enemy_heli.bullets = [] missile_x = 800 missile_alive = False warning = False warning_counter = 0 warning_counter = 0 player.wreck_start = False player.y = HALF_WINWIDTH - 40 player.x = 100 player.wrecked = False player.health = 3 bullets = [] game_loop() if game_over_selected == "quit": pygame.quit() quit() # game over screen game_over_text = message_to_screen("GAME OVER", None, 72, BLACK) game_over_caption = message_to_screen("TRY AGAIN?", None, 36, BLACK) if game_over_selected == "play again": play_again = message_to_screen("YES, PLAY AGAIN",None, 36, WHITE) else: play_again = message_to_screen("YES, PLAY AGAIN",None, 36, BLACK) if game_over_selected == "quit": game_quit = message_to_screen("NO, QUIT",None, 36, WHITE) else: game_quit = message_to_screen("NO, QUIT", None, 36, BLACK) game_over_rect = game_over_text.get_rect() caption_rect = game_over_caption.get_rect() play_again_rect = play_again.get_rect() game_quit_rect = game_quit.get_rect() DISPLAYSURF.blit(game_over_text, (HALF_WINWIDTH - game_over_rect[2]/2, 80)) DISPLAYSURF.blit(game_over_caption, (HALF_WINWIDTH - (caption_rect[2]/2+5), 160)) DISPLAYSURF.blit(play_again, (HALF_WINWIDTH - play_again_rect[2]/2, 260)) DISPLAYSURF.blit(game_quit, (HALF_WINWIDTH - game_quit_rect[2]/2, 320)) pygame.display.update() pygame.display.set_caption("Helicopter shooter (" + str(int(FPSCLOCK.get_fps())) + " FPS)") FPSCLOCK.tick(10) # event handler for event in pygame.event.get(): if event.type == pygame.QUIT: if score > highscore_int: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() pygame.quit() quit() # player move ''' 1. click and hold left mouse to go up 2. release to go down ''' left_click = 1 if moving: if event.type == pygame.MOUSEBUTTONDOWN: if event.button == left_click: player.moving_up = True player.moving_down = False if event.type == pygame.MOUSEBUTTONUP: player.moving_up = False player.moving_down = True if player.health < 1: player.wreck() if player.wrecked: game_over = True # scrolling background DISPLAYSURF.blit(sprites.bg1, (background_width, 0)) DISPLAYSURF.blit(sprites.bg2, (background_width - 1920, 0)) if score > hard_mode: background_width -= 6 elif score < hard_mode: background_width -= 2 if background_width <= 0: background_width = 1920 # drawing player DISPLAYSURF.blit(player.current, (player.x, player.y)) # drawing enemy helicopter DISPLAYSURF.blit(enemy_heli.current, (enemy_heli.x, enemy_heli.y)) # drawing missile DISPLAYSURF.blit(sprites.missile, (missile_x, missile_y)) # enabling movement and animations player.player_init() enemy_heli.init() # rendering enemy bullets if not player.wreck_start and not player.wrecked and not game_over: for draw_bullet in enemy_heli.bullets: pygame.draw.rect(DISPLAYSURF, GRAY, (draw_bullet[0], draw_bullet[1]+40, 40, 10)) pygame.draw.rect(DISPLAYSURF, RED, (draw_bullet[0]+30, draw_bullet[1]+40, 10, 10)) for move_bullet in range(len(enemy_heli.bullets)): enemy_heli.bullets[move_bullet][0] -= 15 for del_bullet in enemy_heli.bullets: if del_bullet[0] <= -40: enemy_heli.bullets.remove(del_bullet) # spawn red heart randomly DISPLAYSURF.blit(sprites.red_heart, (red_heart_x, red_heart_y)) magicnumber = random.randint(1, 1000) if player.health < 3: if magicnumber == 500: # 0.1% rate red_heart_x = 800 red_heart_y = random.randint(0, 400) else: if not player.wreck_start: red_heart_x -= 9 elif player.health < 2: if magicnumber in range(490, 500): # 1% rate red_heart_x = 800 red_heart_y = random.randint(0, 400) else: if not player.wreck_start: red_heart_x -= 9 # spawn missile randomly missile_spawn_num = random.randint(0, 100) if score > hard_mode: if not missile_alive: #continuous missile warning = True else: if missile_spawn_num == 50 and not missile_alive and score > 150: warning = True # show warning before missile spawning if warning: if warning_once: warning_once = False DISPLAYSURF.blit(warning_message, (750, missile_y-15)) if warning_counter > 45: missile_alive = True warning_counter = 0 warning = False warning_once = True else: warning_counter += 1 # missile movement hardmode_missile_speed = random.randint(30, 60) if missile_alive: if score > hard_mode: missile_x -= hardmode_missile_speed else: missile_x -= 30 if missile_x < 0-100: missile_hit_player = False missile_alive = False missile_x = 800 missile_y = player.y # spawn enemy helicopter randomly enemy_spawn_num = random.randint(0, 100) if not enemy_heli_alive and score > 650 and enemy_spawn_num == 50: enemy_heli_alive = True enemy_heli.x = 800 enemy_heli.y = player.y counter = score if enemy_heli_alive == True: if score > hard_mode: if score > counter + 550: enemy_heli.x = -100 enemy_heli_alive = False else: enemy_heli.y = player.y else: if score > counter + 300: enemy_heli.x = -100 enemy_heli_alive = False else: enemy_heli.y = player.y # player-heart collision detection if red_heart_x < player.x < red_heart_x+70 or red_heart_x < player.x+100 < red_heart_x+70: if red_heart_y < player.y < red_heart_y+80 or red_heart_y < player.y+80 < red_heart_y+80: if player.health < 3: player.health += 1 red_heart_x = 800-870 # player-enemy helicopter collision detection for hit_player in enemy_heli.bullets: if player.x < hit_player[0] < player.x+100 or player.x < hit_player[0]+40 < player.x+100: if player.y < hit_player[1]+40 < player.y+80 or player.y < hit_player[1]+50 < player.y+80: player.damaged = True player.health -= 1 enemy_heli.bullets.remove(hit_player) # player-missile collision detection if missile_x < player.x < missile_x+100 or missile_x < player.x+100 < missile_x+100: if missile_y < player.y < missile_y+88 or missile_y < player.y+80 < missile_y+88: if not missile_hit_player: player.damaged = True player.health -= 1 missile_hit_player = True # draw score if score > hard_mode: DISPLAYSURF.blit(message_to_screen("SCORE: {0}".format(score),None, 36, RED), (800-240, 0)) hard_mode_message = message_to_screen("<HARD MODE>", None, 36, RED) DISPLAYSURF.blit(hard_mode_message, (240, 0)) else: DISPLAYSURF.blit(message_to_screen("SCORE: {0}".format(score), None, 36, BLACK), (800-240, 0)) # draw high score if score < highscore_int: hi_score_message = message_to_screen("HIGH SCORE: {0}".format(highscore_int), None, 22, BLACK) else: highscore_file = open('highscore.dat', "w") highscore_file.write(str(score)) highscore_file.close() highscore_file = open('highscore.dat', "r") highscore_int = int(highscore_file.read()) highscore_file.close() hi_score_message = message_to_screen("HI-SCORE: {0}".format(highscore_int),None, 22, RED) hi_score_message_rect = hi_score_message.get_rect() DISPLAYSURF.blit(hi_score_message, (800-220, 50)) # draw health (maximum = 3) if player.health >= 1: DISPLAYSURF.blit(sprites.icon, (10 , 10)) if player.health >= 2: DISPLAYSURF.blit(sprites.icon, (52, 10)) if player.health >= 3: DISPLAYSURF.blit(sprites.icon, (94, 10)) # draw spikes at the buttom of the screen DISPLAYSURF.blit(sprites.spikes, (0, 500)) # score increase every second if score > hard_mode: score += 2 #default = 2 else: score += 1 #default = 1 # pygame systems pygame.display.update() pygame.display.flip() #smooth scrolling background pygame.display.set_caption("Helicopter shooter (" + str(int(FPSCLOCK.get_fps())) + " FPS)") FPSCLOCK.tick(FPS)