def shoot(self, mouse_pos, mc_pos, settings):
        self.ammo -= 1
        self.cooldown = self.max_cooldown

        for i in range(self.shots):
            spread = random.random() * self.spread * random.choice([-1, 1])
            v = [mouse_pos[0] - mc_pos[0], mouse_pos[1] - mc_pos[1]]
            mv = math.sqrt(v[0]**2 + v[1]**2)
            uv = [(v[0] / mv), (v[1] / mv)]
            new_bullet = entities.Bullet(self.damage, self.speed, mc_pos, uv,
                                         spread)
            settings.game_sprites.add(new_bullet)
            settings.bullet_sprites.add(new_bullet)
Exemplo n.º 2
0
def enemyShoot():
    # Picks the enemies that are in the last row of the matriz
    enemyFront = [None] * nColumns
    for i in range(nRows):
        for j in range(nColumns):
            enemy = enemies[i][j]
            if enemy.life > 0:
                enemyFront[j] = enemy
    
    # Checks if there were any empty columns, if so, these ones
    # are removed from the enemy front
    cleanEnemyFront = []
    for enemy in enemyFront:
        if enemy != None:
            cleanEnemyFront.append(enemy)

    # Checks if there are still enemies alive and makes a random enemy of
    # the enemy front shoot
    if len(cleanEnemyFront) > 0:
        shootingEnemy = random.randint(0, len(cleanEnemyFront) - 1)
        bullets.append(entities.Bullet(cleanEnemyFront[shootingEnemy], DOWN, ENEMY))
        enemyShooting.play()
    else:
        gameOver()
Exemplo n.º 3
0
    def update(self, resolution, fps, dt, timestep, time):
        # move player
        self.player.update_position(dt, limits=resolution)

        # fire bullet
        if self.player.firing is True:
            self.bullets.append(
                entities.Bullet(position=self.player.position +
                                Vector(21, -64),
                                damage=self.player.damage))
            self.player.firing = False
        if self.player.last_time_fire is not None:
            self.player.last_time_fire += dt

        # move bullets
        for bullet in self.bullets:
            bullet.update_position(dt)

        # check bullet collision
        for bullet in self.bullets:
            if bullet.position.y <= -bullet.hieght:
                self.bullets.remove(bullet)
            for enemy in self.enemy_grid.enemies:
                if collision(bullet, enemy):
                    try:
                        self.bullets.remove(bullet)
                    except ValueError:
                        pass
                    enemy.hit(self, bullet)

        # move enemies
        if int(timestep * self.enemy_grid.move_rate) % fps == 0:
            self.enemy_grid.update_position(dt, limits=resolution)

        # remove dead enemies
        for enemy in self.enemy_grid.enemies:
            if enemy.health <= 0:
                self.enemy_grid.enemies.remove(enemy)

        # fire bombs
        for enemy in self.enemy_grid.enemies:
            if int(timestep * enemy.fire_rate) % fps == 0:
                if random.random() < enemy.fire_chance:
                    self.bombs.append(
                        entities.Bomb(position=enemy.position + Vector(35, 40),
                                      damage=enemy.damage,
                                      color=enemy.color))

        # move bombs
        for bomb in self.bombs:
            bomb.update_position(dt)

        # check bomb collision
        for bomb in self.bombs:
            if bomb.position.y > resolution[1] + bomb.hieght:
                self.bombs.remove(bomb)
            if collision(bomb, self.player):
                self.player.hit(self, bomb)
                self.bombs.remove(bomb)

        # check player-enemy collision
        for enemy in self.enemy_grid.enemies:
            if collision(enemy, self.player):
                self.player.health -= enemy.damage * 3
                self.enemy_grid.enemies.remove(enemy)
Exemplo n.º 4
0
def run_level(LEVELNAME):

    # Map
    CORE_grid.loadLevel(LEVELNAME)
    # frame count
    frame = 0
    # Bullets
    bullets_collector = []
    bullets_collector_ennemies = []
    ennemies_collector = []
    missiles_collector = []
    bullet_time_regen_coeff = 1

    if LEVELNAME == "level1":
        # Sounds
        pg.mixer.music.load('../assets/music/level1.mp3')
        pg.mixer.music.play(-1)
        # Ennemies
        ennemies_collector.append(
            entities.Ennemy(100, 40, 2, 1, BLOCKSIZE * 5, 380, [-80, 80]))
        ennemies_collector.append(
            entities.Ennemy(100, 40, 2, 2, BLOCKSIZE * 14, 620, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(100, 40, 2, 2, 2050, 380, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(200, 40, 2, 2, 4150, 260, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(200, 40, 2, 1, 6800, 380, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(200, 50, 2, 1, 7450, 625, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(200, 50, 2, 1, 8350, 260, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(300, 50, 2, 2, 9050, 505, [-100, 100]))
        ennemies_collector.append(
            entities.Ennemy(300, 60, 2, 1, 12150, 625, [-100, 100]))

        # Level 1 - Loading
        intro_level1 = pg.transform.scale(
            pg.image.load("../assets/intro_level1.png"),
            (SCREEN_WIDTH, SCREEN_HEIGHT))
        brighten = 255 / 30
        screen.blit(intro_level1, (0, 0))
        pg.display.update()
        time.sleep(1)
        for i in range(30):
            intro_level1.fill((brighten, brighten, brighten),
                              special_flags=pg.BLEND_RGB_ADD)
            screen.blit(intro_level1, (0, 0))
            pg.display.update()
            time.sleep(0.2 / 30)

    elif LEVELNAME == "level1_boss":
        # Sounds
        pg.mixer.music.load('../assets/music/level1.mp3')
        pg.mixer.music.play(-1)
        # Ennemies
        ennemies_collector.append(entities.Boss(1000, 30, "hillary", 1000,
                                                200))
        bullet_time_regen_coeff = 4
        # Missiles
        for i in range(1, 20):
            missiles_collector.append(entities.Missile(300, BLOCKSIZE,
                                                       i * 100))
            missiles_collector.append(entities.Missile(400, BLOCKSIZE,
                                                       i * 175))
            missiles_collector.append(entities.Missile(800, BLOCKSIZE,
                                                       i * 220))
            missiles_collector.append(entities.Missile(400, BLOCKSIZE,
                                                       i * 330))
            missiles_collector.append(entities.Missile(300, BLOCKSIZE,
                                                       i * 250))
            missiles_collector.append(
                entities.Missile(1000, BLOCKSIZE, i * 300))
            missiles_collector.append(entities.Missile(500, BLOCKSIZE,
                                                       i * 360))

        # Level 1 boss - Loading
        intro_level1 = pg.transform.scale(
            pg.image.load("../assets/intro_level1_boss.png"),
            (SCREEN_WIDTH, SCREEN_HEIGHT))
        brighten = 255 / 30
        screen.blit(intro_level1, (0, 0))
        pg.display.update()
        time.sleep(1)
        for i in range(30):
            intro_level1.fill((brighten, brighten, brighten),
                              special_flags=pg.BLEND_RGB_ADD)
            screen.blit(intro_level1, (0, 0))
            pg.display.update()
            time.sleep(0.2 / 30)

    elif LEVELNAME == "level2":
        # Sounds
        pg.mixer.music.load('../assets/music/level1.mp3')
        pg.mixer.music.play(-1)
        # Ennemies
        ennemies_collector.append(
            entities.ShootingEnnemy(35, 40, 2, 3, 860, 500, 0))
        ennemies_collector.append(
            entities.ShootingEnnemy(35, 40, 2, 3, 1700, 380, 0))
        ennemies_collector.append(
            entities.ShootingEnnemy(35, 40, 2, 3, 2350, 500, 0))
        ennemies_collector.append(
            entities.Ennemy(300, 60, 2, 4, 2650, 260, [-100, 100]))
        ennemies_collector.append(
            entities.ShootingEnnemy(35, 40, 2, 3, 3100, 500, 0))
        ennemies_collector.append(
            entities.ShootingEnnemy(35, 40, 2, 3, 4850, 500, 0))
        ennemies_collector.append(
            entities.ShootingEnnemy(35, 40, 2, 3, 5600, 500, 0))
        ennemies_collector.append(
            entities.Ennemy(300, 60, 2, 4, 6050, 620, [-100, 100]))
        pos0 = 400
        pos1 = 7000
        for i in range(1, 20):
            missiles_collector.append(
                entities.Missile(random.randint(pos0, pos1), BLOCKSIZE,
                                 i * 100))
            missiles_collector.append(
                entities.Missile(random.randint(pos0, pos1), BLOCKSIZE,
                                 i * 300))
            missiles_collector.append(
                entities.Missile(random.randint(pos0, pos1), BLOCKSIZE,
                                 i * 500))

        # Level 2 - Loading
        intro_level1 = pg.transform.scale(
            pg.image.load("../assets/intro_level2.png"),
            (SCREEN_WIDTH, SCREEN_HEIGHT))
        brighten = 255 / 30
        screen.blit(intro_level1, (0, 0))
        pg.display.update()
        time.sleep(1)
        for i in range(30):
            intro_level1.fill((brighten, brighten, brighten),
                              special_flags=pg.BLEND_RGB_ADD)
            screen.blit(intro_level1, (0, 0))
            pg.display.update()
            time.sleep(0.2 / 30)

    elif LEVELNAME == "final":
        # Sounds
        pg.mixer.music.load('../assets/music/level1.mp3')
        pg.mixer.music.play(-1)
        # Ennemies
        bullet_time_regen_coeff = 40
        ennemies_collector.append(
            entities.Biden(1500, 5, 10000, "biden", 1500, 300))

        # Level Final - Loading
        intro_level1 = pg.transform.scale(
            pg.image.load("../assets/intro_final.png"),
            (SCREEN_WIDTH, SCREEN_HEIGHT))
        brighten = 255 / 30
        screen.blit(intro_level1, (0, 0))
        pg.display.update()
        time.sleep(1)
        for i in range(30):
            intro_level1.fill((brighten, brighten, brighten),
                              special_flags=pg.BLEND_RGB_ADD)
            screen.blit(intro_level1, (0, 0))
            pg.display.update()
            time.sleep(0.2 / 30)

    elif LEVELNAME == "ending":
        # Sounds
        pg.mixer.music.load('../assets/music/sad.mp3')
        pg.mixer.music.play(-1)
        pg.mixer.music.set_volume(0.6)

        screen.blit(pg.image.load("../assets/ending.png"), (0, 0))
        pg.display.update()
        return "success"

    dirPlayer = 1
    sprites_collecor = pg.sprite.Group()
    player = entities.Player(screen)
    lastPlayerHealth = player.health
    updateMap = True
    damageTimer = 0
    slowDownPower = False
    isPause = False

    running = True
    level_result = "failed"
    while running:

        sprites_collecor.empty()

        # Update level
        if updateMap:
            playerBlockX = int(player.posX // BLOCKSIZE)
            playerBlockY = player.rect.y // BLOCKSIZE
            player.playerBlock = playerBlockX
            player.playerH = playerBlockY
            minBlockIndex = playerBlockX - 16
            if minBlockIndex < 0:
                minBlockIndex = 0
            minX, maxX = minBlockIndex, playerBlockX + 16
            minX_aroundPlayer, maxX_aroundPlayer = playerBlockX - 15, playerBlockX + 15
            mapBlocks = painter.drawMap(pg, CORE_grid.map, [minX, maxX],
                                        BLOCKSIZE, player)

            if mapBlocks == "LEVEL_END":
                if LEVELNAME in ["level1_boss", "final"]:
                    if len(ennemies_collector) == 0:
                        level_result = "success"
                        break
                    else:
                        level_result = "failed"
                        break
                else:
                    level_result = "success"
                    break

            for block in mapBlocks:
                screen.blit(block.image, (block.rect.x, block.rect.y))
                sprites_collecor.add(block)

            updateMap = False

        # verif direction of acceleration
        if player.accX != 0:
            if frame % 10 == 0:
                if player.image == player.moving1:
                    player.image = player.moving2
                else:
                    player.image = player.moving1
        else:
            player.image = player.still
            if dirPlayer == -1:
                player.image = pg.transform.flip(player.image, True, False)

        if player.accX < 0:
            dirPlayer = -1
            screen.blit(pg.transform.flip(player.image, True, False),
                        player.rect)
        elif player.accX > 0:
            dirPlayer = 1
            screen.blit(player.image, player.rect)
        else:
            screen.blit(player.image, player.rect)

        # Key manager
        firstDraw = True
        while True:
            events = pg.event.get()
            keyAction = km.get(events)

            updateMap = True
            if keyAction == "exit":
                running = False
                pg.quit()
                break
            elif keyAction == "onetap_go_up" and not isPause:
                player.accelerateY(-2, mapBlocks,
                                   [minX_aroundPlayer, maxX_aroundPlayer],
                                   BLOCKSIZE)
            elif keyAction == "go_left" and not isPause:
                player.accelerateX(-0.2)
            elif keyAction == "go_right" and not isPause:
                player.accelerateX(0.2)
            elif keyAction == "go_projectile" and not isPause:
                if player.ammo > 0 and not player.reloading:
                    newBullet = entities.Bullet(
                        player.rect.x,
                        player.rect.y + (random.randint(0, 6) - 3), BLOCKSIZE,
                        dirPlayer)
                    SOUND_gunshot.play()
                    bullets_collector.append(newBullet)
                    player.ammo -= 1
                    if player.ammo == 0:
                        player.reloading = True
            elif keyAction == "slowDown_switch" and not isPause:
                if not slowDownPower:
                    if player.mana > 0:
                        slowDownPower = True
                        SOUND_bulletTime_in.play()
                        pg.mixer.music.set_volume(0)
                else:
                    slowDownPower = False
                    SOUND_bulletTime_out.play()
                    pg.mixer.music.set_volume(0.1)
            elif keyAction == "pause_switch":
                isPause = not isPause
            elif keyAction == "cheat":
                return "success"
            else:
                if not isPause:
                    if len(bullets_collector) == 0 and len(
                            ennemies_collector) == 0:
                        updateMap = False

            if not isPause:
                break
            else:
                if firstDraw:
                    # Pause menu
                    screen.blit(pause_menu, (0, 0))
                    pg.display.update()
                    firstDraw = False

        # Ennemies update
        for ennemy in ennemies_collector:
            isAlive = ennemy.update(player, screen)
            if ennemy.type == 1:
                if frame % 30 == 0:
                    if ennemy.image == ennemy.an1:
                        ennemy.image = ennemy.an2
                    else:
                        ennemy.image = ennemy.an1
            elif ennemy.type == 2:
                canShoot, shootingDirection = ennemy.canShoot(player)
                if canShoot:
                    newBullet = entities.Bullet(
                        ennemy.rect.x,
                        ennemy.rect.y + (random.randint(0, 6) - 3), BLOCKSIZE,
                        shootingDirection)
                    SOUND_gunshot.play()
                    bullets_collector_ennemies.append(newBullet)
            elif ennemy.type == 3:
                canShoot, shootingDirection = ennemy.canShoot(player)
                if canShoot:
                    newBullet = entities.Bullet(
                        ennemy.rect.x,
                        player.rect.y + (random.randint(0, 6) - 3), BLOCKSIZE,
                        shootingDirection)
                    SOUND_gunshot.play()
                    bullets_collector_ennemies.append(newBullet)
                if ennemy.health < ennemy.maxHealth / 2 and len(
                        missiles_collector) == 0:
                    missiles_collector.append(
                        entities.Missile(player.posX + player.rect.x,
                                         BLOCKSIZE, 0))

            if not isAlive:
                ennemies_collector.remove(ennemy)
                player.mana += 25
                if player.mana > player.maxMana:
                    player.mana = player.maxMana

            screen.blit(ennemy.image, ennemy.rect)

        # Missiles update
        for missile in missiles_collector:
            if missile.v == 0 and missile.frameStart <= frame:
                missile.v = 6

            isExisting = missile.update(player, mapBlocks)
            if not isExisting:
                missiles_collector.remove(missile)

            screen.blit(missile.image, missile.rect)

        # Superpower
        if slowDownPower:
            if player.mana > 0:
                screen.blit(slowDownOverlay, (0, 0))
                player.mana -= 1
                timer = clock.tick(18)

            else:
                timer = clock.tick(FPS)
                SOUND_bulletTime_out.play()
                slowDownPower = False
                pg.mixer.music.set_volume(0.1)

        # Player update
        playerUpdated = player.update(mapBlocks,
                                      [minX_aroundPlayer, maxX_aroundPlayer],
                                      BLOCKSIZE, frame)
        if playerUpdated[0] != playerUpdated[1]:
            updateMap = True

        # HUD
        # Player Mana
        w = 300
        h = 25
        hW = (player.mana / player.maxMana) * w
        pg.draw.rect(screen, (200, 200, 200), pg.Rect(50, 115, w, h))
        pg.draw.rect(screen, (100, 100, 255), pg.Rect(50, 115, hW, h))
        screen.blit(manaBarOutliner, (10, 75))

        # Player health
        w = 400
        h = 40
        hW = (player.health / player.maxHealth) * w
        ratio = (player.health / player.maxHealth)
        pg.draw.rect(screen, (200, 200, 200), pg.Rect(50, 50, w, h))
        pg.draw.rect(screen, getHealthBarColor(ratio), pg.Rect(50, 50, hW, h))
        screen.blit(healthBarOutliner, (10, 10))

        # Player ammo
        screen.blit(ammo_imgs[player.ammo], (10, SCREEN_HEIGHT - 165))

        # Bullets update
        for bullet in bullets_collector:
            shouldLive = bullet.update(player, mapBlocks, ennemies_collector)
            if not shouldLive:
                bullets_collector.remove(bullet)
            screen.blit(bullet.image, bullet.rect)

        for bullet in bullets_collector_ennemies:
            shouldLive = bullet.update_en(player, mapBlocks)
            if not shouldLive:
                bullets_collector_ennemies.remove(bullet)
            screen.blit(bullet.image, bullet.rect)

        # show fps
        """
        fps = str(0)
        font = pg.font.SysFont("Arial", 30)
        fps_text = font.render(fps, 1, pg.Color("black"))
        screen.blit(fps_text, (20,SCREEN_HEIGHT - 100))
        """

        # Damage took
        if lastPlayerHealth != player.health:
            damageTimer = 50

        if damageTimer > 0:
            pg.draw.rect(screen, (200, 0, 0),
                         pg.Rect(5, 5, SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10),
                         4)
            damageTimer -= 1

        if player.health <= 0:
            break

        # Draw sprites
        pg.display.update()
        pg.display.flip()

        lastPlayerHealth = player.health
        if frame % 60 == 0:
            player.mana += 1 * bullet_time_regen_coeff
            if player.mana > player.maxMana:
                player.mana = player.maxMana
        frame += 1

    if level_result == "success":
        screen.blit(pg.image.load("../assets/success.png"), (0, 0))
    else:
        screen.blit(pg.image.load("../assets/gameover.png"), (0, 0))
    pg.display.update()

    time.sleep(5)

    return level_result
Exemplo n.º 5
0
        enemyShoot()
        enemyShoot()

    # Checks for the events in a list of events
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    # Checks if the right and left keys are pressed, if so, moves
    # the ship in the current direction
    keys = pygame.key.get_pressed()
    if keys[K_RIGHT] and ship.hitBox[0] + 64 < WIDTH:
        ship.move(RIGHT)
    elif keys[K_LEFT] and ship.hitBox[0] > 0:
        ship.move(LEFT)
    if keys[K_SPACE] and ship.hasAmmo:
        bullets.append(entities.Bullet(ship, UP, FRIEND))
        shipShooting.play()
        ship.hasAmmo = False
    
    # Checks which side the enemies must go (if they had hit the border)
    if enemies[0][nColumns - 1].x >= WIDTH - (enemiesX + enemySize):
        goingRight = False
    if enemies[0][0].x < enemiesX:
        goingRight = True

    # Moves all the enemies
    for i in range(nRows):
        for j in range(nColumns):
            enemy = enemies[i][j]
            if goingRight:
                enemy.move(RIGHT)