def run_game(): # Initialize pygame, settings, and screen object pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Inveders") clock = pygame.time.Clock() # make a ship ship = Ship(ai_settings, screen) ufo = UFO(ai_settings, screen) play_button = Button(screen, "Play", 200) score_button = Button(screen, "High Scores", 300) screen_rect = screen.get_rect() stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) bullets = Group() lasers = Group() aliens = Group() explosions = Group() bunkers = [ Bunker(screen_rect.width / 4, screen_rect.bottom - 150, screen, 1), Bunker(screen_rect.width / 2, screen_rect.bottom - 150, screen, 2), Bunker(3 * screen_rect.width / 4, screen_rect.bottom - 150, screen, 3), Bunker(screen_rect.width, screen_rect.bottom - 150, screen, 4) ] # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, score_button, ship, aliens, bullets) if stats.game_active: pygame.mixer.music.load('sounds/bgm.wav') pygame.mixer.music.set_volume(400) pygame.mixer.music.play(-1) ship.update() gf.update_ufo(ufo, ai_settings) gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets, lasers, ufo, explosions, bunkers) gf.update_aliens(ai_settings, stats, screen, sb, ship, ufo, aliens, bullets, lasers) gf.update_explosions(explosions) gf.update_screen(ai_settings, stats, screen, sb, ship, ufo, aliens, bullets, lasers, play_button, score_button, explosions, bunkers) clock.tick(60)
def create_bunker(screen, bunker): for x in range(3): bunk_foot_left = Bunker( screen, ['images/bottom_left.png', 'images/bottom_left_break.png'], 720, 140 + (x * 400)) bunk_left = Bunker(screen, ['images/left.png', 'images/left_break.png'], 680, 140 + (x * 400)) bunk_up_left = Bunker( screen, ['images/top_left.png', 'images/top_left_break.png'], 640, 140 + (x * 400)) bunk_up = Bunker(screen, ['images/top.png', 'images/top_break.png'], 640, 180 + (x * 400)) bunk_down = Bunker(screen, ['images/bottom.png', 'images/bottom_break.png'], 680, 180 + (x * 400)) bunk_up_right = Bunker( screen, ['images/top_right.png', 'images/top_right_break.png'], 640, 220 + (x * 400)) bunk_right = Bunker(screen, ['images/right.png', 'images/right_break.png'], 680, 220 + (x * 400)) bunk_foot_right = Bunker( screen, ['images/bottom_right.png', 'images/bottom_right_break.png'], 720, 220 + (x * 400)) bunker.add(bunk_foot_left) bunker.add(bunk_left) bunker.add(bunk_up_left) bunker.add(bunk_up) bunker.add(bunk_down) bunker.add(bunk_up_right) bunker.add(bunk_right) bunker.add(bunk_foot_right)
def create_bunker(ai_settings, screen, bunkers, bunker_number): bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width bunker.x = bunker_width + 2 * bunker_width * bunker_number bunker.rect.x = bunker.x bunker.rect.y = bunker.rect.height + 7 * bunker.rect.height bunkers.add(bunker)
def create_bunkers(ai_settings, screen, bunkers): width = 200 for i in range(5): new_bunk = Bunker(ai_settings, screen) new_bunk.rect.centerx = 200 + i * width bunkers.add(new_bunk) print('called')
def create_bunker(ai_settings, screen, bunkers, num_bunker): bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width bunker.x = bunker_width + 20 bunker.rect.x = bunker.x + 2 * bunker_width * num_bunker - 50 bunker.rect.y = bunker.rect.height + 2 * bunker.rect.height + 360 bunkers.add(bunker)
def create_bunkers(ai_settings, screen, ship, bunkers): bunker = Bunker(ai_settings, screen) number_bunkers_x = 4 number_rows = 1 for row_number in range(number_rows): for bunker_number in range(number_bunkers_x): disp_bunker(ai_settings, screen, bunkers, bunker_number, row_number)
def setup(): global aliens, bombs, bullets, bunkers, hits, shots, ufos aliens = [] a1 = TieFighter(230, 90) a2 = TieFighter(330, 90) a3 = TieFighter(430, 90) a4 = TieFighter(530, 90) a5 = TieFighter(630, 90) a6 = TieFighter(730, 90) a7 = Bug(230, 160) a8 = Bug(330, 160) a9 = Bug(430, 160) a10 = Bug(530, 160) a11 = Bug(630, 160) a12 = Bug(730, 160) a13 = Scrubby(230, 240) a14 = Scrubby(330, 240) a15 = Scrubby(430, 240) a16 = Scrubby(530, 240) a17 = Scrubby(630, 240) a18 = Scrubby(730, 240) tie_fighters = [a1, a2, a3, a4, a5, a6] bugs = [a7, a8, a9, a10, a11, a12] scrubbies = [a13, a14, a15, a16, a17, a18] aliens = tie_fighters + bugs + scrubbies for a in aliens: a.v_x = alien_speed ufo_x = random.randrange(3000, 10000) ufos = [UFO(ufo_x, 60)] b1 = Bunker(200, 400) b2 = Bunker(460, 400) b3 = Bunker(720, 400) bunkers = [b1, b2, b3] bombs = [] bullets = [] hits = 0 shots = 0
def create_bunker(ai_settings, screen, ship, bunkers, bunker_num): """Create a bunker and place it in the row.""" bunker = Bunker(ai_settings, screen, ship) bunker_width = bunker.rect.width x = bunker_width + 1.8 * bunker_width * bunker_num # Set its position bunker.set_width(x) bunkers.add(bunker)
def disp_bunker(ai_settings, screen, bunkers, bunker_number, row_number): """Create an alien, and place it in the row.""" bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width bunker.x = bunker_width + 2 * bunker_width * bunker_number bunker.rect.x = bunker.x - 40 bunker.rect.y = 475 bunkers.add(bunker)
def create_bunker(ai_settings, screen, bunkers, bunker_number): bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width + 20 bunker.x = bunker_width + 2.4 * bunker_width * bunker_number bunker.y = 500 bunker.rect.x = bunker.x bunker.rect.y = bunker.y bunkers.add(bunker)
def create_bunker(ai_settings, screen, bunkers, bunker_number): """Create a bunker and place it in the row""" bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width bunker.x = bunker_width + 3.25 * bunker_width * bunker_number bunker.rect.x = bunker.x / 2 bunker.rect.y = bunker.rect.height * 4 bunkers.add(bunker)
def _create_bunker(self, bunker_number): """Create an bunker and place it in the row.""" bunker = Bunker(self) bunker_width, bunker_height = bunker.rect.size bunker.x = (bunker_width // 2) + 2 * bunker_width * bunker_number bunker.rect.x = bunker.x bunker.rect.y = int(self.settings.screen_height * 0.80) self.bunkers.add(bunker)
def create_bunkers(settings, screen, bunkers): screen_rect = screen.get_rect() x, y = 150, screen_rect.height - 100 for bunker_num in range(4): bunker = Bunker(settings, screen, x, y) bunkers.add(bunker) x += 300
def create_bunker_7(ai_settings, screen, bunkers, alien_number1, row_number): """Create an alien, and place it in the row.""" bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width bunker.x = 175 + 15 * bunker_width * alien_number1 bunker.rect.x = bunker.x bunker.rect.y = 550 + 1 * bunker.rect.height * row_number bunker.add(bunkers)
def create_bunker_set(ai_settings, screen, bunkers): """Create a series of bunkers""" bunker = Bunker(ai_settings, screen) number_bunker_x = get_number_bunkers_x(ai_settings, bunker.rect.width) # Create a set of bunkers for bunker_number in range(number_bunker_x): create_bunker(ai_settings, screen, bunkers, bunker_number)
def create_bunker(ai_settings, screen, bunkers, number_bunker_x): """Create a bunker and place it in the row""" bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width - 70 # width in between the collumns bunker.x = bunker_width + 20 bunker.rect.x = bunker.x + 2 * bunker_width * number_bunker_x - 50 bunker.rect.y = bunker.rect.height + 2 * bunker.rect.height + 360 bunkers.add(bunker)
def create_bunker_rows(ai_settings, screen, bunkers): bunker = Bunker(ai_settings, screen) number_bunkers_x = get_number_aliens_x(ai_settings, bunker.rect.width) number_rows = 1 for row_number in range(number_rows): for bunker_number in range(number_bunkers_x): create_bunker(ai_settings, screen, bunkers, bunker_number)
def create_bunker(ai_settings, screen, bunkers, bunker_number, row_number): bunker = Bunker(ai_settings, screen) bunker_width = bunker.rect.width bunker.x = bunker_width + 4 * bunker_width * bunker_number bunker.rect.x = bunker.x bunker.rect.y = 450 # bunker.rect.y = bunker.rect.height + 4 * bunker.rect.height * row_number * 0.5 bunker.lives = 0 bunkers.add(bunker)
def setup_bunkers(screen, ship, bunkers): """Create 4 bunkers on the field""" screen_rect = screen.get_rect() partition = screen_rect.width / 5 for bunker_num in range(4): bunker = Bunker(screen, ship) bunker.rect.centerx = partition * (1 + bunker_num) bunkers.add(bunker)
def create_bunkers(ai_settings, screen, bunkers): image = pygame.image.load('images/Bunker.png') for i in range(1, 10, 2): bunker = Bunker(ai_settings, screen, image) bunker_width = bunker.rect.width bunker.x = bunker_width bunker.rect.x = bunker.x * i bunker.rect.y = 600 bunkers.add(bunker)
def create_bunkers(self): space = int( (self.director.screen.get_rect().right - ((self.number_of_bunkers - 1) * 56)) / self.number_of_bunkers) x = space for i in range(self.number_of_bunkers): self.bunkers.add(Bunker(self.director.screen, x)) x += space
def add_bunkers(screen, ai_settings, bunkers, ship): width = ai_settings.screen_width pixels_between_bunkers = width / 5 # Divided by 5 to fit 4 bunkers with space left+right of each bunker_x = 0 bunker_y = ship.rect.top - 15 for i in range(4): new_bunker = Bunker(screen) bunker_x += pixels_between_bunkers new_bunker.rect.centerx = bunker_x new_bunker.rect.bottom = bunker_y bunkers.add(new_bunker)
def _create_bunker_wall(self): """Create the row of bunkers.""" # Create an bunker and find the number of bunkers in a row. # Spacing between each bunker is equal to one alien width. bunker = Bunker(self) bunker_width, bunker_height = bunker.rect.size available_space_x = self.settings.screen_width number_bunkers_x = (available_space_x // bunker_width) // 2 # Create the full wall of bunkers. for bunker_number in range(number_bunkers_x): self._create_bunker(bunker_number)
def make_bunker(ai_settings, screen, position): bunker = pygame.sprite.Group() for row in range(ai_settings.bunker_rows): for col in range(ai_settings.bunker_cols): if not ((row > 3 and (1 < col < ai_settings.bunker_cols - 2)) or (row > 2 and (2 < col < ai_settings.bunker_cols - 3)) or (row == 0 and (col < 1 or col > ai_settings.bunker_cols - 2))): block = Bunker(ai_settings, screen, row, col) block.rect.x = int(ai_settings.screen_width * 0.15) + ( 250 * position) + (col * block.width) block.rect.y = int( ai_settings.screen_height * 0.8) + (row * block.height) bunker.add(block) return bunker
def check_play_button(game_settings, screen, stats, scoreboard, play_button, ship, aliens, bullets, enemy_bullets, bunkers, mouse_x, mouse_y): button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y) if button_clicked and stats.game_status != 2: # Reset game settings game_settings.initialize_dynamic_settings() # Hide mouse cursor pygame.mouse.set_visible(False) # Reset game stats stats.reset_stats() stats.game_status = 2 # Reset scoreboard scoreboard.prep_score() scoreboard.prep_high_score() scoreboard.prep_level() scoreboard.prep_ships() # Clear aliens, bullets lists aliens.empty() game_settings.boss_number = 0 bullets.empty() enemy_bullets.empty() # Create aliens fleet and center the ship create_fleet(game_settings, screen, aliens) # Create bunkers bunkers.clear() bunkers.append(Bunker(screen, 150, 480)) bunkers.append(Bunker(screen, 400, 480)) bunkers.append(Bunker(screen, 650, 480)) ship.reset()
def create_bunker(ai_settings,screen,bunkers,bunkerName,i): """Create a bunker and place it in the row""" img = pygame.image.load("resources/images/bunker"+str(i)+".gif") bunkerName = 'bunker'+str(i)+'.gif' bunker = Bunker(ai_settings,screen,img,bunkerName) if i == 0: bunker_width = bunker.rect.width bunker_height = bunker.rect.height bunker.rect.x = 250 bunker.rect.y = 600 elif i == 1: bunker_width = bunker.rect.width bunker_height = bunker.rect.height bunker.rect.x = 550 bunker.rect.y = 600 elif i ==2: bunker_width = bunker.rect.width bunker_height = bunker.rect.height bunker.rect.x = 850 bunker.rect.y = 600 bunkers.add(bunker)
def check_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, a_bullets, mouse_x, mouse_y, background, bunker): """Start a new game when player clicks play""" button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y) if button_clicked and not stats.game_active and not stats.check_hs: # restart game settings ai_settings.initialize_dynamic_settings() # Hide mouse pygame.mouse.set_visible(False) # restart game stats stats.reset_stats() stats.game_active = True # reset scoreboard sb.prep_score() sb.prep_high_score() sb.prep_level() sb.prep_ships() # Empty group sprites aliens.empty() bullets.empty() a_bullets.empty() bunker.empty() # create a new fleet and center ship create_fleet(ai_settings, screen, ship, aliens) ship.center_ship() # Crete bunkers for letter in ai_settings.potato: bunker.add(Bunker(ai_settings, screen, letter)) # set background screen background.switch_to(1)
def create_bunker(ai_settings, screen, bunkers, x): bunker = Bunker(ai_settings, screen) bunker.rect.centerx = x bunker.add(bunkers)
def create_bunkers(bunkers, screen): for i in range(4): bunker = Bunker(screen) bunker.rect.x = 175 + (i * 250) bunker.rect.y = 600 bunkers.add(bunker)
def create_bunker(screen, position, bunker): bunk_foot_left = None bunk_left = None bunk_up_left = None bunk_up = None bunk_down = None bunk_up_right = None bunk_right = None bunk_foot_right = None if position == 1: bunk_foot_left = Bunker( screen, ['images/bunk_foot.bmp', 'images/bunk_foot_dmg.bmp'], 720, 140) bunk_left = Bunker( screen, ['images/bunk_left.bmp', 'images/bunk_left_dmg.bmp'], 680, 140) bunk_up_left = Bunker( screen, ['images/bunk_up_left.bmp', 'images/bunk_up_left_dmg.bmp'], 640, 140) bunk_up = Bunker(screen, ['images/bunk_up.bmp', 'images/bunk_up_dmg.bmp'], 640, 180) bunk_down = Bunker( screen, ['images/bunk_down.bmp', 'images/bunk_down_dmg.bmp'], 680, 180) bunk_up_right = Bunker( screen, ['images/bunk_up_right.bmp', 'images/bunk_up_right_dmg.bmp'], 640, 220) bunk_right = Bunker( screen, ['images/bunk_right.bmp', 'images/bunk_right_dmg.bmp'], 680, 220) bunk_foot_right = Bunker( screen, ['images/bunk_foot.bmp', 'images/bunk_foot_dmg.bmp'], 720, 220) elif position == 2: bunk_foot_left = Bunker( screen, ['images/bunk_foot.bmp', 'images/bunk_foot_dmg.bmp'], 720, 540) bunk_left = Bunker( screen, ['images/bunk_left.bmp', 'images/bunk_left_dmg.bmp'], 680, 540) bunk_up_left = Bunker( screen, ['images/bunk_up_left.bmp', 'images/bunk_up_left_dmg.bmp'], 640, 540) bunk_up = Bunker(screen, ['images/bunk_up.bmp', 'images/bunk_up_dmg.bmp'], 640, 580) bunk_down = Bunker( screen, ['images/bunk_down.bmp', 'images/bunk_down_dmg.bmp'], 680, 580) bunk_up_right = Bunker( screen, ['images/bunk_up_right.bmp', 'images/bunk_up_right_dmg.bmp'], 640, 620) bunk_right = Bunker( screen, ['images/bunk_right.bmp', 'images/bunk_right_dmg.bmp'], 680, 620) bunk_foot_right = Bunker( screen, ['images/bunk_foot.bmp', 'images/bunk_foot_dmg.bmp'], 720, 620) elif position == 3: bunk_foot_left = Bunker( screen, ['images/bunk_foot.bmp', 'images/bunk_foot_dmg.bmp'], 720, 940) bunk_left = Bunker( screen, ['images/bunk_left.bmp', 'images/bunk_left_dmg.bmp'], 680, 940) bunk_up_left = Bunker( screen, ['images/bunk_up_left.bmp', 'images/bunk_up_left_dmg.bmp'], 640, 940) bunk_up = Bunker(screen, ['images/bunk_up.bmp', 'images/bunk_up_dmg.bmp'], 640, 980) bunk_down = Bunker( screen, ['images/bunk_down.bmp', 'images/bunk_down_dmg.bmp'], 680, 980) bunk_up_right = Bunker( screen, ['images/bunk_up_right.bmp', 'images/bunk_up_right_dmg.bmp'], 640, 1020) bunk_right = Bunker( screen, ['images/bunk_right.bmp', 'images/bunk_right_dmg.bmp'], 680, 1020) bunk_foot_right = Bunker( screen, ['images/bunk_foot.bmp', 'images/bunk_foot_dmg.bmp'], 720, 1020) bunker.add(bunk_foot_left) bunker.add(bunk_left) bunker.add(bunk_up_left) bunker.add(bunk_up) bunker.add(bunk_down) bunker.add(bunk_up_right) bunker.add(bunk_right) bunker.add(bunk_foot_right)