import pygame
import random
import sys
import math
import subprocess
import time
import os

# Initialize pygame and font
pygame.init()
pygame.font.init()

FULLSCREEN = True
WIDTH, HEIGHT = 800, 600
if FULLSCREEN:
    info = pygame.display.Info()
    WIDTH, HEIGHT = info.current_w, info.current_h
    screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
else:
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2D Shooter Game")

clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
INTRO_FONT = pygame.font.SysFont(None, 38)

# Powerup settings
POWERUP_TYPES = ["invincibility", "super_speed", "fast_shoot", "teleport"]
POWERUP_COLORS = {
    "invincibility": (255, 255, 80),
    "super_speed": (80, 180, 255),
    "fast_shoot": (255, 90, 90),
    "teleport": (180, 80, 255),
}
POWERUP_ICONS = {
    "invincibility": "🛡",
    "super_speed": "⚡",
    "fast_shoot": "🔫",
    "teleport": "🌀",
}
POWERUP_DURATION = 5

def draw_screen_warning(screen, danger_level, blink_on):
    """Draws blinking side bars if a Kavin is close to the player."""
    if danger_level == 0 or not blink_on:
        return
    color = (255, 40, 40) if danger_level == 2 else (255, 120, 0)
    width = 32 if danger_level == 2 else 16
    alpha = 180 if danger_level == 2 else 120
    s = pygame.Surface((width, HEIGHT), pygame.SRCALPHA)
    s.fill((*color, alpha))
    screen.blit(s, (0, 0))
    screen.blit(s, (WIDTH-width, 0))

def show_jumpscare(screen):
    try:
        jump_img = pygame.image.load("jump.png").convert_alpha()
        jump_img = pygame.transform.scale(jump_img, (WIDTH, HEIGHT))
        screen.blit(jump_img, (0, 0))
        pygame.display.flip()
        pygame.mixer.music.stop()
        # If you have a jumpscare sound, you can play it here:
        # pygame.mixer.Sound("jumpsound.wav").play()
        pygame.time.wait(1800)
    except Exception as e:
        print("Error loading jumpscare:", e)
        screen.fill((255,0,0))
        font = pygame.font.SysFont(None, 100)
        text = font.render("JUMPSCARE!", True, (255,255,255))
        screen.blit(text, (WIDTH//2-text.get_width()//2, HEIGHT//2-text.get_height()//2))
        pygame.display.flip()
        pygame.time.wait(1800)

def show_game_over_and_play_again(screen, score):
    screen.fill((0,0,0))
    fontbig = pygame.font.SysFont(None, 70)
    game_over_text = fontbig.render("Game Over!", True, (255,0,0))
    screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 120))
    score_text = font.render(f"Final Score: {score}", True, (255,255,255))
    screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, HEIGHT // 2 - 50))
    button_rect = pygame.Rect(WIDTH//2 - 140, HEIGHT//2 + 50, 280, 70)
    pygame.draw.rect(screen, (60,180,80), button_rect)
    play_again_text = font.render("PLAY AGAIN", True, (0,0,0))
    screen.blit(play_again_text, (button_rect.centerx-play_again_text.get_width()//2, button_rect.centery-play_again_text.get_height()//2))
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit()
            if event.type == pygame.KEYDOWN and event.key in (pygame.K_RETURN, pygame.K_SPACE):
                return True
            if event.type == pygame.MOUSEBUTTONDOWN and button_rect.collidepoint(event.pos):
                return True
        pygame.time.wait(10)

try:
    ENEMY_IMAGE = pygame.image.load("kavin.png").convert_alpha()
    ENEMY_IMAGE = pygame.transform.scale(ENEMY_IMAGE, (64, 64))
except Exception as e:
    print("Error loading kavin.png:", e)
    ENEMY_IMAGE = None

try:
    PLAYER_IMAGE = pygame.image.load("surkhab.png").convert_alpha()
    PLAYER_IMAGE = pygame.transform.scale(PLAYER_IMAGE, (64, 64))
except Exception as e:
    print("Error loading surkhab.png:", e)
    PLAYER_IMAGE = None

# --- ADD THIS BLOCK FOR BULLET IMAGE ---
try:
    BULLET_IMAGE = pygame.image.load("bullets.png").convert_alpha()
    BULLET_IMAGE = pygame.transform.scale(BULLET_IMAGE, (20, 20))  # Adjust size as needed
except Exception as e:
    print("Error loading bullets.png:", e)
    BULLET_IMAGE = None

def show_intro(screen, font, lore_text):
    skip_rect = pygame.Rect(WIDTH//2-80, HEIGHT-90, 160, 55)
    while True:
        screen.fill((20,20,40))
        y = 80
        for line in lore_text.split("\n"):
            text = font.render(line, True, (255,255,255))
            screen.blit(text, (WIDTH//2-text.get_width()//2, y))
            y += 38 + 8
        pygame.draw.rect(screen, (100,100,100), skip_rect)
        skip_label = font.render("SKIP", True, (255,255,255))
        screen.blit(skip_label, (skip_rect.centerx-skip_label.get_width()//2, skip_rect.centery-skip_label.get_height()//2))
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit()
            if event.type == pygame.KEYDOWN and event.key in (pygame.K_RETURN, pygame.K_SPACE, pygame.K_ESCAPE):
                return
            if event.type == pygame.MOUSEBUTTONDOWN and skip_rect.collidepoint(event.pos):
                return
        pygame.time.wait(10)

def vec_from_points(start, end, speed):
    dx = end[0] - start[0]
    dy = end[1] - start[1]
    dist = math.hypot(dx, dy)
    if dist == 0:
        return (0, 0)
    return (dx / dist * speed, dy / dist * speed)

class Powerup:
    def __init__(self, x, y, ptype):
        self.x = x
        self.y = y
        self.ptype = ptype
        self.radius = 20
        self.spawn_time = time.time()

    def draw(self, surface, offset):
        px = int(self.x - offset[0])
        py = int(self.y - offset[1])
        color = POWERUP_COLORS[self.ptype]
        pygame.draw.circle(surface, color, (px, py), self.radius)
        power_font = pygame.font.SysFont(None, 32)
        icon = POWERUP_ICONS[self.ptype]
        icon_surf = power_font.render(icon, True, (40, 40, 40))
        rect = icon_surf.get_rect(center=(px, py))
        surface.blit(icon_surf, rect)

    def rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius, self.radius*2, self.radius*2)

class Player:
    def __init__(self):
        self.x = 3200 // 2
        self.y = 260 + 80
        self.radius = 20
        self.speed = 5

    def move(self, dx, dy, solid_rects, super_speed):
        speed = self.speed * (2 if super_speed else 1)
        new_x = self.x + dx * speed
        new_y = self.y + dy * speed
        new_x = max(self.radius, min(3200 - self.radius, new_x))
        new_y = max(260 + self.radius, min(3200 - self.radius, new_y))
        player_rect = pygame.Rect(new_x - self.radius, new_y - self.radius, self.radius * 2, self.radius * 2)
        for rect in solid_rects:
            if player_rect.colliderect(rect):
                return
        self.x = new_x
        self.y = new_y

    def draw(self, surface, offset, power=None, power_time=0, power_active=False):
        px = int(self.x - offset[0])
        py = int(self.y - offset[1])

        mx, my = pygame.mouse.get_pos()
        angle = math.atan2(my - py, mx - px)
        if PLAYER_IMAGE:
            rotated_image = pygame.transform.rotate(PLAYER_IMAGE, -math.degrees(angle))
            rect = rotated_image.get_rect(center=(px, py))
            surface.blit(rotated_image, rect)
        else:
            pygame.draw.circle(surface, (255,255,255), (px, py), self.radius)
        gun_length = self.radius + 23
        barrel_length = 26
        gun_x = px + math.cos(angle) * (64 // 2 - 7)
        gun_y = py + math.sin(angle) * (64 // 2 - 7)
        barrel_end_x = gun_x + math.cos(angle) * barrel_length
        barrel_end_y = gun_y + math.sin(angle) * barrel_length
        pygame.draw.line(surface, (100,100,100), (gun_x, gun_y), (barrel_end_x, barrel_end_y), 7)
        handle_angle = angle + math.pi / 2
        handle_length = 13
        handle_end_x = gun_x + math.cos(handle_angle) * handle_length
        handle_end_y = gun_y + math.sin(handle_angle) * handle_length
        pygame.draw.line(surface, (60,60,60), (gun_x, gun_y), (handle_end_x, handle_end_y), 6)
        # Powerup ring and timer
        if power_active:
            color = POWERUP_COLORS[power]
            pygame.draw.circle(surface, color, (px, py), 28, 4)
            tleft = max(0, POWERUP_DURATION - (time.time() - power_time))
            bar_w = 60
            pygame.draw.rect(surface, color, (px-bar_w//2, py-50, int(bar_w * (tleft/POWERUP_DURATION)), 8))
            power_font = pygame.font.SysFont(None, 36)
            icon = POWERUP_ICONS[power]
            icon_surf = power_font.render(icon, True, color)
            surface.blit(icon_surf, (px-18, py-88))

def draw_tombstone(surface, x, y, color=(160,160,160)):
    pygame.draw.rect(surface, color, (x, y + 32 // 3, 24, 32 * 2 // 3))
    pygame.draw.ellipse(surface, color, (x, y, 24, 32 // 2))

def draw_big_graveyard_bg(surface, solid_graves):
    surface.fill((30,30,90), rect=pygame.Rect(0, 0, 3200, 260))
    moon_x, moon_y = 3200 - 300, 150
    pygame.draw.circle(surface, (200,200,180), (moon_x, moon_y), 90)
    pygame.draw.rect(surface, (20, 50, 20), (0, 260, 3200, 3200 - 260))
    random.seed(0)
    solid_rects = []
    graves_placed = 0
    max_solid_graves = 30
    for row in range((3200 - 600)//100):
        for col in range(3200//170):
            gx = 20 + col * 170 + random.randint(-8,8)
            gy = 260 + 20 + row * 100 + random.randint(-8,8)
            if gy > 3200 - 80: continue
            if random.random() > 0.65: continue
            color = (160,160,160)
            is_solid = False
            if graves_placed < max_solid_graves and random.random() < 0.15:
                is_solid = True
                color = (100, 100, 170)
                solid_rects.append(pygame.Rect(gx, gy, 24, 32))
                graves_placed += 1
            draw_tombstone(surface, gx, gy, color)
            if is_solid:
                solid_graves.append((gx, gy, 24, 32))
    pygame.draw.rect(surface, (80,80,80), (500, 3200//2 + 100, 100, 50))
    pygame.draw.circle(surface, (34,139,34), (3200//3, 3200//3), 60)
    pygame.draw.rect(surface, (90,90,90), (1800, 2100, 70, 35))

class Bullet:
    def __init__(self, start, target, player_pos):
        self.x, self.y = start
        self.radius = 5
        tx = player_pos[0] + (target[0] - WIDTH // 2)
        ty = player_pos[1] + (target[1] - HEIGHT // 2)
        self.vx, self.vy = vec_from_points((self.x, self.y), (tx, ty), 10)

    def move(self):
        self.x += self.vx
        self.y += self.vy

    def offscreen(self, offset):
        sx = self.x - offset[0]
        sy = self.y - offset[1]
        return sx < -self.radius or sx > WIDTH + self.radius or sy < -self.radius or sy > HEIGHT + self.radius

    def draw(self, surface, offset):
        sx = int(self.x - offset[0])
        sy = int(self.y - offset[1])
        if BULLET_IMAGE:
            rect = BULLET_IMAGE.get_rect(center=(sx, sy))
            surface.blit(BULLET_IMAGE, rect)
        else:
            pygame.draw.circle(surface, (255,255,255), (sx, sy), self.radius)

    def rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius, self.radius * 2, self.radius * 2)

class Enemy:
    def __init__(self, player_pos):
        edge = random.choice(['top', 'bottom', 'left', 'right'])
        if edge == 'top':
            self.x = random.randint(32, 3200 - 32)
            self.y = 260 + 32
        elif edge == 'bottom':
            self.x = random.randint(32, 3200 - 32)
            self.y = 3200 - 32
        elif edge == 'left':
            self.x = 32
            self.y = random.randint(260 + 32, 3200 - 32)
        else:
            self.x = 3200 - 32
            self.y = random.randint(260 + 32, 3200 - 32)
        self.radius = 32
        self.vx = 0
        self.vy = 0

    def move(self, player_pos, solid_rects):
        self.vx, self.vy = vec_from_points((self.x, self.y), player_pos, 2)
        self.x += self.vx
        self.y += self.vy

    def draw(self, surface, offset):
        sx = int(self.x - offset[0])
        sy = int(self.y - offset[1])
        if ENEMY_IMAGE:
            rect = ENEMY_IMAGE.get_rect(center=(sx, sy))
            surface.blit(ENEMY_IMAGE, rect)
        else:
            pygame.draw.circle(surface, (255,0,0), (sx, sy), self.radius)

    def rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius, self.radius * 2, self.radius * 2)

def main():
    while True:
        # --- MAIN GAME LOOP ---
        lore = (
            "Night 1: The Graveyard\n"
            "Surkhab, the good one, finds himself in the haunted graveyard.\n"
            "But he is not alone. Kavin, the bad one, rises with his clones!\n"
            "Survive the endless night and beware: every Kavin you defeat brings more...\n"
            "Use WASD or arrow keys to move. Click to shoot. SPACE to activate powerup (one use!). ESC to quit. Good luck, Surkhab!"
        )
        show_intro(screen, INTRO_FONT, lore)

        player = Player()
        bullets = []
        enemies = []
        powerups = []
        player_power = None
        player_power_ready = False
        player_power_type = None
        player_power_time = 0
        power_active = False
        fast_shoot_cooldown = 0.1
        normal_shoot_cooldown = 0.33
        last_shot_time = 0
        score = 0
        frame_count = 0
        running = True
        night = 1
        night_timer = 0
        duplication_base_time = 600
        duplication_timer = 0

        solid_graves = []
        static_bg = pygame.Surface((3200, 3200), pygame.SRCALPHA)
        draw_big_graveyard_bg(static_bg, solid_graves)
        solid_rects = [pygame.Rect(x, y, w, h) for (x, y, w, h) in solid_graves]

        STARTING_ENEMIES = 10
        for _ in range(STARTING_ENEMIES):
            enemies.append(Enemy((player.x, player.y)))

        effect_super_speed = False
        effect_invincible = False
        effect_fast_shoot = False
        effect_teleport_flash = False

        game_over = False
        jumpscare_done = False

        while not game_over:
            clock.tick(60)

            # --- Powerup effect duration check
            if power_active and (time.time() - player_power_time > POWERUP_DURATION or player_power_type == 'teleport'):
                power_active = False
                effect_super_speed = False
                effect_invincible = False
                effect_fast_shoot = False
                effect_teleport_flash = False
                player_power_type = None

            # --- Danger warning effect ---
            min_dist = min(math.hypot(enemy.x - player.x, enemy.y - player.y) for enemy in enemies) if enemies else 999999
            danger_level = 0
            if min_dist < 120:
                danger_level = 2
            elif min_dist < 240:
                danger_level = 1
            blink_on = (pygame.time.get_ticks() // 200) % 2 == 0
            draw_screen_warning(screen, danger_level, blink_on)

            # --- NIGHT TRANSITION: If score >= 30, go to night 2 ---
            if score >= 30:
                lore2 = (
                    "Night 2: The School\n"
                    "Surkhab, the good one, has survived the graveyard, but now Kavin, the bad one, haunts the school!\n"
                    "The desks block your path. Every Kavin you defeat brings more.\n"
                    "Can Surkhab escape the endless nightmare, or will Kavin's clones overwhelm him?\n"
                    "Move, shoot, and survive. SPACE to activate powerup (one use!). ESC to quit. Good luck!"
                )
                show_intro(screen, INTRO_FONT, lore2)
                pygame.quit()
                subprocess.Popen([sys.executable, "night2.py"])
                sys.exit()

            offset_x = int(player.x - WIDTH // 2)
            offset_y = int(player.y - HEIGHT // 2)
            offset_x = max(0, min(3200 - WIDTH, offset_x))
            offset_y = max(0, min(3200 - HEIGHT, offset_y))
            offset = (offset_x, offset_y)
            screen.blit(static_bg, (0, 0), area=(offset_x, offset_y, WIDTH, HEIGHT))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit(); sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit(); sys.exit()
                    elif event.key == pygame.K_SPACE and player_power_ready and not power_active:
                        power_active = True
                        player_power_time = time.time()
                        if player_power_type == "teleport":
                            mx, my = pygame.mouse.get_pos()
                            player.x = int(player.x + (mx - WIDTH//2))
                            player.y = int(player.y + (my - HEIGHT//2))
                            player.x = max(player.radius, min(3200 - player.radius, player.x))
                            player.y = max(260 + player.radius, min(3200 - player.radius, player.y))
                            effect_teleport_flash = True
                            player_power_ready = False
                        elif player_power_type == "invincibility":
                            effect_invincible = True
                            player_power_ready = False
                        elif player_power_type == "super_speed":
                            effect_super_speed = True
                            player_power_ready = False
                        elif player_power_type == "fast_shoot":
                            effect_fast_shoot = True
                            player_power_ready = False
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    now = time.time()
                    shoot_delay = fast_shoot_cooldown if effect_fast_shoot and power_active and player_power_type == "fast_shoot" else normal_shoot_cooldown
                    if now - last_shot_time > shoot_delay:
                        bullets.append(Bullet((int(player.x), int(player.y)), pygame.mouse.get_pos(), (player.x, player.y)))
                        last_shot_time = now

            keys = pygame.key.get_pressed()
            dx = dy = 0
            if keys[pygame.K_a] or keys[pygame.K_LEFT]:
                dx -= 1
            if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
                dx += 1
            if keys[pygame.K_w] or keys[pygame.K_UP]:
                dy -= 1
            if keys[pygame.K_s] or keys[pygame.K_DOWN]:
                dy += 1
            player.move(dx, dy, solid_rects, effect_super_speed and power_active and player_power_type == "super_speed")

            # --- Powerup collisions
            player_rect = pygame.Rect(player.x - 20, player.y - 20, 40, 40)
            for p in powerups[:]:
                if player_rect.colliderect(p.rect()) and not player_power_ready and not power_active:
                    player_power_ready = True
                    player_power_type = p.ptype
                    powerups.remove(p)

            # --- Bullets
            for bullet in bullets[:]:
                bullet.move()
                if bullet.offscreen(offset):
                    bullets.remove(bullet)

            # --- Enemy logic (move, check player collision)
            for enemy in enemies[:]:
                enemy.move((player.x, player.y), solid_rects)
                if effect_invincible and power_active and player_power_type == "invincibility":
                    continue # ignore enemy collision while invincible
                if enemy.rect().colliderect(player_rect):
                    game_over = True

            # --- Enemy death: 1/5 drop powerups
            enemies_to_add = []
            for enemy in enemies[:]:
                for bullet in bullets[:]:
                    if enemy.rect().colliderect(bullet.rect()):
                        enemies.remove(enemy)
                        bullets.remove(bullet)
                        score += 1
                        if random.random() < 0.2:
                            ptype = random.choice(POWERUP_TYPES)
                            powerups.append(Powerup(enemy.x, enemy.y, ptype))
                        enemies_to_add.append(Enemy((player.x, player.y)))
                        enemies_to_add.append(Enemy((player.x, player.y)))
                        break
            enemies.extend(enemies_to_add)

            # --- Draw everything ---
            if player_power_ready:
                player.draw(screen, offset, player_power_type, 0, False)
                txt = font.render(f"Press SPACE for {player_power_type.upper()}!", True, POWERUP_COLORS[player_power_type])
                screen.blit(txt, (20, HEIGHT-60))
            elif power_active:
                player.draw(screen, offset, player_power_type, player_power_time, True)
                if player_power_type != "teleport":
                    txt = font.render(f"{player_power_type.upper()} ACTIVE!", True, POWERUP_COLORS[player_power_type])
                    screen.blit(txt, (20, HEIGHT-60))
            else:
                player.draw(screen, offset)

            for p in powerups:
                p.draw(screen, offset)
            for bullet in bullets:
                bullet.draw(screen, offset)
            for enemy in enemies:
                enemy.draw(screen, offset)
            score_text = font.render(f"Score: {score}", True, (255,255,255))
            night_text = font.render(f"Night: {night}", True, (255,255,255))
            screen.blit(score_text, (10, 10))
            screen.blit(night_text, (10, 40))

            pygame.display.flip()
            screen.fill((0,0,0))

        # --- JUMPSCARE on game over ---
        show_jumpscare(screen)
        # --- Play Again? ---
        play_again = show_game_over_and_play_again(screen, score)
        if not play_again:
            pygame.quit()
            sys.exit()
        # If play again, loop restarts automatically

if __name__ == "__main__":
    main() this is my main.py  and this is my night2.py import pygame
import random
import sys
import math
import time
import os

pygame.init()
pygame.font.init()

FULLSCREEN = True
WIDTH, HEIGHT = 800, 600
if FULLSCREEN:
    info = pygame.display.Info()
    WIDTH, HEIGHT = info.current_w, info.current_h
    screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
else:
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2D Shooter Game - Night 2: School")

clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
INTRO_FONT = pygame.font.SysFont(None, 38)

PLAYER_IMG_SIZE = 64

POWERUP_TYPES = ["invincibility", "super_speed", "fast_shoot", "teleport"]
POWERUP_COLORS = {
    "invincibility": (255, 255, 80),
    "super_speed": (80, 180, 255),
    "fast_shoot": (255, 90, 90),
    "teleport": (180, 80, 255),
}
POWERUP_ICONS = {
    "invincibility": "🛡",
    "super_speed": "⚡",
    "fast_shoot": "🔫",
    "teleport": "🌀",
}
POWERUP_DURATION = 5

def draw_screen_warning(screen, danger_level, blink_on):
    if danger_level == 0 or not blink_on:
        return
    color = (255, 40, 40) if danger_level == 2 else (255, 120, 0)
    width = 32 if danger_level == 2 else 16
    alpha = 180 if danger_level == 2 else 120
    s = pygame.Surface((width, HEIGHT), pygame.SRCALPHA)
    s.fill((*color, alpha))
    screen.blit(s, (0, 0))
    screen.blit(s, (WIDTH-width, 0))

def show_intro(screen, font, lore_text):
    skip_rect = pygame.Rect(WIDTH//2-80, HEIGHT-90, 160, 55)
    while True:
        screen.fill((20,20,40))
        y = 80
        for line in lore_text.split("\n"):
            text = font.render(line, True, (255,255,255))
            screen.blit(text, (WIDTH//2-text.get_width()//2, y))
            y += 38 + 8
        pygame.draw.rect(screen, (100,100,100), skip_rect)
        skip_label = font.render("SKIP", True, (255,255,255))
        screen.blit(skip_label, (skip_rect.centerx-skip_label.get_width()//2, skip_rect.centery-skip_label.get_height()//2))
        pygame.display.flip()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit()
            if event.type == pygame.KEYDOWN and event.key in (pygame.K_RETURN, pygame.K_SPACE, pygame.K_ESCAPE):
                return
            if event.type == pygame.MOUSEBUTTONDOWN and skip_rect.collidepoint(event.pos):
                return
        pygame.time.wait(10)

def show_jumpscare(screen):
    try:
        jump_img = pygame.image.load("jump.png").convert_alpha()
        jump_img = pygame.transform.scale(jump_img, (WIDTH, HEIGHT))
        screen.blit(jump_img, (0, 0))
        pygame.display.flip()
        pygame.mixer.music.stop()
        # If you have a jumpscare sound, you can play it here:
        # pygame.mixer.Sound("jumpsound.wav").play()
        pygame.time.wait(1800)
    except Exception as e:
        print("Error loading jumpscare:", e)
        screen.fill((255,0,0))
        font = pygame.font.SysFont(None, 100)
        text = font.render("JUMPSCARE!", True, (255,255,255))
        screen.blit(text, (WIDTH//2-text.get_width()//2, HEIGHT//2-text.get_height()//2))
        pygame.display.flip()
        pygame.time.wait(1800)

def show_game_over_and_play_again(screen, score):
    screen.fill((0,0,0))
    fontbig = pygame.font.SysFont(None, 70)
    game_over_text = fontbig.render("Game Over!", True, (255,0,0))
    screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 120))
    score_text = font.render(f"Final Score: {score}", True, (255,255,255))
    screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, HEIGHT // 2 - 50))
    button_rect = pygame.Rect(WIDTH//2 - 140, HEIGHT//2 + 50, 280, 70)
    pygame.draw.rect(screen, (60,180,80), button_rect)
    play_again_text = font.render("PLAY AGAIN", True, (0,0,0))
    screen.blit(play_again_text, (button_rect.centerx-play_again_text.get_width()//2, button_rect.centery-play_again_text.get_height()//2))
    pygame.display.flip()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit()
            if event.type == pygame.KEYDOWN and event.key in (pygame.K_RETURN, pygame.K_SPACE):
                return True
            if event.type == pygame.MOUSEBUTTONDOWN and button_rect.collidepoint(event.pos):
                return True
        pygame.time.wait(10)

try:
    ENEMY_IMAGE = pygame.image.load("kavin.png").convert_alpha()
    ENEMY_IMAGE = pygame.transform.scale(ENEMY_IMAGE, (64, 64))
except Exception as e:
    print("Error loading kavin.png:", e)
    ENEMY_IMAGE = None

try:
    PLAYER_IMAGE = pygame.image.load("surkhab.png").convert_alpha()
    PLAYER_IMAGE = pygame.transform.scale(PLAYER_IMAGE, (PLAYER_IMG_SIZE, PLAYER_IMG_SIZE))
except Exception as e:
    print("Error loading surkhab.png:", e)
    PLAYER_IMAGE = None

def vec_from_points(start, end, speed):
    dx = end[0] - start[0]
    dy = end[1] - start[1]
    dist = math.hypot(dx, dy)
    if dist == 0:
        return (0, 0)
    return (dx / dist * speed, dy / dist * speed)

class Powerup:
    def __init__(self, x, y, ptype):
        self.x = x
        self.y = y
        self.ptype = ptype
        self.radius = 20
        self.spawn_time = time.time()

    def draw(self, surface, offset):
        px = int(self.x - offset[0])
        py = int(self.y - offset[1])
        color = POWERUP_COLORS[self.ptype]
        pygame.draw.circle(surface, color, (px, py), self.radius)
        power_font = pygame.font.SysFont(None, 32)
        icon = POWERUP_ICONS[self.ptype]
        icon_surf = power_font.render(icon, True, (40, 40, 40))
        rect = icon_surf.get_rect(center=(px, py))
        surface.blit(icon_surf, rect)

    def rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius, self.radius*2, self.radius*2)

class Player:
    def __init__(self):
        self.x = 3200 // 2
        self.y = 400
        self.radius = 20
        self.speed = 5

    def move(self, dx, dy, solid_rects, super_speed):
        speed = self.speed * (2 if super_speed else 1)
        new_x = self.x + dx * speed
        new_y = self.y + dy * speed
        new_x = max(self.radius, min(3200 - self.radius, new_x))
        new_y = max(self.radius, min(3200 - self.radius, new_y))
        player_rect = pygame.Rect(new_x - self.radius, new_y - self.radius, self.radius * 2, self.radius * 2)
        for rect in solid_rects:
            if player_rect.colliderect(rect):
                return
        self.x = new_x
        self.y = new_y

    def draw(self, surface, offset, power=None, power_time=0, power_active=False):
        px = int(self.x - offset[0])
        py = int(self.y - offset[1])
        mx, my = pygame.mouse.get_pos()
        angle = math.atan2(my - py, mx - px)
        if PLAYER_IMAGE:
            rotated_image = pygame.transform.rotate(PLAYER_IMAGE, -math.degrees(angle))
            rect = rotated_image.get_rect(center=(px, py))
            surface.blit(rotated_image, rect)
        else:
            pygame.draw.circle(surface, (255,255,255), (px, py), self.radius)
        gun_length = self.radius + 23
        barrel_length = 26
        gun_x = px + math.cos(angle) * (PLAYER_IMG_SIZE // 2 - 7)
        gun_y = py + math.sin(angle) * (PLAYER_IMG_SIZE // 2 - 7)
        barrel_end_x = gun_x + math.cos(angle) * barrel_length
        barrel_end_y = gun_y + math.sin(angle) * barrel_length
        pygame.draw.line(surface, (100,100,100), (gun_x, gun_y), (barrel_end_x, barrel_end_y), 7)
        handle_angle = angle + math.pi / 2
        handle_length = 13
        handle_end_x = gun_x + math.cos(handle_angle) * handle_length
        handle_end_y = gun_y + math.sin(handle_angle) * handle_length
        pygame.draw.line(surface, (60,60,60), (gun_x, gun_y), (handle_end_x, handle_end_y), 6)
        if power_active:
            color = POWERUP_COLORS[power]
            pygame.draw.circle(surface, color, (px, py), 28, 4)
            tleft = max(0, POWERUP_DURATION - (time.time() - power_time))
            bar_w = 60
            pygame.draw.rect(surface, color, (px-bar_w//2, py-50, int(bar_w * (tleft/POWERUP_DURATION)), 8))
            power_font = pygame.font.SysFont(None, 36)
            icon = POWERUP_ICONS[power]
            icon_surf = power_font.render(icon, True, color)
            surface.blit(icon_surf, (px-18, py-88))

class Bullet:
    def __init__(self, start, target, player_pos):
        self.x, self.y = start
        self.radius = 5
        tx = player_pos[0] + (target[0] - WIDTH // 2)
        ty = player_pos[1] + (target[1] - HEIGHT // 2)
        self.vx, self.vy = vec_from_points((self.x, self.y), (tx, ty), 10)

    def move(self):
        self.x += self.vx
        self.y += self.vy

    def offscreen(self, offset):
        sx = self.x - offset[0]
        sy = self.y - offset[1]
        return sx < -self.radius or sx > WIDTH + self.radius or sy < -self.radius or sy > HEIGHT + self.radius

    def draw(self, surface, offset):
        sx = int(self.x - offset[0])
        sy = int(self.y - offset[1])
        pygame.draw.circle(surface, (255,255,255), (sx, sy), self.radius)

    def rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius, self.radius * 2, self.radius * 2)

class Enemy:
    def __init__(self, player_pos):
        edge = random.choice(['top', 'bottom', 'left', 'right'])
        if edge == 'top':
            self.x = random.randint(32, 3200 - 32)
            self.y = 32
        elif edge == 'bottom':
            self.x = random.randint(32, 3200 - 32)
            self.y = 3200 - 32
        elif edge == 'left':
            self.x = 32
            self.y = random.randint(32, 3200 - 32)
        else:
            self.x = 3200 - 32
            self.y = random.randint(32, 3200 - 32)
        self.radius = 32
        self.vx = 0
        self.vy = 0

    def move(self, player_pos, solid_rects):
        self.vx, self.vy = vec_from_points((self.x, self.y), player_pos, 2)
        self.x += self.vx
        self.y += self.vy

    def draw(self, surface, offset):
        sx = int(self.x - offset[0])
        sy = int(self.y - offset[1])
        if ENEMY_IMAGE:
            rect = ENEMY_IMAGE.get_rect(center=(sx, sy))
            surface.blit(ENEMY_IMAGE, rect)
        else:
            pygame.draw.circle(surface, (255,0,0), (sx, sy), self.radius)

    def rect(self):
        return pygame.Rect(self.x - self.radius, self.y - self.radius, self.radius * 2, self.radius * 2)

def draw_desk(surface, x, y):
    pygame.draw.rect(surface, (160, 120, 80), (x, y, 60, 36))
    pygame.draw.rect(surface, (60,40,20), (x + 5, y + 5, 60-10, 8))

def draw_school_bg(surface, solid_desks):
    surface.fill((150, 150, 180))
    pygame.draw.rect(surface, (130, 130, 130), (0, 0, 3200, 60))
    pygame.draw.rect(surface, (130, 130, 130), (0, 3200-60, 3200, 60))
    pygame.draw.rect(surface, (130, 130, 130), (0, 0, 60, 3200))
    pygame.draw.rect(surface, (130, 130, 130), (3200-60, 0, 60, 3200))
    pygame.draw.rect(surface, (90, 70, 40), (3200//2-50, 10, 100, 30))
    pygame.draw.rect(surface, (90, 70, 40), (3200//2-50, 3200-40, 100, 30))
    random.seed(1)
    for row in range(6):
        for col in range(10):
            gx = 180 + col * 220 + random.randint(-10, 10)
            gy = 300 + row * 120 + random.randint(-10, 10)
            if gy > 3200 - 100: continue
            draw_desk(surface, gx, gy)
            solid_desks.append((gx, gy, 60, 36))

def main():
    while True:
        # --- MAIN GAME LOOP ---
        lore2 = (
            "Night 2: The School\n"
            "Surkhab, the good one, has survived the graveyard, but now Kavin, the bad one, haunts the school!\n"
            "The desks block your path. Every Kavin you defeat brings more.\n"
            "Can Surkhab escape the endless nightmare, or will Kavin's clones overwhelm him?\n"
            "Move, shoot, and survive. SPACE to activate powerup (one use!). ESC to quit. Good luck!"
        )
        show_intro(screen, INTRO_FONT, lore2)

        player = Player()
        bullets = []
        enemies = []
        powerups = []
        player_power = None
        player_power_ready = False
        player_power_type = None
        player_power_time = 0
        power_active = False
        fast_shoot_cooldown = 0.1
        normal_shoot_cooldown = 0.33
        last_shot_time = 0
        score = 0
        frame_count = 0
        running = True
        night = 2
        night_timer = 0
        duplication_base_time = 520
        duplication_timer = 0

        solid_desks = []
        static_bg = pygame.Surface((3200, 3200), pygame.SRCALPHA)
        draw_school_bg(static_bg, solid_desks)
        solid_rects = [pygame.Rect(x, y, w, h) for (x, y, w, h) in solid_desks]

        STARTING_ENEMIES = 10
        for _ in range(STARTING_ENEMIES):
            enemies.append(Enemy((player.x, player.y)))

        effect_super_speed = False
        effect_invincible = False
        effect_fast_shoot = False
        effect_teleport_flash = False

        game_over = False
        jumpscare_done = False

        while not game_over:
            clock.tick(60)

            if power_active and (time.time() - player_power_time > POWERUP_DURATION or player_power_type == 'teleport'):
                power_active = False
                effect_super_speed = False
                effect_invincible = False
                effect_fast_shoot = False
                effect_teleport_flash = False
                player_power_type = None

            # --- Danger warning effect ---
            min_dist = min(math.hypot(enemy.x - player.x, enemy.y - player.y) for enemy in enemies) if enemies else 999999
            danger_level = 0
            if min_dist < 120:
                danger_level = 2
            elif min_dist < 240:
                danger_level = 1
            blink_on = (pygame.time.get_ticks() // 200) % 2 == 0
            draw_screen_warning(screen, danger_level, blink_on)

            offset_x = int(player.x - WIDTH // 2)
            offset_y = int(player.y - HEIGHT // 2)
            offset_x = max(0, min(3200 - WIDTH, offset_x))
            offset_y = max(0, min(3200 - HEIGHT, offset_y))
            offset = (offset_x, offset_y)
            screen.blit(static_bg, (0, 0), area=(offset_x, offset_y, WIDTH, HEIGHT))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit(); sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        pygame.quit(); sys.exit()
                    elif event.key == pygame.K_SPACE and player_power_ready and not power_active:
                        power_active = True
                        player_power_time = time.time()
                        if player_power_type == "teleport":
                            mx, my = pygame.mouse.get_pos()
                            player.x = int(player.x + (mx - WIDTH//2))
                            player.y = int(player.y + (my - HEIGHT//2))
                            player.x = max(player.radius, min(3200 - player.radius, player.x))
                            player.y = max(player.radius, min(3200 - player.radius, player.y))
                            effect_teleport_flash = True
                            player_power_ready = False
                        elif player_power_type == "invincibility":
                            effect_invincible = True
                            player_power_ready = False
                        elif player_power_type == "super_speed":
                            effect_super_speed = True
                            player_power_ready = False
                        elif player_power_type == "fast_shoot":
                            effect_fast_shoot = True
                            player_power_ready = False
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    now = time.time()
                    shoot_delay = fast_shoot_cooldown if effect_fast_shoot and power_active and player_power_type == "fast_shoot" else normal_shoot_cooldown
                    if now - last_shot_time > shoot_delay:
                        bullets.append(Bullet((int(player.x), int(player.y)), pygame.mouse.get_pos(), (player.x, player.y)))
                        last_shot_time = now

            keys = pygame.key.get_pressed()
            dx = dy = 0
            if keys[pygame.K_a] or keys[pygame.K_LEFT]:
                dx -= 1
            if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
                dx += 1
            if keys[pygame.K_w] or keys[pygame.K_UP]:
                dy -= 1
            if keys[pygame.K_s] or keys[pygame.K_DOWN]:
                dy += 1
            player.move(dx, dy, solid_rects, effect_super_speed and power_active and player_power_type == "super_speed")

            player_rect = pygame.Rect(player.x - 20, player.y - 20, 40, 40)
            for p in powerups[:]:
                if player_rect.colliderect(p.rect()) and not player_power_ready and not power_active:
                    player_power_ready = True
                    player_power_type = p.ptype
                    powerups.remove(p)

            for bullet in bullets[:]:
                bullet.move()
                if bullet.offscreen(offset):
                    bullets.remove(bullet)

            for enemy in enemies[:]:
                enemy.move((player.x, player.y), solid_rects)
                if effect_invincible and power_active and player_power_type == "invincibility":
                    continue
                if enemy.rect().colliderect(player_rect):
                    game_over = True

            enemies_to_add = []
            for enemy in enemies[:]:
                for bullet in bullets[:]:
                    if enemy.rect().colliderect(bullet.rect()):
                        enemies.remove(enemy)
                        bullets.remove(bullet)
                        score += 1
                        if random.random() < 0.2:
                            ptype = random.choice(POWERUP_TYPES)
                            powerups.append(Powerup(enemy.x, enemy.y, ptype))
                        enemies_to_add.append(Enemy((player.x, player.y)))
                        enemies_to_add.append(Enemy((player.x, player.y)))
                        break
            enemies.extend(enemies_to_add)

            if player_power_ready:
                player.draw(screen, offset, player_power_type, 0, False)
                txt = font.render(f"Press SPACE for {player_power_type.upper()}!", True, POWERUP_COLORS[player_power_type])
                screen.blit(txt, (20, HEIGHT-60))
            elif power_active:
                player.draw(screen, offset, player_power_type, player_power_time, True)
                if player_power_type != "teleport":
                    txt = font.render(f"{player_power_type.upper()} ACTIVE!", True, POWERUP_COLORS[player_power_type])
                    screen.blit(txt, (20, HEIGHT-60))
            else:
                player.draw(screen, offset)

            for p in powerups:
                p.draw(screen, offset)
            for bullet in bullets:
                bullet.draw(screen, offset)
            for enemy in enemies:
                enemy.draw(screen, offset)
            score_text = font.render(f"Score: {score}", True, (255,255,255))
            night_text = font.render(f"Night: {night}", True, (255,255,255))
            screen.blit(score_text, (10, 10))
            screen.blit(night_text, (10, 40))

            pygame.display.flip()
            screen.fill((0,0,0))

        # --- JUMPSCARE on game over ---
        show_jumpscare(screen)
        # --- Play Again? ---
        play_again = show_game_over_and_play_again(screen, score)
        if not play_again:
            pygame.quit()
            sys.exit()
        # If play again, loop restarts automatically

if __name__ == "__main__":
    main() make it so in both of these the bullets are something callled bullets.png