from pico2d import *
import random

class Item:
    PIXEL_PER_METER = (10.0 / 0.3)  # 10 pixel 30 cm
    RUN_SPEED_KMPH = 80.0  # Km / Hour
    RUN_SPEED_MPM = (RUN_SPEED_KMPH * 1000.0 / 60.0)
    RUN_SPEED_MPS = (RUN_SPEED_MPM / 60.0)
    RUN_SPEED_PPS = (RUN_SPEED_MPS * PIXEL_PER_METER)

    TIME_PER_ACTION = 0.5
    ACTION_PER_TIME = 1.0 / TIME_PER_ACTION
    FRAMES_PER_ACTION = 4

    def __init__(self):
        self.hpitem_image = load_image('Resource\\item.png')
        self.speeditem_image = load_image('Resource\\item.png')
        self.frame = 0
        self.x = random.randint(300,800)
        self.y = random.randint(50,300)
        self.speed = random.randint(5,20)
        self.collide_button = True

        self.run_frame = 0
        self.total_frame = 0

    def update(self, frame_time):
        self.total_frame += Item.FRAMES_PER_ACTION * Item.ACTION_PER_TIME* frame_time
        self.run_frame = int(self.total_frame + 1)%self.FRAMES_PER_ACTION
        self.x = self.x - self.speed

        if self.x <  0:
            self.x = 800
            self.speed = random.randint(5,20)
            self.y = random.randint(50,300)

    def draw(self):
        self.hpitem_image.clip_draw(66, 162, 66, 55, self.x, self.y)

    def erase(self):
        pass

    def collide_box(self):
        draw_rectangle(*self.collide_check())

    def collide_check(self):
        return self.x - 30, self.y - 40, self.x + 25, self.y + 40