import pyxel,random

pyxel.init(128, 128, title="NDC")
pyxel.load("theme2.pyxres")
t= [[0,64,24,16,16]]
btnp = [0,0]
maps = {}
points = 20
player_chunck = [0,0]
p = 0

def generate():
    global p
    print(p)
    stone = (10*20)+(p) # %.
    hdirt = (p*5)
    heart =  (2*10)-(p/20) # %.
    gheart = (0.5*10)-(p/20) # %.
    dirt = 1000-heart-stone-gheart
    choices = []
    for i in range(int(dirt)):
        choices.append(1)
    for i in range(int(stone)):
        choices.append(0)
    for i in range(int(heart)):
        choices.append(-1)
    for i in range(int(gheart)):
        choices.append(-2)
    for i in range(int(hdirt)):
        choices.append(2)

    return [[choices[random.randint(0,len(choices)-1)] for i in range(8)] for y in range(8)]

def new_chuncks():
    global player_chunck,maps
    
    for i in range(-1,2):
        for j in range(-1,2):
            if (player_chunck[0]+i , player_chunck[1]+j) not in maps:
                maps[(player_chunck[0]+i,player_chunck[1]+j)] = generate()

def mouve():
    global px,py,btnp,maps,player_chunck,points,p
    if ((btnp[0]!=0) and (btnp[1]==0))or((btnp[0]==0) and (btnp[1]!=0)):
        if (px+4+btnp[1] > 7) and (py+4+btnp[0] <= 7):
            t =maps[(player_chunck[0]+1,player_chunck[1])][px+4-8+btnp[1]][py+4+btnp[0]]
            if t == 0:btnp = [0,0];return
            if t == 1:points -= 1
            if t == 2:points -= 2
            if t == -1:points+=10;p+=1
            if t == -2:points+=15;p+=1
            maps[(player_chunck[0]+1,player_chunck[1])][px+4-8+btnp[1]][py+4+btnp[0]] =5
            
        elif (py+4+btnp[0] > 7) and (px+4+btnp[1] <= 7):
            t =maps[(player_chunck[0],player_chunck[1]+1)][px+4+btnp[1]][py+4-8+btnp[0]]
            if t == 0:btnp = [0,0];return
            if t == 1:points -= 1
            if t == 2:points -= 2
            if t == -1:points+=10;p+=1
            if t == -2:points+=15;p+=1
            maps[(player_chunck[0],player_chunck[1]+1)][px+4+btnp[1]][py+4-8+btnp[0]] =5
        elif (py+4+btnp[0] > 7) and (px+4+btnp[1] > 7):
            t = maps[(player_chunck[0]+1,player_chunck[1]+1)][px+4-8+btnp[1]][py+4-8+btnp[0]]
            if t == 0:btnp = [0,0];return
            if t == 1:points -= 1
            if t == 2:points -= 2
            if t == -1:points+=10;p+=1
            if t == -2:points+=15;p+=1
            maps[(player_chunck[0]+1,player_chunck[1]+1)][px+4-8+btnp[1]][py+4-8+btnp[0]] = 5
        else:
            t= maps[(player_chunck[0],player_chunck[1])][px+4+btnp[1]][py+4+btnp[0]]
            if t == 0:btnp = [0,0];return
            if t == 1:points -= 1
            if t == 2:points -= 2
            if t == -1:points+=10;p+=1
            if t == -2:points+=15;p+=1
            maps[(player_chunck[0],player_chunck[1])][px+4+btnp[1]][py+4+btnp[0]] =5
        if (btnp[0]!=0) and (btnp[1]==0):
            py += btnp[0]
            if py > 7: py = 0 ;  player_chunck[1] += 1 
            if py < 0: py = 7 ;  player_chunck[1] += -1 
        if (btnp[1]!=0) and (btnp[0]==0):
            px += btnp[1]
            if px > 7: px = 0 ;  player_chunck[0] += 1
            if px < 0: px = 7 ;  player_chunck[0] += -1 
        print(maps)
    btnp = [0,0]



def update():
    global px,py,btnp,maps,player_chunck,points

    new_chuncks()
    #if pyxel.btn(pyxel.MOUSE_BUTTON_LEFT)
    
    if pyxel.btnp(pyxel.KEY_W) or pyxel.btnp(pyxel.KEY_Z):
        btnp[0]=-1
    if pyxel.btnp(pyxel.KEY_S) :
        btnp[0]=1
    if pyxel.btnp(pyxel.KEY_A) or pyxel.btnp(pyxel.KEY_Q) :
        btnp[1]=-1
    if pyxel.btnp(pyxel.KEY_D):
        btnp[1]=1
        
    mouve()
        #if py < 0 : py = 15 ; player_chunck[1] -= 1
        #if py > 15: py = 0; player_chunck[1] += 1

px = 0
py = 0
#pyxel.play(ch=0,snd=0,loop=True)
def draw():
    global t,px,py,player_chunck,maps,points
    
    new_chuncks()
    pyxel.cls(0)
    
    ma = maps[(player_chunck[0],player_chunck[1])]
    for x in range(8):
        for y in range(8):
            pos = (x*16)-(px*16),(y*16)-(py*16)
            if ma[x][y]==1:
                
                pyxel.blt(pos[0],pos[1],t[0][0],t[0][1],t[0][2],t[0][3],t[0][4])
            if ma[x][y]==0:
                pyxel.blt(pos[0],pos[1],0,64,40,16,16)
            if ma[x][y]==-1:
                pyxel.blt(pos[0],pos[1],0,48,40,16,16)
            if ma[x][y]==-2:
                pyxel.blt(pos[0],pos[1],0,48,56,16,16)
            if ma[x][y]==2:
                pyxel.blt(pos[0],pos[1],0,80,40,16,16)
    mapc = (player_chunck[0],player_chunck[1]+1)
    ma = maps[mapc]
    chunck_pos = [0,128]
    for x in range(8):
        for y in range(8):
            pos = (chunck_pos[0])+(x*16)-(px*16),chunck_pos[1]+(y*16)-(py*16)
            if ma[x][y]==1:
                
                pyxel.blt(pos[0],pos[1],t[0][0],t[0][1],t[0][2],t[0][3],t[0][4])
            if ma[x][y]==0:
                pyxel.blt(pos[0],pos[1],0,64,40,16,16)
            if ma[x][y]==-1:
                pyxel.blt(pos[0],pos[1],0,48,40,16,16)
            if ma[x][y]==-2:
                pyxel.blt(pos[0],pos[1],0,48,56,16,16)
            if ma[x][y]==2:
                pyxel.blt(pos[0],pos[1],0,80,40,16,16)
    mapc = (player_chunck[0]+1,player_chunck[1])
    ma = maps[mapc]
    chunck_pos = [128,0]
    for x in range(8):
        for y in range(8):
            pos = (chunck_pos[0])+(x*16)-(px*16),chunck_pos[1]+(y*16)-(py*16)
            if ma[x][y]==1:
                pyxel.blt(pos[0],pos[1],t[0][0],t[0][1],t[0][2],t[0][3],t[0][4])
            if ma[x][y]==0:
                pyxel.blt(pos[0],pos[1],0,64,40,16,16)
            if ma[x][y]==-1:
                pyxel.blt(pos[0],pos[1],0,48,40,16,16)
            if ma[x][y]==-2:
                pyxel.blt(pos[0],pos[1],0,48,56,16,16)
            if ma[x][y]==2:
                pyxel.blt(pos[0],pos[1],0,80,40,16,16)
    mapc = (player_chunck[0]+1,player_chunck[1]+1)
    ma = maps[mapc]
    chunck_pos = [128,128]
    for x in range(8):
        for y in range(8):
            pos = (chunck_pos[0])+(x*16)-(px*16),chunck_pos[1]+(y*16)-(py*16)
            if ma[x][y]==1:
                pyxel.blt(pos[0],pos[1],t[0][0],t[0][1],t[0][2],t[0][3],t[0][4])
            if ma[x][y]==0:
                pyxel.blt(pos[0],pos[1],0,64,40,16,16)
            if ma[x][y]==-1:
                pyxel.blt(pos[0],pos[1],0,48,40,16,16)
            if ma[x][y]==-2:
                pyxel.blt(pos[0],pos[1],0,48,56,16,16)
            if ma[x][y]==2:
                pyxel.blt(pos[0],pos[1],0,80,40,16,16)
            if ma[x][y] == 5:
                print("truc")
                
    
    pyxel.blt(4*16,4*16,0,0,56,16,16)
    pyxel.text(10,10,"SCORE: "+str(points),15)
    if points <= 0:
        pyxel.cls(0)
        pyxel.text(50,50,"GAME OVER",15)

pyxel.run(update, draw)