class sea(object):
    tileimg = [imload(os.path.join(tile_path, str(17) + '.png')) for x in range(1, 3)]

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.left_movement = True
        self.is_visible = True
        self.hitbox = (self.x, self.y, self.width, self.height)

    def draw_tile(self, win):
        if self.is_visible:
            if self.left_movement == True:
                self.x -= 3
            if self.x < -self.width:
                self.is_visible = False
            for i in range (1):
                picture = self.tileimg[i]
                picture = pygame.transform.scale(picture, (self.width, self.height))
                win.blit(picture , (self.x+i*self.width , self.y))
            self.hitbox = (self.x, self.y, self.width, self.height)

            #pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    def collide(self, rect):
        midpoint = rect[0] + (rect[2]//2)
        if midpoint > self.hitbox[0] and midpoint < self.hitbox[0] + self.hitbox[2]:
            #print("X clear", rect[1] + rect[3], self.hitbox[1])
            if rect[1] + rect[3] >= self.hitbox[1]:
                #print("Y clear")
                return True
        return False
class bird(object):
    tileimg = [imload(os.path.join(obj_path, "tile00" + str(x) + '.png')) for x in range(0, 9)]
    tileimg2 = [imload(os.path.join(obj_path, "tile00" + str(8-x) + '.png')) for x in range(0, 9)]

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.left_movement = True
        self.is_visible = True
        self.framecount = 0
        self.hitbox = (self.x, self.y+20, self.width, self.height-30)

    def draw_tile(self, win):
        if self.is_visible:
            if self.left_movement == True:
                self.x -= 5
            if self.x < -self.width:
                self.is_visible = False
            if(self.framecount%54<27):
                picture = self.tileimg[(self.framecount//3)%9]
            else:
                picture = self.tileimg2[(self.framecount//3)%9]
            picture = pygame.transform.scale(picture, (self.width, self.height))
            win.blit(picture , (self.x, self.y))
            self.hitbox = (self.x, self.y+20, self.width, self.height-30)
            self.framecount+=1

            #pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    def collide(self, rect):
        rect_right = rect[0] + rect[2]
        rect_bottom = rect[1] + rect[3]
        self_right = self.hitbox[0] + self.hitbox[2]
        self_bottom = self.hitbox[1] + self.hitbox[3]

        if (rect[0] >= self.hitbox[0] and rect[0] <= self_right) or (rect_right >= self.hitbox[0] and rect_right <= self_right):
            #print("X clear", rect[1] + rect[3], self.hitbox[1])
            if (rect[1] >= self.hitbox[1] and rect[1] <= self_bottom) or (rect_bottom >= self.hitbox[1] and rect_bottom <= self_bottom):
                #print("BIRD Y clear")
                return True
        return False
class land(object):
    tileimg = [imload(os.path.join(tile_path, str(x) + '.png')) for x in range(1, 4)]

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.left_movement = True
        self.is_visible = True
        self.hitbox = ()

    def draw_tile(self, win):
        if self.is_visible:
            if self.left_movement == True:
                self.x -= 3
            if self.x < -self.width:
                self.is_visible = False
            for i in range (3):
                picture = self.tileimg[i]
                picture = pygame.transform.scale(picture, (int(self.width/3), self.height))
                win.blit(picture , (self.x+i*int(self.width/3), self.y))
            self.hitbox = (self.x, self.y, self.width, self.height)
class mushroom(object):
    tileimg = [imload(os.path.join(obj_path, "Mushroom_" + str(x) + '.png')) for x in range(1, 3)]

    def __init__(self, x, y, width, height, mushroomtype):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.mushroomtype = mushroomtype
        self.left_movement = True
        self.is_visible = True
        self.hitbox = (self.x, self.y, self.width, self.height)

    def draw_tile(self, win):
        if self.is_visible:
            if self.left_movement == True:
                self.x -= 3
            if self.x < -self.width:
                self.is_visible = False
            picture = self.tileimg[self.mushroomtype]
            picture = pygame.transform.scale(picture, (self.width, self.height))
            win.blit(picture , (self.x, self.y))
            self.hitbox = (self.x, self.y, self.width, self.height)

            #pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
    def collide(self, rect):
        rect_right = rect[0] + rect[2]
        rect_bottom = rect[1] + rect[3]
        self_right = self.hitbox[0] + self.hitbox[2]
        self_bottom = self.hitbox[1] + self.hitbox[3]

        if (rect[0] >= self.hitbox[0] and rect[0] <= self_right) or (rect_right >= self.hitbox[0] and rect_right <= self_right) or (rect[0] < self.hitbox[0] and rect_right >= self_right):
            if (rect[1] >= self.hitbox[1] and rect[1] <= self_bottom) or (rect_bottom >= self.hitbox[1] and rect_bottom <= self_bottom)or (rect[1] < self.hitbox[1] and rect_bottom >= self_bottom):
                #self.is_visible = False
                return True
        return False
pygame.init()

W, H = 800, 480
win = pygame.display.set_mode((W,H))
pygame.display.set_caption('PeraNaiChill')

bg_path = "data/tileset_1(main)/BG/"
char_path = "data/character"
obj_path = "data/tileset_1(main)/Object/"
tile_path = "data/tileset_1(main)/Tiles/"

target = 50
lives = 5

bg = imload(os.path.join(bg_path,'BG.png')).convert()
bgX = 0
bgX2 = bg.get_width()

class players(object):
    run = [pygame.image.load(os.path.join(char_path, "Run__00" + str(x) + '.png')) for x in range(1, 10)]
    still = [pygame.image.load(os.path.join(char_path, "Idle__00" + str(x) + '.png')) for x in range(1, 10)]
    death = [pygame.image.load(os.path.join(char_path, "Dead__00" + str(x) + '.png')) for x in range(1, 10)]
    jump = [pygame.image.load(os.path.join(char_path, "Jump__00" + str(x) + '.png')) for x in range(1, 10)]
    slide = [pygame.image.load(os.path.join(char_path, "slide__00" + str(x) + '.png')) for x in range(1, 10)]

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height