def create_hearts(num_of_hearts): global hearts reset_hearts() for i in range(num_of_hearts): heart = Object("images/heart.png", STARTINGXPOS - XDISTACEBETWEENHEARTS * i, STARTINGYPOS) hearts.append(heart)
def create_powerup(): random_powerup_index = random.randint(0, len(powerup_types) - 1) random_powerup_type = powerup_types[random_powerup_index] random_x = random.random() * WINDOWSWIDTH - 64 if random_powerup_type == "heart": heart = Object("images/heart.png", x=random_x, y=0, scale=1.5) powerups.append(heart) engine.on_collision(heart, player, heart_player_colllision) elif random_powerup_type == "ammo": ammo = Object("images/ammo.png", x=random_x, y=0, scale=1.5) powerups.append(ammo) engine.on_collision(obj_1=ammo, obj_2=player, handler=ammo_player_collision) elif random_powerup_type == "speed": speed = Object("images/speed.png", x=random_x, y=0, scale=1.5) powerups.append(speed) engine.on_collision(speed, player, speed_player_collision) create_powerup_timer()
def fire_bullet(): global can_shoot if can_shoot or ammo_collected: bullet = Object("Images/bullet.png", player.pos_x + 16, player.pos_y - 32, scale=SCALEFACTOR) bullets.append(bullet) laser_sound = Audio("audio/laser.wav", False, True, 0.15) for enemy in enemies: engine.on_collision(bullet, enemy, bullet_enemy_collision) can_shoot = False engine.add_timer(0.5, set_can_shoot_to_true)
def spawn_enemies(): columns_of_enemies = (int(COLLUMNS * columns_of_enemies_multiplier)) if columns_of_enemies > MAXCOLUMNS_O_ENEMIES: columns_of_enemies = MAXCOLUMNS_O_ENEMIES rows_of_enemies = (int(ROWS * rows_of_enemies_multiplier)) if rows_of_enemies > MAXROWS_O_ENEMIES: rows_of_enemies = MAXROWS_O_ENEMIES for row in range(rows_of_enemies): for column in range(columns_of_enemies): enemy = Object("images/ufo.png", FIRSTENEMYXPOS + column * XDISTANCEBETWEENENEMIES, FIRSTENEMYYPOS + row * YDISTANCEBETWEENENEMIES, scale=SCALEFACTOR) enemies.append(enemy) set_timer_to_drop_bomb(enemy) engine.on_collision(player, enemy, player_enemy_collision)
def start_game(): global player, score_text, username space_invaders_text.destroy() play_button.destroy() username = username_input_box.text username_input_box.destroy() engine.set_background("images/background.png") background_music = Audio("audio/background.wav", True, True, 0.15) player = Object("images/spaceship.png", 400-32, 450, 0.5) create_hearts(STARTINGNUMOFHEARTS) score_text = Text("Score: " + str(score)) engine.on_key_pressed(pygame.K_SPACE, fire_bullet) engine.on_key_down(pygame.K_a, move_left) engine.on_key_down(pygame.K_d, move_right) create_powerup_timer() spawn_enemies()
def drop_bomb(enemy): if not enemy.destroyed: bomb = Object("images/bomb.png", enemy.pos_x, enemy.pos_y) bombs.append(bomb) engine.on_collision(bomb, player, bomb_player_collision) set_timer_to_drop_bomb(enemy)
def set_background(self, background_path): if self.background is not None: self.background.destroy() self.background = Object(background_path, 0, 0, 1.0, 0.0, False, None, 100)