import pyxel
w=50   
map=[[0 for i in range(w)] for k in range(w)]
map2=[[0 for i in range(w)] for k in range(w)]

def compt(x,y):
    if x==0 and y==0:
        pos=[[1,0],[1,1],[0,1]]
        a=0
        for [f,g] in pos:
            if map[x+f][y+g]==1:
                a+=1
        return a
    elif x==w-1 and y==w-1:
        a=0
        pos=[[-1,0],[-1,-1],[0,-1]]
        for [f,g] in pos:
            if map[x+f][y+g]==1:
                a+=1
        return a
    elif x==0 and y==w-1:
        a=0
        pos=[[0,-1],[1,-1],[-1,0]]
        for [f,g] in pos:
            if map[x+f][y+g]==1:
                a+=1
        return a
    elif x==w-1 and y==0:
        a=0
        pos=[[0,-1],[-1,1],[-1,0]]
        for [f,g] in pos:
            if map[x+f][y+g]==1:
                a+=1
        return a
    elif x==0 and y!=0:
        a=0
        pos=[[0,-1],[1,-1],[0,1],[1,0],[1,1]]
        for [f,g] in pos:
            if map[x+f][y+g]==1:
                a+=1
        return a
    elif x==w-1 and y!=0:
        a=0
        pos=[[0,-1],[1,-1],[0,1],[1,0],[1,1]]
        for [f,g] in pos:
            if map[x-f][y-g]==1:
                a+=1
        return a
    elif y==0 and x!=0:
        a=0
        pos=[[0,-1],[1,-1],[0,1],[1,0],[1,1]]
        for [g,f] in pos:
            if map[x+f][y+g]==1:
                a+=1
        return a
    elif y==w-1 and x!=0:
        a=0
        pos=[[0,-1],[1,-1],[0,1],[1,0],[1,1]]
        for [g,f] in pos:
            if map[x-f][y-g]==1:
                a+=1
        return a
    else:
        a=0
        for i in range(x-1,x+2):
            for k in range(y-1,y+2):
                if map[i][k]==1 and not (i==x and k==y):
                    a+=1
        return a

class Jeu:
    def __init__(self):
        pyxel.init(w*8,w*8,title="Game of Life")
        pyxel.load("res.pyxres")
        self.g=0
        self.play=False
        self.start=0
        self.Mstart=0
        self.Vstart=0
        self.v=30
        pyxel.mouse(True)
        pyxel.run(self.update,self.draw)

    def update(self):
        if self.play:
            if pyxel.btn(pyxel.KEY_SPACE) and pyxel.frame_count-self.start>4:
                self.start=pyxel.frame_count
                self.play=False
            if pyxel.btn(pyxel.KEY_RIGHT) and pyxel.frame_count-self.Vstart>3 and self.v-1!=0:
                self.Vstart=pyxel.frame_count
                self.v-=1
            if pyxel.btn(pyxel.KEY_LEFT) and pyxel.frame_count-self.Vstart>3 and self.v+1!=31:
                self.Vstart=pyxel.frame_count
                self.v+=1
            if pyxel.frame_count%self.v==0:
                self.g+=1
                for i in range(w):
                    for k in range(w):
                        a=pyxel.tilemap(0).pget(i,k)
                        if a==(0,0):
                            map2[i][k]=1
                        else:
                            map2[i][k]=0
                for x in range(w):
                    for y in range(w):
                        nb=compt(x,y)
                        if map[x][y]==0 and nb==3:
                            map2[x][y]=1
                        elif map[x][y]==1 and (nb==2 or nb==3):
                            map2[x][y]=1
                        elif map[x][y]==1 and (nb==0 or nb==1 or nb>3):
                            map2[x][y]=0
                        else:
                            map2[x][y]=map[x][y]

                for i in range(w):
                    for k in range(w):
                        map[i][k]=map2[i][k]
        else:
            if pyxel.btn(pyxel.KEY_RIGHT) and pyxel.frame_count-self.Vstart>3 and self.v-1!=0:
                self.Vstart=pyxel.frame_count
                self.v-=1
            if pyxel.btn(pyxel.KEY_LEFT) and pyxel.frame_count-self.Vstart>3 and self.v+1!=31:
                self.Vstart=pyxel.frame_count
                self.v+=1
            
            if pyxel.btn(pyxel.KEY_SPACE) and pyxel.frame_count-self.start>4:
                self.start=pyxel.frame_count
                self.play=True
            if pyxel.btn(pyxel.KEY_C):
                for i in range(w):
                    for k in range(w):
                        tilemap(0).pset(i//8,k//8,(0,1))
                        map[i][k]=0
                        map2[i][k]=0


            if pyxel.btn(pyxel.MOUSE_BUTTON_LEFT) and pyxel.frame_count-self.Mstart>3:
                self.Mstart=pyxel.frame_count
                X,Y=pyxel.mouse_x,pyxel.mouse_y
                tile=pyxel.tilemap(0).pget(X//8,Y//8)
                if tile==(0,0):
                    pyxel.tilemap(0).pset(X//8,Y//8,(0,1))
                    map[X//8][Y//8]=0
                elif tile==(0,1):
                    pyxel.tilemap(0).pset(X//8,Y//8,(0,0))
                    map[X//8][Y//8]=1

            if pyxel.btn(pyxel.KEY_RIGHT) and pyxel.frame_count-self.Vstart>3 and self.v-1!=0:
                self.Vstart=pyxel.frame_count
                self.v-=1
            if pyxel.btn(pyxel.KEY_LEFT) and pyxel.frame_count-self.Vstart>3 and self.v+1!=31:
                self.Vstart=pyxel.frame_count
                self.v+=1
            if pyxel.btn(pyxel.KEY_C):
                for i in range(w):
                    for k in range(w):
                        pyxel.tilemap(0).pset(i,k,(0,1))
                        map[i][k]=0
                        map2[i][k]=0

    def draw(self):
        pyxel.cls(1)
        pyxel.bltm(0,0,0,0,0,w*8,w*8)
        if self.play:
            pyxel.blt(1,1,0,8,0,4,5,0)

            for i in range(w):
                for k in range(w):
                    if map[i][k]==1:
                        pyxel.tilemap(0).pset(i,k,(0,0))
                    else:
                        pyxel.tilemap(0).pset(i,k,(0,1))
        else:
            pyxel.blt(1,1,0,12,0,4,5,0)
        pyxel.text(6,1,str(self.g),7)
        pyxel.text(1,8,"V:"+str(31-self.v),7)


Jeu()