import pygame
import random

# Initialize pygame
pygame.init()

# Set up the screen
screen = pygame.display.set_mode((800, 600))

# Load background image
bg = pygame.image.load('трава.png')
bg = pygame.transform.scale(bg, (800, 600))

# Load player image
player = pygame.sprite.Sprite()
player.image = pygame.image.load('kek.png')
player.image.set_colorkey((255, 255, 255))
player.rect = player.image.get_rect()
player.rect.x = 400
player.rect.y = 540
xSpeed = 0

# Define the Bonus class
class Bonus(pygame.sprite.Sprite):
    def __init__(self, image, effect):
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, 800)
        self.rect.y = 0
        self.effect = effect  # Эффект бонуса

    def update(self):
        self.rect.y += 5  # Движение бонуса вниз

# Load images for life and score multiplier bonuses
life_bonus_image = pygame.image.load('poco.jpg')
life_bonus_image = pygame.transform.scale(life_bonus_image, (20, 40))
score_multiplier_image = pygame.image.load('food.png')
score_multiplier_image = pygame.transform.scale(score_multiplier_image, (20, 40))

# Create groups for bonuses, asteroids, and bullets
bonuses = pygame.sprite.Group()
asteroids = pygame.sprite.Group()
bullets = pygame.sprite.Group()

# Set initial values for score and lives
score = 0
lives = 3

# Define the Asteroid class
class Aster(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('ke.png')
        self.image.set_colorkey((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.x = random.randint(0, 800)
        self.rect.y = 0
        asteroids.add(self)
   
    def update(self):
        self.rect.y += 5  
        if self.rect.y > 600:
            self.kill()
            global lives
            lives -= 1  # Decrease lives if asteroid reaches bottom
            if lives == 0:
                game_over()  # End game if no lives left

        if pygame.sprite.spritecollide(self, bullets, True):
            self.kill()
            global score
            score += 1

# Define the Bullet class
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('ke.png')
        self.image = pygame.transform.scale(self.image, (10, 10))
        self.image.set_colorkey((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.center = player.rect.center
        self.rect.y = player.rect.y - 20
        bullets.add(self)
   
    def update(self):
        self.rect.y -= 10  
        if self.rect.y < 0:
            self.kill()

# Define the game over function
def game_over():
    screen.fill((0, 0, 0))
    font = pygame.font.SysFont(None, 100)
    text = font.render("Game Over", True, (255, 0, 0))
    text_rect = text.get_rect(center=(400, 300))
    screen.blit(text, text_rect)
    pygame.display.update()
    pygame.time.delay(2000)  # Pause for 2 seconds
    main_menu()  # Restart the game

# Define the main menu function
def main_menu():
    screen.fill((0, 0, 0))
    global lives, spawnInterval
    lives = 3
    spawnInterval = 5
    font = pygame.font.SysFont(None, 100)
    text = font.render("Press SPACE to Start", True, (255, 255, 255))
    text_rect = text.get_rect(center=(400, 300))
    screen.blit(text, text_rect)
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                return

# Start the main menu
main_menu()
strikeTimer = 0
spawnInterval = 5
spawnTimer = 0
# Main game loop
while True:
    strikeTimer -= 1/60 
    # Event handling
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    # Player movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        xSpeed -= 1
    if keys[pygame.K_d]:
        xSpeed += 1
    if keys[pygame.K_SPACE]:
        if strikeTimer <= 0:
            b = Bullet()
            strikeTimer = 0.1

    # Player speed decay
    xSpeed *= 0.96
    player.rect.x += xSpeed
    player.rect.x = max(min(player.rect.x, 730), 0)
   
    # Background and player rendering
    screen.blit(bg, (0, 0))  
    screen.blit(player.image, player.rect)
      
    # Spawn asteroids
    spawnTimer += 0.1
    if spawnTimer > spawnInterval:
        a = Aster()
        spawnTimer = 0
    spawnInterval *= 0.999  # Decrease spawn interval gradually
    if spawnTimer < 0.5:
        spawnTimer = 0.5

    # Update and draw asteroids
    asteroids.update()
    asteroids.draw(screen)

    # Update and draw bullets
    bullets.update()
    bullets.draw(screen)

    # Update and draw bonuses
    bonuses.update()
    bonuses.draw(screen)

    # Check collisions with bonuses
    bonus_collision = pygame.sprite.spritecollide(player, bonuses, True)
    for bonus in bonus_collision:
        if bonus.effect == 'life':
            lives += 1
        elif bonus.effect == 'score_multiplier':
            score *= 2

    # Create new bonuses with a certain probability
    if random.random() < 0.005:
        new_bonus = random.choice([Bonus(life_bonus_image, 'life'), Bonus(score_multiplier_image, 'score_multiplier')])
        bonuses.add(new_bonus)

    # Display score and lives
    font = pygame.font.SysFont(None, 36)
    score_text = font.render("Score: " + str(score), True, (255, 255, 255))
    screen.blit(score_text, (10, 10))
    lives_text = font.render("Lives: " + str(lives), True, (255, 255, 255))
    screen.blit(lives_text, (10, 50))

    pygame.display.update()
    pygame.time.Clock().tick(60)