import pygame
import math
import sys

pygame.init()

WIDTH, HEIGHT = 400, 400
DISPLAY_X, DISPLAY_Y = 1920, 1015

bullet_img = pygame.transform.scale(pygame.image.load('bullet.png'), (50, 100))
player_img = pygame.transform.scale(pygame.image.load('player.png'), (50, 30))
enemy_img = pygame.transform.scale(pygame.image.load('enemy.png'), (50, 30))
crosshair_img = pygame.transform.scale(pygame.image.load('crosshair.png'), (30, 30))

player_bullets = []

player_pos = [WIDTH // 2 - player_img.get_width() // 2, HEIGHT - player_img.get_height()]
enemy_pos = [WIDTH // 2 - enemy_img.get_width() // 2, 50]

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game_name")

clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y, x, y):
        super().__init__()
        #sprites right
        self.sprites_right = []
        self.in_animation = False
        self.side = True
        self.sprites_right.append(pygame.image.load("Sprites/Idle/Player_right.png"))
        self.sprites_right.append(pygame.image.load("Sprites/Running/Player_run_right_1.png"))
        self.sprites_right.append(pygame.image.load("Sprites/Running/Player_run_right_2.png"))
        self.sprites_right.append(pygame.image.load("Sprites/Running/Player_run_right_3.png"))
        self.sprites_right.append(pygame.image.load("Sprites/Running/Player_run_right_4.png"))
        self.sprites_right.append(pygame.image.load("Sprites/Running/Player_run_right_5.png"))
        self.sprites_right.append(pygame.image.load("Sprites/Running/Player_run_right_6.png"))
        self.current_sprite_right = 0
        self.image = self.sprites_right[self.current_sprite_right]

        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x,pos_y]


        #sprites left
        self.sprites_left = []
        self.in_animation_2 = False
        self.sprites_left.append(pygame.image.load("Sprites/Idle/Player_left.png"))
        self.sprites_left.append(pygame.image.load("Sprites/Running/Player_run_left_1.png"))
        self.sprites_left.append(pygame.image.load("Sprites/Running/Player_run_left_2.png"))
        self.sprites_left.append(pygame.image.load("Sprites/Running/Player_run_left_3.png"))
        self.sprites_left.append(pygame.image.load("Sprites/Running/Player_run_left_4.png"))
        self.sprites_left.append(pygame.image.load("Sprites/Running/Player_run_left_5.png"))
        self.sprites_left.append(pygame.image.load("Sprites/Running/Player_run_left_6.png"))
        self.current_sprite_left = 0
        self.image = self.sprites_left[self.current_sprite_left]

        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x,pos_y]

        self.rect = pygame.Rect(x, y, 32, 32)
        self.x = int(x)
        self.y = int(y)
        self.velX = 0
        self.velY = 0
        self.speed = 3
        self.left_pressed = False
        self.right_pressed = False
        self.down_pressed = False
        self.up_pressed = False
        self.shoot_cooldown = 0
        self.shoot_cooldown_max = 10  # Задержка между выстрелами

    def animate_right(self, start):
        self.in_animation = start

    def animate_left(self, start):
        self.in_animation_2 = start

    def sides(self, start):
        self.side = start


    def update(self):
        if self.in_animation:
            self.current_sprite_right += 0.2

            if self.current_sprite_right >= len(self.sprites_right):
                self.current_sprite_right = 0
            self.image = self.sprites_right[int(self.current_sprite_right)]
        
        if not any([self.right_pressed, self.left_pressed, self.up_pressed, self.down_pressed]) and self.side:
            self.image = self.sprites_right[0]

        if not any([self.right_pressed, self.left_pressed, self.up_pressed, self.down_pressed]) and not self.side:
            self.image = self.sprites_left[0]

        #animation left
        if self.in_animation_2:
            self.current_sprite_left += 0.2

            if self.current_sprite_left >= len(self.sprites_left):
                self.current_sprite_left = 0
            self.image = self.sprites_left[int(self.current_sprite_left)]
            
        

        self.velX = 0
        self.velY = 0
        if self.left_pressed and not self.right_pressed:
            self.velX = -self.speed
        if self.right_pressed and not self.left_pressed:
            self.velX = self.speed
        if self.up_pressed and not self.down_pressed:
            self.velY = -self.speed
        if self.down_pressed and not self.up_pressed:
            self.velY = self.speed

        self.x += self.velX
        self.y += self.velY
        self.rect = pygame.Rect(self.x, self.y, 32, 32)


        keys = pygame.key.get_pressed()
        if self.shoot_cooldown > 0:
            self.shoot_cooldown -= 1
        
        if keys[pygame.K_SPACE] and self.shoot_cooldown == 0:
            self.shoot()
            self.shoot_cooldown = self.shoot_cooldown_max

    def shoot(self):
        mouse_x, mouse_y = pygame.mouse.get_pos()
        angle = math.atan2(mouse_y - self.rect.centery, mouse_x - self.rect.centerx)
        player_bullets.append({"position": [self.rect.centerx - bullet_img.get_width() // 2,
                                            self.rect.centery - bullet_img.get_height() // 2],
                               "angle": angle})
        
screen = pygame.display.set_mode((DISPLAY_X, DISPLAY_Y))
pygame.display.set_caption("Game_name")

clock = pygame.time.Clock()

moving_sprites = pygame.sprite.Group()
player = Player(100, 100, WIDTH/2, HEIGHT/2)
moving_sprites.add(player)

def draw_elements():
    screen.blit(player_img, player_pos)
    screen.blit(enemy_img, enemy_pos)
    screen.blit(crosshair_img, pygame.mouse.get_pos())  
    for bullet in player_bullets:
        screen.blit(bullet_img, bullet["position"])

moving_sprites = pygame.sprite.Group()
player = Player(100, 100, WIDTH/2, HEIGHT/2)
moving_sprites.add(player)

while True:
    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_a:
                player.left_pressed = True
                player.animate_left(True)
                player.sides(False)
            if event.key == pygame.K_d:
                player.right_pressed = True
                player.animate_right(True)  # Start animation when right key pressed
                player.sides(True)
            if event.key == pygame.K_w:
                player.up_pressed = True
            if event.key == pygame.K_s:
                player.down_pressed = True

        # Key release events
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.left_pressed = False
                player.animate_left(False)
            if event.key == pygame.K_d:
                player.right_pressed = False
                player.animate_right(False)  # Stop animation when right key released
            if event.key == pygame.K_w:
                player.up_pressed = False
            if event.key == pygame.K_s:
                player.down_pressed = False

    for bullet in player_bullets:
        bullet["position"][0] += 5 * math.cos(bullet["angle"])
        bullet["position"][1] += 5 * math.sin(bullet["angle"])
        
        if bullet["position"][0] < 0 or bullet["position"][0] > WIDTH or bullet["position"][1] < 0 or bullet["position"][1] > HEIGHT:
            player_bullets.remove(bullet)

    screen.fill((0, 255, 255))
    moving_sprites.draw(screen)
    dt = clock.tick(60)/1000
    draw_elements()
    moving_sprites.update()
    pygame.display.flip()

