import pyxel,random,math
W,H,FPS=256,192,30
LT,LB=90,158
LM=(LT+LB)//2
JO,DV,RS=30,26,2.5
PAL={0:(5,20,45),1:(10,60,100),2:(15,100,140),3:(25,150,175),4:(50,190,200),
     5:(200,230,245),6:(160,210,235),7:(90,160,210),8:(150,200,240),
     9:(255,200,50),10:(220,90,20),11:(180,50,20),12:(60,130,60),
     13:(90,90,100),14:(230,230,240),15:(255,255,255)}
C={"deep":0,"wave_dark":1,"wave_mid":2,"wave_face":3,"wave_lite":4,"foam":5,
   "spray":6,"sky_low":7,"sky_hi":8,"yellow":9,"orange":10,"red":11,
   "green":12,"grey":13,"white_sh":14,"white":15}
def sp(): [pyxel.colors.__setitem__(i,(r<<16)|(g<<8)|b) for i,(r,g,b) in PAL.items()]

class Wave:
    def __init__(self): self.o=0.0
    def update(self,s): self.o=(self.o+s)%W
    def draw(self):
        o=self.o; sh=LT-10
        for y in range(sh): pyxel.line(0,y,W-1,y,C["sky_hi"] if 0.5>y/(sh-1) else C["sky_low"])
        sx,sy=W-38,18
        for r in [14,11,8]: pyxel.circ(sx,sy,r,[C["sky_hi"],C["spray"],C["foam"]][14-r if 14>=r else 0] if r==14 else C["spray"] if r==11 else C["foam"])
        pyxel.circ(sx,sy,6,C["yellow"]); pyxel.circ(sx,sy,4,C["white"])
        for a in range(0,360,45):
            rd=math.radians(a); rx,ry=int(sx+math.cos(rd)*17),int(sy+math.sin(rd)*17)
            pyxel.line(rx,ry,int(sx+math.cos(rd)*20),int(sy+math.sin(rd)*20),C["yellow"])
        for x in range(W):
            s=(x+o*0.3)%W; wt=math.sin(s*.035)*7+math.sin(s*.071)*3+math.sin(s*.12)*2
            wy=int(sh+wt)
            for y in range(wy,LT+1):
                f=(y-wy)/max(1,LT-wy)
                pyxel.pset(x,y,[C["wave_dark"],C["wave_mid"],C["wave_face"],C["wave_lite"]][0 if .25>f else 1 if .55>f else 2 if .8>f else 3])
        for x in range(W):
            s=(x+o*.55)%W; cr=int(sh-2+math.sin(s*.05)*4+math.sin(s*.11)*2)
            for dy in range(-5,4):
                yy=cr+dy
                if yy>=0 and H>yy: pyxel.pset(x,yy,C["foam"] if .45>abs(dy)/5 else C["spray"])
        for x in range(W):
            s=(x+o*.85)%W
            for y in range(LT,LB+1):
                f=(y-LT)/(LB-LT); rp=math.sin(s*.06+f*9+o*.08)*1.8+math.sin(s*.13+f*4)*.9
                pyxel.pset(x,y,C["wave_lite"] if rp>1.4 else C["wave_face"] if rp>.5 or .35>f else C["wave_mid"] if .65>f else C["wave_dark"])
        bs=int(o*.4)%W
        for i in range(50):
            bx=(bs+i)%W; gl=int(LT+14+math.sin(bx*.07)*5)
            pyxel.pset(bx,gl,C["foam"]); pyxel.pset(bx,gl+1,C["wave_lite"])
        for i in range(7):
            s=(int(o*1.1)+i*38)%W; pyxel.line(s,LT+10+i*9,(s+12+i*4)%W,LT+10+i*9,C["foam"])
        for y in range(LB+1,H):
            f=(y-LB)/max(1,H-LB); pyxel.line(0,y,W-1,y,C["wave_dark"] if .3>f else C["deep"] if .6>f else C["sky_low"])
        for i in range(4): pyxel.line((int(o*1.4)+i*64)%W,LB+2,(int(o*1.4)+i*64+20)%W,LB+2,C["spray"])

def joe(cx,cy,fr):
    b=int(math.sin(fr*.22)*1.5); x,y=int(cx),int(cy)+b
    pyxel.rect(x-16,y+9,33,5,C["green"]); pyxel.rect(x-14,y+10,29,3,C["yellow"])
    pyxel.line(x-3,y+5,x-3,y+9,C["orange"]); pyxel.line(x+3,y+5,x+3,y+9,C["orange"])
    pyxel.line(x-6,y+9,x,y+9,C["orange"]); pyxel.line(x+1,y+9,x+6,y+9,C["orange"])
    pyxel.circ(x,y+1,8,C["orange"]); pyxel.circ(x,y+2,5,C["yellow"])
    pyxel.tri(x-9,y,x-1,y-3,x-10,y+6,C["orange"]); pyxel.tri(x+9,y,x+1,y-3,x+10,y+6,C["orange"])
    pyxel.rect(x-2,y-10,5,6,C["orange"]); pyxel.circ(x+1,y-14,7,C["orange"]); pyxel.circ(x+2,y-14,4,C["yellow"])
    pyxel.circ(x-1,y-20,3,C["red"]); pyxel.circ(x+2,y-22,2,C["red"]); pyxel.circ(x+4,y-19,2,C["red"]); pyxel.circ(x+4,y-11,2,C["red"])
    pyxel.tri(x+5,y-15,x+11,y-13,x+5,y-11,C["yellow"])
    pyxel.rect(x-5,y-17,5,3,C["red"]); pyxel.rect(x+1,y-17,5,3,C["red"])
    pyxel.rect(x-4,y-16,3,2,C["deep"]); pyxel.rect(x+2,y-16,3,2,C["deep"])

def cody(cx,cy,fr):
    b=int(math.sin(fr*.22)*1.5); x,y=int(cx),int(cy)+b
    pyxel.rect(x-16,y+9,33,5,C["orange"]); pyxel.line(x-14,y+11,x+14,y+11,C["wave_face"])
    pyxel.tri(x-3,y+9,x-8,y+12,x+1,y+12,C["orange"]); pyxel.tri(x+3,y+9,x+8,y+12,x-1,y+12,C["orange"])
    pyxel.circ(x,y+1,9,C["deep"]); pyxel.circ(x,y+2,6,C["white"]); pyxel.circ(x,y+3,4,C["foam"])
    pyxel.tri(x-10,y,x-2,y-4,x-11,y+7,C["deep"]); pyxel.tri(x+10,y,x+2,y-4,x+11,y+7,C["deep"])
    pyxel.rect(x-3,y-10,6,5,C["deep"]); pyxel.rect(x-1,y-10,3,5,C["white"])
    pyxel.circ(x,y-14,8,C["deep"]); pyxel.circ(x+1,y-14,5,C["white"])
    for dx,dy2 in [(-7,-20),(-6,-21),(-5,-22)]: pyxel.line(x+dx,y+dy2,x-dx//3,y+dy2+2,C["yellow"])
    for dx,dy2 in [(7,-20),(6,-21),(5,-22)]: pyxel.line(x+dx,y+dy2,x+dx//3+1,y+dy2+2,C["yellow"])
    pyxel.tri(x-2,y-13,x+2,y-13,x,y-10,C["orange"]); pyxel.tri(x-2,y-14,x+2,y-14,x,y-12,C["yellow"])
    pyxel.circ(x-3,y-16,2,C["white"]); pyxel.circ(x+3,y-16,2,C["white"])
    pyxel.pset(x-3,y-16,C["deep"]); pyxel.pset(x+3,y-16,C["deep"])
    pyxel.pset(x-2,y-17,C["white"]); pyxel.pset(x+4,y-17,C["white"])
    pyxel.circ(x,y-5,2,C["red"]); pyxel.pset(x,y-5,C["orange"])

class Obs:
    KS=["shark","rock","ship"]
    def __init__(self,spd):
        self.kind=random.choice(self.KS); self.x=float(W+16); self.spd=spd; self._geo()
    def _geo(self):
        if self.kind=="shark": self.y=LM-5; self.w=26; self.hb=12
        elif self.kind=="rock": self.y=LB-28; self.w=30; self.hb=28
        else: self.y=LT-2; self.w=36; self.hb=40
    def update(self): self.x-=self.spd
    def off(self): return 0>self.x+self.w
    def rect(self): return int(self.x),int(self.y),self.w,self.hb
    def draw(self):
        x,y=int(self.x),int(self.y)
        if self.kind=="shark": self._shark(x,y)
        elif self.kind=="rock": self._rock(x,y)
        else: self._ship(x,y)
    def _shark(self,x,y):
        pyxel.tri(x,y+5,x+26,y+4,x+13,y,C["grey"]); pyxel.tri(x+1,y+3,x+24,y+3,x+13,y,C["wave_dark"])
        pyxel.tri(x,y+5,x+26,y+6,x+13,y+11,C["grey"])
        pyxel.tri(x+3,y+6,x+23,y+6,x+13,y+10,C["foam"]); pyxel.tri(x+5,y+6,x+21,y+6,x+13,y+9,C["white"])
        pyxel.line(x+2,y+5,x+24,y+5,C["wave_mid"])
        pyxel.tri(x+9,y+1,x+16,y+1,x+8,y-8,C["grey"]); pyxel.tri(x+10,y+2,x+15,y+2,x+9,y-6,C["wave_dark"])
        pyxel.pset(x+8,y-8,C["foam"])
        pyxel.tri(x+5,y+6,x+14,y+8,x+6,y+13,C["grey"])
        pyxel.tri(x,y+3,x-6,y,x+2,y+5,C["grey"]); pyxel.tri(x,y+5,x-6,y+10,x+2,y+6,C["grey"])
        pyxel.tri(x+22,y+3,x+26,y+5,x+22,y+8,C["grey"]); pyxel.pset(x+25,y+5,C["foam"])
        pyxel.circ(x+20,y+4,2,C["white"]); pyxel.circ(x+20,y+4,1,C["deep"]); pyxel.pset(x+19,y+3,C["white"])
        pyxel.line(x+21,y+7,x+26,y+6,C["deep"]); pyxel.pset(x+22,y+6,C["white"]); pyxel.pset(x+24,y+6,C["white"])
        for g in range(3): pyxel.line(x+13+g*2,y+2,x+13+g*2,y+7,C["wave_dark"])
        pyxel.text(x+4,y-14,"^v",C["yellow"])
    def _rock(self,x,y):
        pyxel.rect(x+2,y+22,26,6,C["grey"]); pyxel.rect(x,y+24,30,4,C["grey"])
        pyxel.tri(x+15,y,x,y+24,x+30,y+24,C["grey"])
        pyxel.tri(x+15,y+2,x+20,y+24,x+30,y+24,C["wave_dark"]); pyxel.tri(x+15,y+2,x+18,y+24,x+30,y+24,C["deep"])
        pyxel.tri(x+25,y+10,x+16,y+28,x+34,y+28,C["grey"]); pyxel.tri(x+25,y+11,x+22,y+28,x+34,y+28,C["wave_dark"])
        pyxel.circ(x+9,y+12,4,C["green"]); pyxel.circ(x+12,y+8,3,C["green"]); pyxel.rect(x+4,y+17,7,4,C["green"]); pyxel.circ(x+22,y+14,3,C["green"])
        pyxel.line(x+10,y+6,x+13,y+20,C["deep"]); pyxel.line(x+17,y+5,x+19,y+16,C["deep"])
        pyxel.pset(x+15,y+1,C["foam"]); pyxel.pset(x+14,y+2,C["spray"]); pyxel.pset(x+16,y+2,C["spray"])
        for i in range(0,32,3): pyxel.pset(x+i,y+27,C["foam"])
        pyxel.line(x-2,y+26,x+32,y+26,C["spray"]); pyxel.line(x-3,y+28,x+33,y+28,C["foam"])
        pyxel.text(x+6,y-10,"^",C["yellow"])
    def _ship(self,x,y):
        pyxel.line(x+18,y,x+18,y+42,C["grey"]); pyxel.line(x+19,y,x+19,y+42,C["wave_dark"])
        pyxel.line(x+10,y+14,x+28,y+14,C["grey"])
        pyxel.tri(x+18,y+2,x+18,y+32,x+38,y+32,C["white"]); pyxel.tri(x+19,y+5,x+19,y+30,x+35,y+30,C["foam"]); pyxel.tri(x+20,y+10,x+20,y+28,x+32,y+28,C["spray"])
        pyxel.line(x+19,y+16,x+30,y+20,C["white_sh"]); pyxel.line(x+19,y+22,x+27,y+25,C["white_sh"])
        pyxel.tri(x+18,y+4,x+18,y+30,x+2,y+30,C["white"]); pyxel.tri(x+17,y+8,x+17,y+28,x+5,y+28,C["foam"])
        pyxel.line(x+18,y+32,x+38,y+30,C["grey"])
        pyxel.line(x+18,y+2,x+38,y+32,C["grey"]); pyxel.line(x+18,y+2,x+2,y+30,C["grey"])
        pyxel.line(x+10,y+14,x+18,y+2,C["grey"]); pyxel.line(x+28,y+14,x+18,y+2,C["grey"])
        pyxel.rect(x+1,y+31,36,8,C["deep"]); pyxel.rect(x+2,y+31,34,3,C["wave_dark"]); pyxel.rect(x+1,y+31,36,2,C["wave_face"])
        pyxel.rect(x+8,y+32,10,3,C["wave_mid"])
        pyxel.circ(x+14,y+35,2,C["sky_hi"]); pyxel.rectb(x+13,y+34,4,4,C["grey"])
        pyxel.circ(x+24,y+35,2,C["sky_hi"]); pyxel.rectb(x+23,y+34,4,4,C["grey"])
        pyxel.tri(x+19,y,x+19,y+6,x+25,y+3,C["red"])
        pyxel.line(x+4,y+39,x+32,y+39,C["wave_dark"]); pyxel.line(x+8,y+40,x+28,y+40,C["wave_mid"])
        pyxel.text(x+10,y+43,"v",C["yellow"])

class Ptcl:
    def __init__(self,x,y,burst=False):
        self.x=x+random.uniform(-6,6); self.y=y+random.uniform(-4,4)
        self.vx=random.uniform(-2,2) if burst else random.uniform(-.5,.5)
        self.vy=random.uniform(-2.5,-.2) if burst else random.uniform(-1.5,0)
        self.life=random.randint(12,25); self.col=random.choice([C["foam"],C["spray"],C["wave_lite"],C["white"]])
    def update(self): self.x+=self.vx; self.y+=self.vy; self.vy+=.12; self.life-=1
    def draw(self):
        if self.life>0 and int(self.x)>=0 and W>int(self.x) and int(self.y)>=0 and H>int(self.y): pyxel.pset(int(self.x),int(self.y),self.col)

def music():
    pyxel.sounds[0].set("a3 r  e3 r  g3 r  e3 r  a3 r  a3 r  b3 r  a3 r  e3 r  g3 r  a3 r  g3 r  e3 r  c3 r  e3 r  r  r ","p","6060 6060 6060 6060 6060 6060 6060 6060 6060 6060 6060 6060 6060 5050 5050 0000","s",18)
    pyxel.sounds[1].set("a1 r  a1 r  e1 r  e1 r  a1 r  a1 r  d1 r  e1 r  a1 r  a1 r  e1 r  e1 r  d1 r  d1 r  e1 r  r  r ","t","7070 7070 7070 7070 7070 7070 7070 7070 7070 7070 7070 7070 7070 7070 7070 0000","n",18)
    pyxel.sounds[2].set("r  a2 r  a2 r  e2 r  e2 r  a2 r  a2 r  d2 r  e2 r  a2 r  a2 r  e2 r  e2 r  d2 r  d2 r  e2 r  r ","s","0606 0606 0606 0606 0606 0606 0606 0606 0606 0606 0606 0606 0606 0606 0606 0000","n",18)
    pyxel.sounds[3].set("a4 a4 r  a4 a4 r  a4 r  a4 a4 r  a4 a4 r  a4 r  a4 a4 r  a4 a4 r  a4 r  a4 a4 r  a4 a4 r  r  r ","n","3230 3230 3230 3230 3230 3230 3230 3230 3230 3230 3230 3230 3230 3230 3230 0000","n",18)
    pyxel.sounds[4].set("a3g3e3c3","s","7654","n",7)
    pyxel.sounds[5].set("e3d3c3b2a2","t","54321","n",10)
    pyxel.sounds[6].set("a3","p","6","n",5)
    pyxel.sounds[7].set("e4","p","7","n",5)
    pyxel.musics[0].set([0],[1],[2],[3])

class Game:
    SS,SC,SP,SH,SG="sel","cnt","play","hit","over"
    def __init__(self):
        pyxel.init(W,H,title="Surf's Up!",fps=FPS); sp(); music()
        self.hi=0; self.wave=Wave(); self._rst(); pyxel.run(self.update,self.draw)
    def _rst(self):
        self.st=self.SS; self.ch=0; self.cy=float(LM); self.ct=float(LM)
        self.lives=3; self.ta=0; self.spd=2.0; self.obs=[]; self.pts=[]
        self.st2=0; self.st3=0; self.cd=3*FPS; self.ht=0; self.fr=0; self.lcs=3
    def update(self):
        self.fr+=1
        if   self.st==self.SS: self._us()
        elif self.st==self.SC: self._uc()
        elif self.st==self.SP: self._up()
        elif self.st==self.SH: self._uh()
        elif self.st==self.SG: self._ug()
    def _us(self):
        self.wave.update(.7)
        if pyxel.btnp(pyxel.KEY_LEFT) or pyxel.btnp(pyxel.KEY_RIGHT): self.ch=1-self.ch
        if pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.KEY_RETURN): self.st=self.SC; self.cd=3*FPS; self.lcs=3
    def _uc(self):
        self.wave.update(.9); self.cd-=1; cs=self.cd//FPS
        if cs!=self.lcs: self.lcs=cs; pyxel.play(0,7 if cs==0 else 6)
        if 0>=self.cd: self.st=self.SP; pyxel.playm(0,loop=True)
    def _up(self):
        self.ta+=1; self.st2+=1; self.st3+=1
        if self.st2>=10*FPS: self.st2=0; self.spd=min(self.spd+.6,9.0)
        self.wave.update(self.spd)
        if pyxel.btnp(pyxel.KEY_UP): self.ct=LM-JO
        if pyxel.btnp(pyxel.KEY_DOWN): self.ct=LM+DV
        if not pyxel.btn(pyxel.KEY_UP) and not pyxel.btn(pyxel.KEY_DOWN): self.ct=LM
        dy=self.ct-self.cy
        self.cy=self.ct if RS>=abs(dy) else self.cy+math.copysign(RS,dy)
        gap=max(45,int(130-self.spd*9))
        if self.st3>=gap: self.st3=0; self.obs.append(Obs(self.spd+1.8))
        px,py2=58,self.cy; alive=[]; hit=False
        for o in self.obs:
            o.update()
            if o.off(): continue
            alive.append(o)
            if not hit:
                ox,oy,ow,oh=o.rect()
                if px+12>ox and ox+ow>px-12:
                    if o.kind=="shark":
                        if not (oy-6>py2 or py2>oy+oh+6): hit=True
                    elif py2+10>oy and oy+oh>py2-14: hit=True
        self.obs=alive
        if hit: self._hit(int(px),int(py2)); return
        if self.fr%2==0: self.pts.append(Ptcl(px,int(py2)+10))
        self._tp()
        s=self.ta//FPS
        if s>self.hi: self.hi=s
    
    def _hit(self,px,py2):
        pyxel.play(1,4)
        for _ in range(30): self.pts.append(Ptcl(px,py2,burst=True))
        self.lives-=1
        if 0>=self.lives: pyxel.stop(); pyxel.play(0,5); self.st=self.SG
        else: self.obs=[]; self.st3=0; self.ht=2*FPS; self.st=self.SH
    def _uh(self):
        self.ta+=1; self.st2+=1; self.wave.update(self.spd); self._tp(); self.ht-=1
        if 0>=self.ht: self.st=self.SP; self.st3=0
    def _tp(self):
        for p in self.pts: p.update()
        self.pts=[p for p in self.pts if p.life>0]
    def _ug(self):
        self.wave.update(self.spd*.3)
        if pyxel.btnp(pyxel.KEY_R) or pyxel.btnp(pyxel.KEY_SPACE):
            h=self.hi; self._rst(); self.hi=h
        if pyxel.btnp(pyxel.KEY_Q) or pyxel.btnp(pyxel.KEY_ESCAPE): pyxel.quit()
    def draw(self):
        pyxel.cls(C["sky_hi"])
        if   self.st==self.SS: self._ds()
        elif self.st==self.SC: self._dc()
        elif self.st in (self.SP,self.SH): self._dg()
        elif self.st==self.SG: self._do()
    def _ds(self):
        self.wave.draw()
        pyxel.text(W//2-32,13,"SURF'S UP!",C["deep"]); pyxel.text(W//2-32,13,"SURF'S UP!",C["yellow"]); pyxel.text(W//2-31,14,"SURF'S UP!",C["orange"])
        pyxel.text(W//2-44,25,"Choose your surfer!",C["deep"]); pyxel.text(W//2-44,25,"Choose your surfer!",C["white"])
        jx,cx2,cy=W//2-50,W//2+50,LM+5
        joe(jx,cy,self.fr); cody(cx2,cy,self.fr)
        sx=jx if self.ch==0 else cx2; ox2=cx2 if self.ch==0 else jx
        bk=(self.fr//7)%2
        if bk: pyxel.rectb(sx-25,cy-31,50,58,C["yellow"])
        pyxel.rectb(sx-24,cy-30,48,56,C["orange"]); pyxel.rectb(ox2-24,cy-30,48,56,C["wave_dark"])
        pyxel.text(sx-15,cy-43,"SELECTED",C["deep"]); pyxel.text(sx-15,cy-43,"SELECTED",C["yellow"])
        pyxel.tri(sx-3,cy-35,sx+3,cy-35,sx,cy-31,C["orange"])
        jc=C["yellow"] if self.ch==0 else C["foam"]; cc=C["yellow"] if self.ch==1 else C["foam"]
        pyxel.text(jx-23,cy+25,"Chicken Joe",C["deep"]); pyxel.text(jx-23,cy+24,"Chicken Joe",jc)
        pyxel.text(cx2-11,cy+25,"Cody",C["deep"]); pyxel.text(cx2-11,cy+24,"Cody",cc)
        pyxel.rect(0,H-16,W,16,C["deep"]); pyxel.line(0,H-16,W-1,H-16,C["wave_face"])
        pyxel.text(W//2-60,H-12,"<- ->  switch     SPACE / ENTER  surf!",C["white"])
    def _dc(self):
        self.wave.draw()
        if self.ch==0: joe(58,LM,self.fr)
        else: cody(58,LM,self.fr)
        txt=str(self.cd//FPS+1) if self.cd>0 else "GO!"
        cx=W//2-len(txt)*2
        pyxel.text(cx+1,H//2+1,txt,C["deep"]); pyxel.text(cx,H//2,txt,C["yellow"] if self.cd>0 else C["orange"])
    def _dg(self):
        self.wave.draw()
        for o in self.obs: o.draw()
        for p in self.pts: p.draw()
        px,py2=58,int(self.cy); show=True
        if self.st==self.SH: show=(self.ht//5)%2==0
        if show:
            if self.ch==0: joe(px,py2,self.fr)
            else: cody(px,py2,self.fr)
        self._hud()
    def _hud(self):
        pyxel.rect(0,0,W,13,C["deep"]); pyxel.line(0,13,W-1,13,C["wave_face"])
        s=self.ta//FPS
        pyxel.text(4,3,f"TIME:{s:04d}s",C["white"]); pyxel.text(88,3,f"BEST:{self.hi:04d}s",C["yellow"])
        sp2=int((self.spd-2)/7*38)
        pyxel.rect(150,4,38,5,C["wave_dark"]); pyxel.rect(150,4,max(0,sp2),5,C["orange"]); pyxel.text(151,4,"SPD",C["white"])
        for i in range(3):
            hx=W-12-i*11; col=C["red"] if self.lives>i else C["wave_dark"]
            pyxel.pset(hx,3,col); pyxel.pset(hx+2,3,col); pyxel.line(hx-1,4,hx+3,4,col); pyxel.line(hx,5,hx+2,5,col); pyxel.pset(hx+1,6,col)
        pyxel.rect(0,H-10,W,10,C["deep"]); pyxel.text(2,H-8,"UP=jump  DOWN=dive | ROCK:^ | SHIP:v | SHARK:^v",C["grey"])
    def _do(self):
        self.wave.draw()
        bx,by=W//2-66,H//2-42
        pyxel.rect(bx,by,132,84,C["deep"]); pyxel.rectb(bx,by,132,84,C["yellow"]); pyxel.rectb(bx+1,by+1,130,82,C["orange"])
        pyxel.text(W//2-24,by+6,"GAME OVER",C["red"])
        s=self.ta//FPS
        pyxel.text(W//2-40,by+22,f"Your time : {s} s",C["white"]); pyxel.text(W//2-40,by+32,f"High Score: {self.hi} s",C["yellow"])
        if s>0 and s>=self.hi and (self.fr//10)%2: pyxel.text(W//2-28,by+44,"NEW RECORD!",C["orange"])
        pyxel.text(W//2-46,by+58,"[SPACE / R]  Retry",C["white"]); pyxel.text(W//2-24,by+70,"[Q]  Quit",C["grey"])

Game()