import pygame
import sys

pygame.init()

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

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]



        #movement
        self.rect = pygame.Rect(x, y, 32, 32)
        self.x = int(x)
        self.y = int(y)
        self.velX = 0
        self.velY = 0
        self.left_pressed = False
        self.right_pressed = False
        self.down_pressed = False
        self.up_pressed = False
        self.speed = 3

    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):
        #animation right
        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)]
            
        

        

        #movement
        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)



        















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)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # Key press events
        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)  
                player.sides(True)
            if event.key == pygame.K_w:
                player.up_pressed = True
            if event.key == pygame.K_s:
                player.down_pressed = True
            if event.key == pygame.K_a and event.key == pygame.K_w:
                player.animate_left(True)
            if event.key == pygame.K_w and event.key == pygame.K_d:
                player.animate_right(True)
            if event.key == pygame.K_d and event.key == pygame.K_s:
                player.animate_right(True)
            if event.key == pygame.K_s and event.key == pygame.K_a:
                player.animate_left(True)
            if event.key == pygame.K_w and player.side == False:
                player.animate_left(True)
            if event.key == pygame.K_w and player.side == True:
                player.animate_right(True)
            if event.key == pygame.K_s and player.side == False:
                player.animate_left(True)
            if event.key == pygame.K_s and player.side == True:
                player.animate_right(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)  
            if event.key == pygame.K_w:
                player.up_pressed = False
                player.animate_left(False)
                player.animate_right(False)
            if event.key == pygame.K_s:
                player.down_pressed = False
                player.animate_left(False)
                player.animate_right(False)
                
             



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