import pyxel
import time

SPEED = 10

def se():
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara,  bloke_ok,count, gamen_sentaku_x, sentaku_y, sentaku_gamen_y, last_click_time, sakusei
    gamen_x = 0
    gamen_y = 0
    p_x = 50
    p_y = 290
    p_j = False
    j_speed = -60
    tikara = 5
    bloke_ok = []
    count = 1
    gamen_sentaku_x = 0
    sentaku_y = 0
    sentaku_gamen_y = 0
    last_click_time = 0
    sakusei = []
def __init__():
    #初期設定
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, game, bloke_ok, gamen_j, gool_y
    pyxel.init(800,400)
    pyxel.load("my_resource.pyxres")
    se()
    gamen_j = None
    gool_y = 0
    pyxel.run(draw, update)
def bloke_gool(s_x, s_y):
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, game, bloke_ok, check, gamen_j, gool_y
    stone_x = s_x * 50
    stone_y = s_y * 50
    gool_y = stone_y
    p = gamen_x + stone_x + 20
    pyxel.blt(p, stone_y+ 20, 1, 0, 16, 10, 10, 1, 0,5)
    if ((abs(p_x - (gamen_x + stone_x)) <= 45 and p_y - (gamen_y + stone_y) <= 0 and (p_y + j_speed) - (gamen_y + stone_y) >= -45 and p_j == True and j_speed >= 0) or
         (abs(p_x - (gamen_x + stone_x)) <= 45 and p_y - (gamen_y + stone_y) == -50 and p_j == True and j_speed >= 0)):
        gamen_j = "game_clear"
        bloke_ok.append(True)
        p_y = stone_y - 50
        p_j = False
        j_speed = -60
    if abs(p_x - (gamen_x + stone_x)) <= 45 and p_y - (gamen_y + stone_y) == -50:
        bloke_ok.append(True)
        gamen_j = "game_clear"
    else:
        bloke_ok.append(False)
def bloke_stone(s_x, s_y):
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, game, bloke_ok, check
    stone_x = s_x * 50
    stone_y = s_y * 50
    p = gamen_x + stone_x + 20
    pyxel.blt(p, stone_y+ 20, 1, 0, 0, 10, 10, 1, 0,5)
    if ((abs(p_x - (gamen_x + stone_x)) <= 45 and p_y - (gamen_y + stone_y) <= 0 and (p_y + j_speed) - (gamen_y + stone_y) >= -45 and p_j == True and j_speed >= 0) or
         (abs(p_x - (gamen_x + stone_x)) <= 45 and p_y - (gamen_y + stone_y) == -50 and p_j == True and j_speed >= 0)):
        bloke_ok.append(True)
        p_y = stone_y - 50
        p_j = False
        j_speed = -60
    if abs(p_x - (gamen_x + stone_x)) <= 45 and p_y - (gamen_y + stone_y) == -50:
        bloke_ok.append(True)
    else:
        bloke_ok.append(False)
def junp():
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, gamen_j, bloke_ok, check
    check = True in bloke_ok
    bloke_ok = []
    if pyxel.btn(pyxel.KEY_SPACE) and p_j == False and check == True:
        p_j = True
    if p_j == True:
        j_speed = j_speed + tikara
        p_y = p_y + j_speed
        if p_y >= 355:
            gamen_j = "game_over"
def u_player():
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, gamen_j, bloke_ok
    junp()
    if pyxel.btn(pyxel.KEY_RIGHT):
        gamen_x = gamen_x - SPEED
    if pyxel.btn(pyxel.KEY_LEFT):
        gamen_x = gamen_x + SPEED
        if gamen_x >= 0:
            gamen_x = 0
    if check == True:
        pass
    if not check == True:
        p_y = p_y + SPEED
        if p_y >= 355:
            gamen_j = "game_over"
def update():
    if gamen_j == "play_game":
        u_player()
    else:
        pass
def yomitori(ko_do):
    de_ta = ko_do.split(',')
    print(de_ta)
    bloke_kosuu = int(len(de_ta) / 3)
    print(bloke_kosuu)
    yomitori_de_ta = 0
    for ok in range(bloke_kosuu):
        if yomitori_de_ta < len(de_ta) and de_ta[yomitori_de_ta] == "1":
            bloke_stone(int(de_ta[yomitori_de_ta+1]), int(de_ta[yomitori_de_ta+2]))
            yomitori_de_ta = yomitori_de_ta + 3
        elif yomitori_de_ta < len(de_ta) and de_ta[yomitori_de_ta] == "2":
            bloke_gool(int(de_ta[yomitori_de_ta+1]), int(de_ta[yomitori_de_ta+2]))
            yomitori_de_ta = yomitori_de_ta + 3
def draw_bloke():
    yomitori("1,0,7,1,1,7,1,2,7,1,3,4,1,8,5,1,12,6,1,16,4,1,20,2,1,2,7,1,26,6,2,30,2")
    #yomitori("1,0,7,1,1,7,1,2,7,1,3,4,1,4,4,1,7,5,1,8,5,1,12,6,1,13,6,1,16,4,1,17,4,2,20,6")
def draw_start():
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, bloke_ok, gamen_j, count, gool_y, gamen_sentaku_x, sentaku_y, sentaku_gamen_y, last_click_time, sakusei
    if gamen_j == None:
        #pyxel.blt()
        pyxel.blt(120, 270, 1, 16, 16, 10, 10, 1, 0, 25)
        pyxel.blt(620, 250, 0, 5, 159, 29, 30, 7, 0, 8)
        if pyxel.btnp(pyxel.KEY_DOWN):
            count = count + 1
            if count > 3:
                count = 3
        if pyxel.btnp(pyxel.KEY_UP):
            count = count - 1
            if count < 1:
                count = 1
        if count == 1:
            pyxel.blt(495, 188, 0, 0, 161, 5, 7, 7, 0, 8)
            if pyxel.btnp(pyxel.KEY_SPACE):
                se()
                gamen_j = "play_game"
        if count == 2:
            pyxel.blt(495, 268, 0, 0, 161, 5, 7, 7, 0, 8)
            if pyxel.btnp(pyxel.KEY_SPACE):
                gamen_j = "make"
        if count == 3:
            pyxel.blt(495, 356, 0, 0, 161, 5, 7, 7, 0, 8)
            if pyxel.btnp(pyxel.KEY_SPACE):
                pyxel.quit()
    elif gamen_j == "game_over":
        draw_bloke()
        pyxel.rect(530, 228, 290, 185, 9)
        pyxel.blt(670, 300, 0, 6, 190, 44, 32, 7, 0, 5)
        pyxel.blt(p_x + 20, 350+20, 1, 16, 0, 10, 10, 1, 0, 5)
        pyxel.blt(250, 150, 0, 0, 240, 28, 16, 1, -5, 16)
        if pyxel.btnp(pyxel.KEY_DOWN):
            count = count + 1
            if count > 3:
                count = 3
        if pyxel.btnp(pyxel.KEY_UP):
            count = count - 1
            if count < 1:
                count = 1
        if count == 1:
            pyxel.blt(555, 268, 0, 0, 161, 5, 7, 7, 0, 5)
            if pyxel.btnp(pyxel.KEY_SPACE):
                se()
                gamen_j = "play_game"
        if count == 2:
            pyxel.blt(555, 315, 0, 0, 161, 5, 7, 7, 0, 5)
            if pyxel.btnp(pyxel.KEY_SPACE):
                se()
                gamen_j = None
        if count == 3:
            pyxel.blt(555, 370, 0, 0, 161, 5, 7, 7, 0, 5)
            if pyxel.btnp(pyxel.KEY_SPACE):
                pyxel.quit()
    elif gamen_j == "game_clear":
        draw_bloke()
        pyxel.rect(530, 228, 290, 185, 9)
        pyxel.blt(670, 300, 0, 6, 190, 44, 32, 7, 0, 5)
        pyxel.blt(70,gool_y-50+20, 1, 16, 16, 10, 10, 1, 0, 5)
        pyxel.blt(240, 130, 0, 30, 240, 33, 15, 1, 0, 15)
        if pyxel.btnp(pyxel.KEY_DOWN):
            count = count + 1
            if count > 3:
                count = 3
        if pyxel.btnp(pyxel.KEY_UP):
            count = count - 1
            if count < 1:
                count = 1
        if count == 1:
            pyxel.blt(555, 268, 0, 0, 161, 5, 7, 7, 0, 5)
            if pyxel.btnp(pyxel.KEY_SPACE):
                se()
                gamen_j = "play_game"
        if count == 2:
            pyxel.blt(555, 315, 0, 0, 161, 5, 7, 7, 0, 5)
            if pyxel.btnp(pyxel.KEY_SPACE):
                se()
                gamen_j = None
        if count == 3:
            pyxel.blt(555, 370, 0, 0, 161, 5, 7, 7, 0, 5)
            if pyxel.btnp(pyxel.KEY_SPACE):
                pyxel.quit()
    elif gamen_j == "make":
        make_x = 0
        make_y = 0
        for okk in range(16):
            for ook in range(8):
                pyxel.blt(make_x+20, make_y+20,1,0,48,10,10,0,0,5)
                make_y = make_y + 50
            make_y = 0
            make_x = make_x + 50
        '''
        if pyxel.btnp(pyxel.KEY_RIGHT):
            gamen_sentaku_x = gamen_sentaku_x + 50
            if gamen_sentaku_x > 750:
                gamen_sentaku_x = 0
        if pyxel.btnp(pyxel.KEY_LEFT):
            gamen_sentaku_x = gamen_sentaku_x - 50
            if gamen_sentaku_x < 0:
                gamen_sentaku_x = 750
        if pyxel.btnp(pyxel.KEY_UP):
            sentaku_gamen_y = sentaku_gamen_y - 50
            if sentaku_gamen_y < 0:
                sentaku_gamen_y = 350
        if pyxel.btnp(pyxel.KEY_DOWN):
            sentaku_gamen_y = sentaku_gamen_y + 50
            if sentaku_gamen_y > 350:
                sentaku_gamen_y = 0
        if pyxel.btn(pyxel.KEY_Q):
            gamen_j = None
        if pyxel.btn(pyxel.KEY_RETURN):
            print("a")
        pyxel.blt(gamen_sentaku_x+20,sentaku_gamen_y+20,1,16,32,10,10,1,0,5)
        '''
        pyxel.blt(330,160,0,80,14,114,13,1,0,5)
        pyxel.blt(515,179,0,32,112,9,9,1,0,3.5)
        if pyxel.btn(pyxel.KEY_RETURN):
            gamen_j = None
def draw():
    global gamen_x, gamen_y, p_x, p_y, p_j, j_speed, tikara, bloke_ok, gamen_j
    pyxel.cls(9)
    if gamen_j == None:
        pyxel.blt(p_x + 20, 370, 1, 16, 0, 10, 10, 1, 0,5)
    if not gamen_j == "play_game":
        draw_start()
    else:
        if gamen_j == "play_game":
            pyxel.blt(p_x + 20, p_y+ 20, 1, 16, 16, 10, 10, 1, 0, 5)
            draw_bloke()
__init__()