def create_alien(ai_settings, screen, aliens, alien_number, row_number, type): """Create an alien, and place it in the row.""" alien = Aliens(ai_settings, screen, alien_number, row_number, type) alien_width = alien.rect.width alien.x = 232 + 2 * alien_width * alien_number alien.rect.x = alien.x alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number aliens.add(alien)
def _create_alien(self, alien_number, row_number): alien = Aliens(self) alien_width, alien_height = alien.rect.size alien.x = alien_width + 2 * alien_width * alien_number alien.y = alien_height + 2 * alien_height * row_number alien.rect.x = alien.x alien.rect.y = alien.y self.aliens.add(alien)
def create_aliens(game_settings, screen, aliens, alien_number, row_number): """Create row and column of a fleet of aliens """ # Create an alien and place it in a row alien = Aliens(game_settings, screen) alien_x(alien, alien_number) alien_y(alien, row_number) aliens.add(alien)
def run_game(): pygame.init() ai_settings = Settings() FPS = ai_settings.fps fpsclock = pygame.time.Clock() score = Score() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Alien Invasion!') # instantiating the ship object and two missile groups ship = Ship(screen) missiles1_fired = Group() missiles2_fired = Group() pygame.time.set_timer(USEREVENT + 1, 5000) # blitting the first alien to the screen, # and adding it to the curaliens group newalien = Aliens(screen) Aliens.curaliens.add(newalien) for alien in Aliens.curaliens: alien.blitme() # main game loop while True: gf.check_events(ship, missiles1_fired, missiles2_fired, ai_settings, screen, Aliens.curaliens, score) gf.update_screen(ai_settings, screen, ship, missiles1_fired, missiles2_fired, Aliens.curaliens, score) fpsclock.tick(FPS)
def create_fleet(game_settings, screen, aliens, ship): """Create a fleet of aliens on the screen""" alien = Aliens(game_settings, screen) number_of_aliens_x = get_number_of_aliens(game_settings, alien.rect.width) number_of_rows = get_number_of_rows(game_settings, ship.rect.height, alien.rect.height) # Create the first row of aliens for row_number in range(number_of_rows): for alien_number in range(number_of_aliens_x): create_aliens(game_settings, screen, aliens, alien_number, row_number)
def _create_feelt(self): #create a number of aliens and add them to the fleet alien = Aliens(self) #available horizontal space alien_width, alien_height = alien.rect.size available_space_x = self.screen.get_rect().width - 2 * alien_width availebe_space_y = self.screen.get_rect( ).height - self.ship.rect.height - 3 * alien_height number_of_rows = availebe_space_y // (2 * alien_height) number_of_aliens = available_space_x // (2 * alien_width) for row_number in range(number_of_rows): for alien_number in range(number_of_aliens): self._create_alien(alien_number, row_number)
def reset(): nonlocal player_ship,aliens,game_over,started,seconds,seconds_text,start_time,second_text_rect,wave,wave_text,time_text,time_text_rect,time_passed wave = 1 wave_text = wave_font.render(f"WAVE: {wave}",True,WHITE) player_ship = pygame.sprite.GroupSingle(Spaceship(WIDTH,HEIGHT)) aliens = Aliens(WIDTH) enemy_ships.empty() items.empty() enemy_objects.empty() #for sprite in items: #sprite.kill() started = False game_over = False #seconds =3 seconds_text = font.render(texts[len(texts) - seconds],True,WHITE) second_text_rect =seconds_text.get_rect(center=(WIDTH//2,HEIGHT//2 + gap_from_center)) time_passed =0 time_text = second_font.render(str(time_passed),True,WHITE) time_text_rect = time_text.get_rect(center=(WIDTH//2,second_top_gap + time_text.get_height()//2)) start_time = time.time() start_sound.play()
from player import Player from aliens import Aliens from missile import Missile import aliens from win_lose import WIN_LOSE screen = Screen() screen.setup(width=600, height=600) screen.bgcolor("black") screen.tracer(0) player = Player() missile = Missile() win_lose = WIN_LOSE() aliens_first_row = Aliens.create_aliens(-250, 240, 15, 36) aliens_second_row = Aliens.create_aliens(-220, 200, 14, 33) aliens_third_row = Aliens.create_aliens(-250, 160, 15, 36) aliens_fourth_row = Aliens.create_aliens(-220, 120, 14, 33) screen.listen() screen.onkey(player.right, "Right") screen.onkey(player.left, "Left") game_is_on = True while game_is_on: screen.update() Aliens.move_cars() missile.move_missile() if missile.ycor() > 240:
def game(): high_score_file_name = "high_scores.txt" with open(high_score_file_name,'r') as f: high_scores = list(map(int,f.readlines())) def update_high_scores_if_necessary(score): if score > high_scores[-1]: high_scores.pop() high_scores.append(score) high_scores.sort(reverse=True) with open(high_score_file_name,'w') as f: for high_score in high_scores: f.write(str(high_score) + '\n') coin_image = pygame.image.load(os.path.join('assets','gold_coin_animation','gold_coin_round_blank_1.png')).convert_alpha() coin_size = 20 coin_image = pygame.transform.scale(coin_image,(coin_size,coin_size)) coin_font = pygame.font.Font(os.path.join('assets','atari.ttf'),20) def draw_coins_collected_topleft(): topleft = (5,5) screen.blit(coin_image,topleft) coin_text = coin_font.render(str(player_ship.sprite.coins_collected),True,WHITE) screen.blit(coin_text,(10 + coin_image.get_width(),5)) def reset(): nonlocal player_ship,aliens,game_over,started,seconds,seconds_text,start_time,second_text_rect,wave,wave_text,time_text,time_text_rect,time_passed wave = 1 wave_text = wave_font.render(f"WAVE: {wave}",True,WHITE) player_ship = pygame.sprite.GroupSingle(Spaceship(WIDTH,HEIGHT)) aliens = Aliens(WIDTH) enemy_ships.empty() items.empty() enemy_objects.empty() #for sprite in items: #sprite.kill() started = False game_over = False #seconds =3 seconds_text = font.render(texts[len(texts) - seconds],True,WHITE) second_text_rect =seconds_text.get_rect(center=(WIDTH//2,HEIGHT//2 + gap_from_center)) time_passed =0 time_text = second_font.render(str(time_passed),True,WHITE) time_text_rect = time_text.get_rect(center=(WIDTH//2,second_top_gap + time_text.get_height()//2)) start_time = time.time() start_sound.play() font = pygame.font.Font(os.path.join('assets','atari.ttf'),30) game_over_text = font.render("GAME OVER!",True,WHITE) gap_from_center = 50 game_over_text_rect = game_over_text.get_rect(center=(WIDTH//2,HEIGHT//2 + gap_from_center)) wave = 1 wave_font = pygame.font.Font(os.path.join('assets','atari.ttf'),20) wave_text = wave_font.render(f"WAVE: {wave}",True,WHITE) wave_text_rect= wave_text.get_rect(topright=(WIDTH-10,10)) item_types = [Heart,InvincibilityPotion,Cross,Star,TorpedoPowerUp,Safety,Skull,BombPowerUp,Snowflake,PoisonMuk,BombPowerUp,Strength,RedButton,'wildcard',Safety,InvincibilityPotion] buttons_gap_from_edge = 100 play_again_text = font.render("PLAY AGAIN",True,WHITE) play_again_surface = pygame.Surface((play_again_text.get_width() + 50,play_again_text.get_height() + 50),pygame.SRCALPHA) play_again_surface.fill(RED) play_again_surface.blit(play_again_text,(play_again_surface.get_width()//2 - play_again_text.get_width()//2,play_again_surface.get_width()//2 - play_again_text.get_width()//2)) play_again_surface_rect = play_again_surface.get_rect(center=(WIDTH//2,game_over_text_rect.bottom + 100)) menu_text = font.render("MENU",True,WHITE) menu_surface = pygame.Surface((menu_text.get_width() + 50,menu_text.get_height() + 50),pygame.SRCALPHA) menu_surface.fill(RED) menu_surface.blit(menu_text,(menu_surface.get_width()//2 - menu_text.get_width()//2,menu_surface.get_width()//2 - menu_text.get_width()//2)) menu_surface_rect = menu_surface.get_rect(center=(WIDTH//2,play_again_surface_rect.bottom + 100)) alpha_play_again_text_surface = pygame.Surface(play_again_text.get_size(),pygame.SRCALPHA) alpha_play_again_text_surface.fill((255,0,0,128)) pygame.mixer.music.load(os.path.join('assets','level1.ogg')) start_sound = pygame.mixer.Sound(os.path.join('assets','racestart.wav')) player_ship = pygame.sprite.GroupSingle(Spaceship(WIDTH,HEIGHT)) aliens = Aliens(WIDTH) explosions = pygame.sprite.Group() hearts = pygame.sprite.Group() potions = pygame.sprite.Group() items = pygame.sprite.Group() crosses = pygame.sprite.Group() ships = EnemySpaceShips() game_over = False topleft=(0,0) red_transparent = (255,0,0,128) started = False texts = ["READY!","SET!","GO!"] seconds = 3 seconds_text = font.render(texts[len(texts) - seconds],True,WHITE) second_text_rect =seconds_text.get_rect(center=(WIDTH//2,HEIGHT//2 + gap_from_center)) game_over_music = pygame.mixer.Sound(os.path.join('assets','Retro_No hope.ogg')) def display(wave): topleft = (0,0) wave_font = pygame.font.Font(os.path.join('assets','atari.ttf'),50) gap = 50 wave_text_1 = font.render("WAVE",True,WHITE) wave_text_2 = font.render(str(wave),True,WHITE) wave_text_1_rect = wave_text_1.get_rect(center=(WIDTH//2,HEIGHT//2)) wave_text_2_rect = wave_text_2.get_rect(center=(WIDTH//2,wave_text_1_rect.bottom + gap)) SECOND_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(SECOND_EVENT,1000) seconds = 3 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == SECOND_EVENT: seconds -= 1 if seconds == 0: pygame.time.set_timer(SECOND_EVENT,0) return explosions.update() screen.blit(background_image,topleft) screen.blit(wave_text_1,wave_text_1_rect) screen.blit(wave_text_2,wave_text_2_rect) pygame.display.update() clock.tick(FPS) second_font = pygame.font.SysFont(os.path.join('assets','atari.ttf'),30) HEART_EVENT = pygame.USEREVENT + 2 milliseconds =2000 #10000 pygame.time.set_timer(HEART_EVENT,2000) ENEMY_SHIP_EVENT = pygame.USEREVENT + 5 pygame.time.set_timer(ENEMY_SHIP_EVENT,15000) ENEMY_OBJECT_EVENT = pygame.USEREVENT + 7 pygame.time.set_timer(ENEMY_OBJECT_EVENT,7000) COIN_EVENT = pygame.USEREVENT + 10 milliseconds = 5000 pygame.time.set_timer(COIN_EVENT,milliseconds) enemy_objects = pygame.sprite.Group() display(wave) start_time = time.time() start_sound.play() time_passed = 0 time_text = second_font.render(str(time_passed),True,WHITE) second_top_gap = 10 time_text_rect = time_text.get_rect(center=(WIDTH//2,second_top_gap + time_text.get_height()//2)) enemy_ships = pygame.sprite.Group() enemy_ship_types = [RocketShip,EnemySpaceShip] while True: current_time = time.time() if not started: if current_time - start_time >= 1: seconds -= 1 if seconds == 0: started = True pygame.mixer.music.play(-1) seconds = 3 start_time = current_time else: seconds_text = font.render(texts[len(texts) - seconds],True,WHITE) second_text_rect =seconds_text.get_rect(center=(WIDTH//2,HEIGHT//2 + gap_from_center)) start_time = current_time elif not game_over: if current_time - start_time >= 1: time_passed += 1 time_text = second_font.render(str(time_passed),True,WHITE) time_text_rect = time_text.get_rect(center=(WIDTH//2,second_top_gap + time_text.get_height()//2)) start_time = current_time for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif game_over and event.type == pygame.MOUSEBUTTONDOWN: point = pygame.mouse.get_pos() if play_again_surface_rect.collidepoint(point): game_over_music.stop() reset() elif menu_surface_rect.collidepoint(point): game_over_music.stop() return elif started and not game_over: if event.type == HEART_EVENT: if random.randint(1,25) == 1: item = Cross(WIDTH,HEIGHT) items.add(item) else: number = random.randint(len(item_types) - 1,len(item_types) -1) class_ =item_types[number] hidden = False if class_ == 'wildcard': class_ = random.choice([Strength,RedButton,InvincibilityPotion,Safety,TorpedoPowerUp,BombPowerUp]) hidden= True item = class_(WIDTH,HEIGHT,hidden=hidden) items.add(item) elif event.type == ENEMY_SHIP_EVENT: ship_class= random.choice(enemy_ship_types) enemy = FlyingSaucer(WIDTH,HEIGHT,speed=3) enemy_ships.add(enemy) elif event.type == ENEMY_OBJECT_EVENT: meteor = BlackHole(WIDTH,HEIGHT) #enemy_ships.add(meteor) enemy_objects.add(meteor) elif event.type == COIN_EVENT: coin = Coin(WIDTH,HEIGHT) items.add(coin) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: if player_ship.sprite.has_torpedo: player_ship.sprite.fire_torpedo() elif player_ship.sprite.has_bomb: player_ship.sprite.fire_bomb() elif player_ship.sprite.poisoned and player_ship.sprite.coins_collected >= 5 and event.key == pygame.K_j: player_ship.sprite.unpoison() player_ship.sprite.removeCoins(5) elif event.type == pygame.KEYUP and event.key == pygame.K_RETURN: player_ship.sprite.release_bomb() if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: start_sound.stop() game_over_music.stop() return explosions.update() if started and not game_over: items.update() #hearts.update() pressed_keys = pygame.key.get_pressed() aliens.update() ships.update() enemy_objects.update() enemy_ships.update() game_over = player_ship.sprite.update(pressed_keys,aliens.get_group(),aliens.get_bullets(),explosions,hearts,potions,crosses,items,enemy_ships,enemy_objects) if game_over: pygame.mixer.music.stop() game_over_music.play() update_high_scores_if_necessary(wave -1) if aliens.is_empty(): wave += 1 update_high_scores_if_necessary(wave -1) wave_text = wave_font.render(f"WAVE: {wave}",True,WHITE) player_ship.sprite.restore_health_and_remove_bullets() #for sprite in items: #sprite.kill() aliens.reset() hearts.empty() items.empty() potions.empty() started = False time_passed =0 time_text = second_font.render(str(time_passed),True,WHITE) time_text_rect = time_text.get_rect(center=(WIDTH//2,second_top_gap + time_text.get_height()//2)) display(wave) start_sound.play() seconds_text = font.render(texts[len(texts) - seconds],True,WHITE) start_time = time.time() screen.blit(background_image,topleft) for enemy_object in enemy_objects: enemy_object.draw(screen) for enemy_ship in enemy_ships: enemy_ship.draw(screen) items.draw(screen) #hearts.draw(screen) aliens.draw(screen) if player_ship: player_ship.sprite.draw(screen) draw_coins_collected_topleft() player_ship.sprite.draw_health_bar_and_bullets(screen) explosions.draw(screen) screen.blit(wave_text,wave_text_rect) screen.blit(time_text,time_text_rect) if not started: screen.blit(seconds_text,second_text_rect) elif game_over: point = pygame.mouse.get_pos() if play_again_surface_rect.collidepoint(point): play_again_surface.fill(red_transparent) play_again_surface.blit(play_again_text,(play_again_surface.get_width()//2 - play_again_text.get_width()//2,play_again_surface.get_width()//2 - play_again_text.get_width()//2)) else: play_again_surface.fill(RED) play_again_surface.blit(play_again_text,(play_again_surface.get_width()//2 - play_again_text.get_width()//2,play_again_surface.get_width()//2 - play_again_text.get_width()//2)) if menu_surface_rect.collidepoint(point): menu_surface.fill(red_transparent) menu_surface.blit(menu_text,(menu_surface.get_width()//2 - menu_text.get_width()//2,menu_surface.get_width()//2 - menu_text.get_width()//2)) else: menu_surface.fill(RED) menu_surface.blit(menu_text,(menu_surface.get_width()//2 - menu_text.get_width()//2,menu_surface.get_width()//2 - menu_text.get_width()//2)) screen.blit(game_over_text,game_over_text_rect) screen.blit(play_again_surface,play_again_surface_rect) screen.blit(menu_surface,menu_surface_rect) pygame.display.update() clock.tick(FPS)