From 6225b1d66745bf4a15cb01940f96241c54691dde Mon Sep 17 00:00:00 2001 From: Sylvain PILLOT Date: Sat, 10 Feb 2024 10:39:24 +0100 Subject: [PATCH] added more examples using Kandinsky/Ion/Time modules from NW + reorganisation of NW examples --- .../examples/numworks/NW_pacman_original.py | 223 ++++++++++++ ports/sh/examples/numworks/NW_snake_origin.py | 303 ++++++++++++++++ .../{ => numworks}/cg_NW_kand_mandel.py | 0 .../{ => numworks}/cg_NW_kand_test.py | 0 ports/sh/examples/numworks/cg_NW_pacman.py | 227 ++++++++++++ .../examples/{ => numworks}/cg_NW_rosace.py | 0 ports/sh/examples/numworks/cg_NW_snake.py | 326 ++++++++++++++++++ .../{ => numworks}/cg_NW_time_test.py | 0 8 files changed, 1079 insertions(+) create mode 100644 ports/sh/examples/numworks/NW_pacman_original.py create mode 100644 ports/sh/examples/numworks/NW_snake_origin.py rename ports/sh/examples/{ => numworks}/cg_NW_kand_mandel.py (100%) rename ports/sh/examples/{ => numworks}/cg_NW_kand_test.py (100%) create mode 100644 ports/sh/examples/numworks/cg_NW_pacman.py rename ports/sh/examples/{ => numworks}/cg_NW_rosace.py (100%) create mode 100644 ports/sh/examples/numworks/cg_NW_snake.py rename ports/sh/examples/{ => numworks}/cg_NW_time_test.py (100%) diff --git a/ports/sh/examples/numworks/NW_pacman_original.py b/ports/sh/examples/numworks/NW_pacman_original.py new file mode 100644 index 000000000..52af608c0 --- /dev/null +++ b/ports/sh/examples/numworks/NW_pacman_original.py @@ -0,0 +1,223 @@ +# PacMan par Kevin FEDYNA +# https://nsi.xyz/numapps/pac-man-en-python-numworks/ + +from kandinsky import fill_rect, draw_string +from ion import keydown +from math import sqrt +from random import randint +from time import monotonic + +try: + from kandinsky import get_keys + color = (192, 53, 53) +except: + color = (255, 183, 52) + +terrain = (262143,131841,187245,187245,131073,186285,135969,252783,249903,251823,1152,251823,249903,251823,131841,187245,147465,219051,135969,195453,131073,262143) +bits = 18 +width = 320 +height = 222 +colors = ((0, 0, 0), (32, 48, 248), (248, 224, 8), tuple(color)) +ghost_color = ((255, 184, 255), (255, 0,0), (255, 184, 82), (0, 255, 255)) +pacgommes = [0,130302,9360,74898,131070,75858,126174,8208,8208,8208,8208,8208,8208,8208,130302,74898,49140,43092,126174,66690,131070,0] +superpacgommes = [0,0,65538,0,0,0,0,0,0,0,0,0,0,0,0,0,65538,0,0,0,0,0,0] +frightened = 0 +lives = 2 +won = 0 +lvl = 0 +score = 0 +chained = 0 + + +class Entity: + def __init__(self, x, y, clr, d=0): + self.x = x + self.y = y + self.d = d + self.nd = d + self.f = 0 + self.out = 0 + self.color = clr + fill_rect(int(self.x*10)+136,int(self.y*10)-3,8,8,self.color) + def espace(self,dx=-1,dy=-1): + if dx == dy: + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[self.nd] + return not terrain[int(self.y + 5.5*dy)]>>(bits-1-int(self.x + 5.5*dx)) & 1 and ((dx != 0 and self.y%1 == 0.5) or (dy != 0 and self.x%1== 0.5)) + def move(self): + global frightened, ghosts, score, chained, lives, total, won + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[self.d] + if self.espace(dx,dy): + fill_rect(int(self.x*10)+136,int(self.y*10)-3,8,8,colors[0]) + self.x = (round(self.x + dx, 1) - 0.5) % 16.5 + 0.5 + self.y = round(self.y + dy, 1) + fill_rect(int(self.x*10)+136,int(self.y*10)-3,8,8,self.color) + if self.color == colors[2]: + if pacgommes[int(self.y)] >> (bits - 1 - int(self.x)) & 1: + pacgommes[int(self.y)] -= 1 << (bits - 1 - int(self.x)) + score += 10 + if superpacgommes[int(self.y)] >> (bits - 1 - int(self.x)) & 1: + superpacgommes[int(self.y)] -= 1 << (bits - 1 - int(self.x)) + score += 50 + chained = 0 + frightened = monotonic() + for g in ghosts: + if g.out: + g.color = colors[1] + g.d = (3, 2, 1, 0)[g.d] + g.f = 1 + for g in range(4): + if sqrt((self.x-ghosts[g].x)**2+(self.y-ghosts[g].y)**2) < 0.6: + if ghosts[g].f: + chained += 1 + total += 1 + score += (1 << chained)*100 + ghosts[g].f = 0 + ghosts[g].color = ghost_color[g] + ghosts[g].x = 9 + ghosts[g].y = 8.5 + if total == 16: + score += 12000 + else: + for gp in range(4): + ghosts[gp].f = 0 + ghosts[gp].color = ghost_color[gp] + ghosts[gp].x = 9 + ghosts[gp].y = 10.5 + ghosts[gp].out = 0 + self.x = 9 + self.y = 16.5 + self.d, self.nd = 0, 0 + lives -= 1 + return render() + if not won and score > 10000: + lives += 1 + won = 1 + px, py = int(self.x - 5.5*dx), int(self.y - 5.5*dy) + if pacgommes[py]>>(bits-1-px) & 1: + fill_rect(px*10+144,py*10+5,2,2,(250, 207, 173)) + if superpacgommes[py]>>(bits-1-px) & 1: + fill_rect(px*10+143,py*10+4,4,4,(250, 207, 173)) + def ia(self,x,y): + if self.f: + while True: + d = randint(0,3) + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[d] + if d != (3,2,1,0)[self.d] and self.espace(dx,dy): + self.d = d + break + else: + distances = [9999 for _ in range(4)] + for i in range(4): + if i != (3,2,1,0)[self.d]: + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[i] + if self.espace(dx,dy): + distances[i] = sqrt((self.y + dy - y)**2 + (self.x + dx - x)**2) + self.d = distances.index(min(distances)) + +def prebuild(): + fill_rect(0,0,width,height,colors[0]) + fill_rect(138, 0, 2, height, colors[3]) + draw_string("PAC-MAN", 35, 10, colors[3], colors[0]) + draw_string("nsi.xyz/pacman", 0, 204,colors[0], colors[3]) + draw_string("Score :", 35, 40, (255,)*3, colors[0]) + draw_string("Niveau :", 30, 90, (255,)*3, colors[0]) + +def render(): + global terrain, pacgommes, superpacgommes, lives, arrivee + if lives == -1: + return 42 + draw_string(str(lvl),70-5*len(str(lvl)),110,(255,)*3,colors[0]) + fill_rect(0,150,138,20,colors[0]) + for i in range(lives): + fill_rect(60-(lives-1)*20+i*40,150,20,20,colors[2]) + for l in range(len(terrain)): + for c in range(bits): + fill_rect(c*10+140,l*10+1,10,10,colors[0]) + if pacgommes[l]>>(bits-1-c) & 1: + fill_rect(c*10+144,l*10+5,2,2,(250, 207, 173)) + if superpacgommes[l]>>(bits-1-c) & 1: + fill_rect(c*10+143,l*10+4,4,4,(250, 207, 173)) + if terrain[l]>>(bits-1-c) & 1: + for d in ((1,0),(0,1),(-1,0),(0,-1)): + if 0 <= l + d[0] <= len(terrain) - 1 and 0 <= c + d[1] <= bits - 1 and not terrain[l + d[0]]>>(bits-1-(c+d[1])) & 1: + fill_rect(c*10+140+9*(d[1]==1),l*10+1+9*(d[0]==1),1+9*(d[1]==0),1+9*(d[0]==0),colors[1]) + arrivee = monotonic() + +def engine(): + global frightened, ghosts, pacgommes, superpacgommes, lvl, arrivee, total + while True: + pacgommes = [0,130302,9360,74898,131070,75858,126174,8208,8208,8208,8208,8208,8208,8208,130302,74898,49140,43092,126174,66690,131070,0] + superpacgommes = [0,0,65538,0,0,0,0,0,0,0,0,0,0,0,0,0,65538,0,0,0,0,0,0] + lvl += 1 + total = 0 + render() + pacman = Entity(9, 16.5, colors[2]) + ghosts = [Entity(9, 10.5, ghost_color[i]) for i in range(4)] + while sum(pacgommes) + sum(superpacgommes): + depart = monotonic() + for i in range(4): + if keydown(i): + if i == (3,2,1,0)[pacman.d]: + pacman.d = i + pacman.nd = i + while monotonic() - depart < 0.01: + if pacman.espace(): + pacman.d = pacman.nd + if pacman.move() == 42: + draw_string("GAME OVER",185,100,colors[3],colors[0]) + return 69 + + draw_string(str(score),70-5*len(str(score)),60,(255,)*3,colors[0]) + + """ Fantomes """ + + if frightened: + if monotonic() - frightened > 6.5: + for g in ghosts: + if g.f: + g.color = (255,)*3 + if monotonic() - frightened > 8.5: + frightened = 0 + for g in range(4): + ghosts[g].color = ghost_color[g] + ghosts[g].f = 0 + + if arrivee: + if monotonic() - arrivee > 0 and not ghosts[1].out: + ghosts[1].out = 1 + ghosts[1].y = 8.5 + if monotonic() - arrivee > 2.5 and not ghosts[0].out: + ghosts[0].out = 1 + ghosts[0].y = 8.5 + if monotonic() - arrivee > 5 and not ghosts[3].out: + ghosts[3].out = 1 + ghosts[3].y = 8.5 + if monotonic() - arrivee > 7.5 and not ghosts[2].out: + ghosts[2].out = 1 + ghosts[2].y = 8.5 + fill_rect(220,101,20,10,colors[0]) + arrivee = 0 + + pdx, pdy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[pacman.d] + + # Pinky + ghosts[0].ia(pacman.x + 20 * pdx, pacman.y + 20 * pdy) + ghosts[0].move() + + # Inky + ghosts[3].ia(max(min(ghosts[1].x + 2*(pacman.x + 20 * pdx - ghosts[1].x), 16.5), 1.5), max(min(ghosts[1].y +2*(pacman.y + 20 * pdy - ghosts[1].y), 21.5), 1.5)) + ghosts[3].move() + + # Blinky + ghosts[1].ia(pacman.x, pacman.y) + ghosts[1].move() + + # Clyde + if sqrt((ghosts[2].x - pacman.x)**2 + (ghosts[2].y - pacman.y)**2) > 4: + ghosts[2].ia(pacman.x, pacman.y) + else: + ghosts[2].ia(1.5, 20.5) + ghosts[2].move() + +prebuild() +engine() diff --git a/ports/sh/examples/numworks/NW_snake_origin.py b/ports/sh/examples/numworks/NW_snake_origin.py new file mode 100644 index 000000000..93621dd3e --- /dev/null +++ b/ports/sh/examples/numworks/NW_snake_origin.py @@ -0,0 +1,303 @@ +# Snake from Golem64 +# https://my.numworks.com/python/golem64/snake + +#Version 1.7 STABLE +#Tip: You should try to press +#some keys in the menu... +from random import * +from kandinsky import * +from ion import * +from time import * +#from pomme import * +def oscolor(): + try: + get_keys() + except: + return 'orange' + else: + return 'red' +def lastPos(i,x,y): + if i[-1]==3: + pos=[x-10,y] + elif i[-1]==2: + pos=[x,y-10] + elif i[-1]==0: + pos=[x+10,y] + elif i[-1]==1: + pos=[x,y+10] + pos[0],pos[1]=checkTeleport(pos[0],pos[1]) + return pos +def newApple(appleC,bgC): + applex=randint(0,31)*10+4 + appley=randint(0,21)*10+5 + while get_pixel(applex,appley)!=bgC: + applex=randint(0,31)*10+4 + appley=randint(0,21)*10+5 + fill_rect(applex-4,appley-4,10,10,appleC) + return applex,appley +def checkTeleport(x,y): + if x<4: + x=314 + if x>314: + x=4 + if y<5: + y=215 + if y>215: + y=5 + return x,y +def getMove(u): + for k in range(4): + if keydown(k)==True and u+k!=3: return k + return u +def clearDraw(): fill_rect(0,0,320,222,(255,255,255)) +def clearHome(): print("\n \n \n \n \n \n \n \n \n \n \n \n \n ") +def redraw(): + draw_string("(DELETE to exit)",0,0) + printLetter([1,1,1,1,0,0,1,1,1,0,0,1,1,1,1],70,80,10,(0,204,0)) + fill_rect(95,80,2,4,(0,0,0)) + fill_rect(95,86,2,4,(0,0,0)) + fill_rect(100,84,4,2,(255,0,0)) + fill_rect(104,82,2,2,(255,0,0)) + fill_rect(104,86,2,2,(255,0,0)) + printLetter([1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],110,80,10,(0,0,0)) + printLetter([1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],150,80,10,(0,0,0)) + printLetter([1,0,1,1,0,1,1,1,0,1,0,1,1,0,1],190,80,10,(0,0,0)) + printLetter([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1],230,80,10,(0,0,0)) +def printLetter(letter,x,y,size,color): + for yi in range(5): + for xi in range(3): + if letter[yi*3+xi]==1: + fill_rect(x+(xi*size),y+(yi*size),size,size,color) +def menu(): + clearDraw() + printLetter([1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],110,80,10,(0,0,0)) + printLetter([1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],150,80,10,(0,0,0)) + printLetter([1,0,1,1,0,1,1,1,0,1,0,1,1,0,1],190,80,10,(0,0,0)) + printLetter([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1],230,80,10,(0,0,0)) + anim=[1,1,1,1,1,1,1,1,1,4,4,3,3,4,4,1,1] + ax=0 + ay=120 + aendx=-110 + aendy=120 + u=1 + aback=0 + for i in range(len(anim)): + ax=ax+((anim[i]==1)-(anim[i]==3))*10 + ay=ay+((anim[i]==2)-(anim[i]==4))*10 + if aendx<0: + aendx=aendx+10 + else: + aendx=aendx+((anim[i-11]==1)-(anim[i-11]==3))*10 + aendy=aendy+((anim[i-11]==2)-(anim[i-11]==4))*10 + fill_rect(aendx,aendy,10,10,(255,255,255)) + fill_rect(ax,ay,10,10,(0,204,0)) +# aback=lastPos(anim,ax,ay) +# if u==26 or u==24: +# fill_rect(ax-1,ay-1,3,1,(0,0,0)) +# fill_rect(ax-1,ay+1,3,1,(0,0,0)) +# fill_rect(aback[0],aback[1],10,10,(0,204,0)) +# elif u==34 or u==25: +# fill_rect(ax-1,ay-1,1,3,(0,0,0)) +# fill_rect(ax+1,ay-1,1,3,(0,0,0)) +# fill_rect(aback[0]-2,aback[1]-2,5,5,(0,204,0)) + sleep(0.05) + fill_rect(ax+5,ay,2,4,(0,0,0)) + fill_rect(ax+5,ay+6,2,4,(0,0,0)) + fill_rect(ax+10,ay+4,4,2,(255,0,0)) + fill_rect(ax+14,ay+2,2,2,(255,0,0)) + fill_rect(ax+14,ay+6,2,2,(255,0,0)) + draw_string("(DELETE to exit)",0,0) + draw_string("> Play <",125,140,oscolor()) + draw_string(" Options ",110,165) + darkMode=0 + Speed=0.05 + power=5 + score=1 + exit=0 + sel=1 + while keydown(KEY_OK)!=True and exit==0: + if keydown(KEY_DOWN) and sel==1: + draw_string(" Play ",125,140) + draw_string("> Options <",110,165,oscolor()) + sel=2 + elif keydown(KEY_UP) and sel==2: + draw_string("> Play <",125,140,oscolor()) + draw_string(" Options ",110,165) + sel=1 + if keydown(KEY_LEFTPARENTHESIS) and keydown(KEY_RIGHTPARENTHESIS): + draw_string("Dark mode enabled !",80,195) + darkMode=1 + if keydown(KEY_BACKSPACE): + exit=1 + sleep(0.1) + if sel==2 and exit!=1: + fill_rect(0,130,300,60,(255,255,255)) + Speed=0.05 + power=5 + score=1 + draw_string("Speed:"+str(Speed),50,140,oscolor(),'white') + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170) + sel=1 + sleep(0.2) + while keydown(KEY_OK)!=True or sel!=4: + if keydown(KEY_RIGHT): + sel=sel+1 + elif keydown(KEY_DOWN): + sel=sel+2 + elif keydown(KEY_LEFT): + sel=sel-1 + elif keydown(KEY_UP): + sel=sel-2 + if sel<0: + sel=0 + if sel>4: + sel=4 + if sel==1: + draw_string("Speed:"+str(Speed),50,140,oscolor(),'white') + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170) + if keydown(KEY_OK): + clearHome() + Speed=input("Speed:") + redraw() + elif sel==2: + draw_string("Speed:"+str(Speed),50,140) + draw_string("Power:+"+str(power),200,140,oscolor(),'white') + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170) + if keydown(KEY_OK): + clearHome() + power=int(input("Power:+")) + redraw() + elif sel==3: + draw_string("Speed:"+str(Speed),50,140) + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170,oscolor(),'white') + draw_string("Play",220,170) + if keydown(KEY_OK): + clearHome() + score=int(input("Score:")) + redraw() + elif sel==4: + draw_string("Speed:"+str(Speed),50,140) + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170,oscolor(),'white') + if (keydown(KEY_LEFTPARENTHESIS) and keydown(KEY_RIGHTPARENTHESIS)) or darkMode==1: + draw_string("Dark mode enabled !",80,195) + darkMode=1 + if keydown(KEY_BACKSPACE): + exit=1 + break + sleep(0.1) + if exit!=1: + if darkMode==1: + launch(1,Speed,power,score) + elif darkMode==0: + launch(0,Speed,power,score) + elif exit==1: + clearDraw() + return +def launch(darkmode=0,speed=0.05,applePower=5,appleScore=1): + bgC=(248,252,248) + borderC=(0,0,0) + snakeC=(0,204,0) + appleC=(248,0,0) + if darkmode==1: + bgC=(0,0,0) + borderC=(0,0,204) + fill_rect(0,0,320,222,bgC) +# fill_rect(315,0,5,222,borderC) +# fill_rect(0,0,5,222,borderC) +# fill_rect(0,0,320,1,(197,52,49)) + fill_rect(0,221,320,1,(0,0,0)) + try: + get_keys() + except: + fill_rect(0,0,320,1,(255,181,49)) + else: + fill_rect(0,0,320,1,(197,52,49)) + snake=[3,3,3,3,3] + x=154 + y=115 + endx=104 + endy=115 + u,v=3,3 + length=5 + applex,appley=newApple(appleC,bgC) + score,touched=0,0 + while touched!=borderC and touched!=snakeC: + if keydown(0) or keydown(1) or keydown(2) or keydown(3): + u=getMove(u) + if keydown(KEY_BACKSPACE): + while keydown(KEY_BACKSPACE): + sleep(0.1) + while keydown(KEY_BACKSPACE)!=True: + sleep(0.1) + while keydown(KEY_BACKSPACE): + sleep(0.1) + snake.append(u) + if x==applex and y==appley: + length=length+float(applePower) + applex,appley=newApple(appleC,bgC) + score=score+int(appleScore) + x=x+((u==3)-(u==0))*10 + y=y+((u==2)-(u==1))*10 + x,y=checkTeleport(x,y) + if length: + length=length-1 + else: + snake.remove(snake[0]) + endx=endx+((v==3)-(v==0))*10 + endy=endy+((v==2)-(v==1))*10 + endx,endy=checkTeleport(endx,endy) + v=snake[0] + fill_rect(endx-4,endy-4,10,10,bgC) + touched=get_pixel(x,y) + if x<0 or x>320 or y<0 or y>220: + touched=borderC + if touched!=appleC and touched!=bgC: + touched=borderC + fill_rect(x-4,y-4,10,10,snakeC) + back=lastPos(snake,x,y) + if u==3 or u==0: + fill_rect(x,y-4,2,4,(0,0,0)) + fill_rect(x,y+2,2,4,(0,0,0)) + fill_rect(back[0]-4,back[1]-4,10,10,snakeC) + elif u==2 or u==1: + fill_rect(x-4,y,4,2,(0,0,0)) + fill_rect(x+2,y,4,2,(0,0,0)) + fill_rect(back[0]-4,back[1]-4,10,10,snakeC) + sleep(float(speed)) +# EPILEPSY WARNING !!! +# snakeC=(randint(0,255),randint(0,255),randint(0,255)) + while snakeC==appleC or snakeC==bgC: + snakeC=(randint(0,255),randint(0,255),randint(0,255)) + # beau() + if len(snake)==640: + if darkmode==1: + draw_string("You win !",120,100,'white','black') + draw_string("(You reached the max length)",20,120,'white','black') + else: + draw_string("You win !",120,100) + draw_string("(You reached the max length)",20,120) + touched=borderC + if darkmode==1: + draw_string("Score:"+str(score),10,10,'white','black') + draw_string("(OK=play again, DELETE=Menu)",10,30,'white','black') + else: + draw_string("Score:"+str(score),10,10) + draw_string("(OK=play again, DELETE=Menu)",10,30) + choice=0 + while choice==0: + if keydown(KEY_OK): + choice=1 + launch(darkmode,speed,applePower,appleScore) + elif keydown(KEY_BACKSPACE): + choice=2 + menu() + print("Score:",score) +menu() diff --git a/ports/sh/examples/cg_NW_kand_mandel.py b/ports/sh/examples/numworks/cg_NW_kand_mandel.py similarity index 100% rename from ports/sh/examples/cg_NW_kand_mandel.py rename to ports/sh/examples/numworks/cg_NW_kand_mandel.py diff --git a/ports/sh/examples/cg_NW_kand_test.py b/ports/sh/examples/numworks/cg_NW_kand_test.py similarity index 100% rename from ports/sh/examples/cg_NW_kand_test.py rename to ports/sh/examples/numworks/cg_NW_kand_test.py diff --git a/ports/sh/examples/numworks/cg_NW_pacman.py b/ports/sh/examples/numworks/cg_NW_pacman.py new file mode 100644 index 000000000..8b7e422b9 --- /dev/null +++ b/ports/sh/examples/numworks/cg_NW_pacman.py @@ -0,0 +1,227 @@ +# PacMan par Kevin FEDYNA +# https://nsi.xyz/numapps/pac-man-en-python-numworks/ +# converti sur fxCG50 et PythonExtra par SlyVTT + +from kandinsky import fill_rect, draw_string +from ion import keydown, KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT +from math import sqrt +from random import randint +from time import monotonic + +try: + from kandinsky import get_keys + color = (192, 53, 53) +except: + color = (255, 183, 52) + +terrain = (262143,131841,187245,187245,131073,186285,135969,252783,249903,251823,1152,251823,249903,251823,131841,187245,147465,219051,135969,195453,131073,262143) +bits = 18 +width = 320 +height = 222 +colors = ((0, 0, 0), (32, 48, 248), (248, 224, 8), tuple(color)) +ghost_color = ((255, 184, 255), (255, 0,0), (255, 184, 82), (0, 255, 255)) +pacgommes = [0,130302,9360,74898,131070,75858,126174,8208,8208,8208,8208,8208,8208,8208,130302,74898,49140,43092,126174,66690,131070,0] +superpacgommes = [0,0,65538,0,0,0,0,0,0,0,0,0,0,0,0,0,65538,0,0,0,0,0,0] +frightened = 0 +lives = 2 +won = 0 +lvl = 0 +score = 0 +chained = 0 + + +class Entity: + def __init__(self, x, y, clr, d=0): + self.x = x + self.y = y + self.d = d + self.nd = d + self.f = 0 + self.out = 0 + self.color = clr + fill_rect(int(self.x*10)+136,int(self.y*10)-3,8,8,self.color) + def espace(self,dx=-1,dy=-1): + if dx == dy: + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[self.nd] + return not terrain[int(self.y + 5.5*dy)]>>(bits-1-int(self.x + 5.5*dx)) & 1 and ((dx != 0 and self.y%1 == 0.5) or (dy != 0 and self.x%1== 0.5)) + def move(self): + global frightened, ghosts, score, chained, lives, total, won + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[self.d] + if self.espace(dx,dy): + fill_rect(int(self.x*10)+136,int(self.y*10)-3,8,8,colors[0]) + self.x = (round(self.x + dx, 1) - 0.5) % 16.5 + 0.5 + self.y = round(self.y + dy, 1) + fill_rect(int(self.x*10)+136,int(self.y*10)-3,8,8,self.color) + if self.color == colors[2]: + if pacgommes[int(self.y)] >> (bits - 1 - int(self.x)) & 1: + pacgommes[int(self.y)] -= 1 << (bits - 1 - int(self.x)) + score += 10 + if superpacgommes[int(self.y)] >> (bits - 1 - int(self.x)) & 1: + superpacgommes[int(self.y)] -= 1 << (bits - 1 - int(self.x)) + score += 50 + chained = 0 + frightened = monotonic() + for g in ghosts: + if g.out: + g.color = colors[1] + g.d = (3, 2, 1, 0)[g.d] + g.f = 1 + for g in range(4): + if sqrt((self.x-ghosts[g].x)**2+(self.y-ghosts[g].y)**2) < 0.6: + if ghosts[g].f: + chained += 1 + total += 1 + score += (1 << chained)*100 + ghosts[g].f = 0 + ghosts[g].color = ghost_color[g] + ghosts[g].x = 9 + ghosts[g].y = 8.5 + if total == 16: + score += 12000 + else: + for gp in range(4): + ghosts[gp].f = 0 + ghosts[gp].color = ghost_color[gp] + ghosts[gp].x = 9 + ghosts[gp].y = 10.5 + ghosts[gp].out = 0 + self.x = 9 + self.y = 16.5 + self.d, self.nd = 0, 0 + lives -= 1 + return render() + if not won and score > 10000: + lives += 1 + won = 1 + px, py = int(self.x - 5.5*dx), int(self.y - 5.5*dy) + if pacgommes[py]>>(bits-1-px) & 1: + fill_rect(px*10+144,py*10+5,2,2,(250, 207, 173)) + if superpacgommes[py]>>(bits-1-px) & 1: + fill_rect(px*10+143,py*10+4,4,4,(250, 207, 173)) + def ia(self,x,y): + if self.f: + while True: + d = randint(0,3) + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[d] + if d != (3,2,1,0)[self.d] and self.espace(dx,dy): + self.d = d + break + else: + distances = [9999 for _ in range(4)] + for i in range(4): + if i != (3,2,1,0)[self.d]: + dx, dy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[i] + if self.espace(dx,dy): + distances[i] = sqrt((self.y + dy - y)**2 + (self.x + dx - x)**2) + self.d = distances.index(min(distances)) + +def prebuild(): + fill_rect(0,0,width,height,colors[0]) + fill_rect(138, 0, 2, height, colors[3]) + draw_string("PAC-MAN", 35, 10, colors[3], colors[0]) + draw_string("nsi.xyz/pacman", 0, 204,colors[0], colors[3]) + draw_string("Score :", 35, 40, (255,)*3, colors[0]) + draw_string("Niveau :", 30, 90, (255,)*3, colors[0]) + +def render(): + global terrain, pacgommes, superpacgommes, lives, arrivee + if lives == -1: + return 42 + draw_string(str(lvl),70-5*len(str(lvl)),110,(255,)*3,colors[0]) + fill_rect(0,150,138,20,colors[0]) + for i in range(lives): + fill_rect(60-(lives-1)*20+i*40,150,20,20,colors[2]) + for l in range(len(terrain)): + for c in range(bits): + fill_rect(c*10+140,l*10+1,10,10,colors[0]) + if pacgommes[l]>>(bits-1-c) & 1: + fill_rect(c*10+144,l*10+5,2,2,(250, 207, 173)) + if superpacgommes[l]>>(bits-1-c) & 1: + fill_rect(c*10+143,l*10+4,4,4,(250, 207, 173)) + if terrain[l]>>(bits-1-c) & 1: + for d in ((1,0),(0,1),(-1,0),(0,-1)): + if 0 <= l + d[0] <= len(terrain) - 1 and 0 <= c + d[1] <= bits - 1 and not terrain[l + d[0]]>>(bits-1-(c+d[1])) & 1: + fill_rect(c*10+140+9*(d[1]==1),l*10+1+9*(d[0]==1),1+9*(d[1]==0),1+9*(d[0]==0),colors[1]) + arrivee = monotonic() + +def engine(): + global frightened, ghosts, pacgommes, superpacgommes, lvl, arrivee, total + while True: + pacgommes = [0,130302,9360,74898,131070,75858,126174,8208,8208,8208,8208,8208,8208,8208,130302,74898,49140,43092,126174,66690,131070,0] + superpacgommes = [0,0,65538,0,0,0,0,0,0,0,0,0,0,0,0,0,65538,0,0,0,0,0,0] + lvl += 1 + total = 0 + render() + pacman = Entity(9, 16.5, colors[2]) + ghosts = [Entity(9, 10.5, ghost_color[i]) for i in range(4)] + while sum(pacgommes) + sum(superpacgommes): + depart = monotonic() + lk=[KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT] + for i in range(4): + if keydown(lk[i]): + if i == (3,2,1,0)[pacman.d]: + pacman.d = i + pacman.nd = i + while monotonic() - depart < 0.01: + if pacman.espace(): + pacman.d = pacman.nd + if pacman.move() == 42: + draw_string("GAME OVER",185,100,colors[3],colors[0]) + return 69 + + draw_string(str(score),70-5*len(str(score)),60,(255,)*3,colors[0]) + + """ Fantomes """ + + if frightened: + if monotonic() - frightened > 6.5: + for g in ghosts: + if g.f: + g.color = (255,)*3 + if monotonic() - frightened > 8.5: + frightened = 0 + for g in range(4): + ghosts[g].color = ghost_color[g] + ghosts[g].f = 0 + + if arrivee: + if monotonic() - arrivee > 0 and not ghosts[1].out: + ghosts[1].out = 1 + ghosts[1].y = 8.5 + if monotonic() - arrivee > 2.5 and not ghosts[0].out: + ghosts[0].out = 1 + ghosts[0].y = 8.5 + if monotonic() - arrivee > 5 and not ghosts[3].out: + ghosts[3].out = 1 + ghosts[3].y = 8.5 + if monotonic() - arrivee > 7.5 and not ghosts[2].out: + ghosts[2].out = 1 + ghosts[2].y = 8.5 + fill_rect(220,101,20,10,colors[0]) + arrivee = 0 + + pdx, pdy = ((-0.1,0),(0,-0.1),(0,0.1),(0.1,0))[pacman.d] + + # Pinky + ghosts[0].ia(pacman.x + 20 * pdx, pacman.y + 20 * pdy) + ghosts[0].move() + + # Inky + ghosts[3].ia(max(min(ghosts[1].x + 2*(pacman.x + 20 * pdx - ghosts[1].x), 16.5), 1.5), max(min(ghosts[1].y +2*(pacman.y + 20 * pdy - ghosts[1].y), 21.5), 1.5)) + ghosts[3].move() + + # Blinky + ghosts[1].ia(pacman.x, pacman.y) + ghosts[1].move() + + # Clyde + if sqrt((ghosts[2].x - pacman.x)**2 + (ghosts[2].y - pacman.y)**2) > 4: + ghosts[2].ia(pacman.x, pacman.y) + else: + ghosts[2].ia(1.5, 20.5) + ghosts[2].move() + + +fill_rect(0,0,319,219,"black") +prebuild() +engine() diff --git a/ports/sh/examples/cg_NW_rosace.py b/ports/sh/examples/numworks/cg_NW_rosace.py similarity index 100% rename from ports/sh/examples/cg_NW_rosace.py rename to ports/sh/examples/numworks/cg_NW_rosace.py diff --git a/ports/sh/examples/numworks/cg_NW_snake.py b/ports/sh/examples/numworks/cg_NW_snake.py new file mode 100644 index 000000000..a498aa897 --- /dev/null +++ b/ports/sh/examples/numworks/cg_NW_snake.py @@ -0,0 +1,326 @@ +# Snake from Golem64 +# https://my.numworks.com/python/golem64/snake +# +# converted to PythonExtra on fx-CG50 by SlyVTT +# +# Version 1.7 STABLE +# Tip: You should try to press +# some keys in the menu... + +from random import * +from kandinsky import * +from ion import * +from time import * + +def oscolor(): + try: + get_keys() + except: + return 'orange' + else: + return 'red' + +def lastPos(i,x,y): + if i[-1]==3: + pos=[x-10,y] + elif i[-1]==2: + pos=[x,y-10] + elif i[-1]==0: + pos=[x+10,y] + elif i[-1]==1: + pos=[x,y+10] + pos[0],pos[1]=checkTeleport(pos[0],pos[1]) + return pos + +def newApple(appleC,bgC): + applex=randint(0,31)*10+4 + appley=randint(0,21)*10+5 + while get_pixel(applex,appley)!=bgC: + applex=randint(0,31)*10+4 + appley=randint(0,21)*10+5 + fill_rect(applex-4,appley-4,10,10,appleC) + return applex,appley + +def checkTeleport(x,y): + if x<4: + x=314 + if x>314: + x=4 + if y<5: + y=215 + if y>215: + y=5 + return x,y + +def getMove(u): + lk=[KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT] + for k in range(4): + if keydown(lk[k])==True and u+k!=3: return k + return u + +def clearDraw(): + fill_rect(0,0,320,222,(255,255,255)) + +def clearHome(): + print("\n \n \n \n \n \n \n \n \n \n \n \n \n ") + +def redraw(): + draw_string("(DELETE to exit)",0,0) + printLetter([1,1,1,1,0,0,1,1,1,0,0,1,1,1,1],70,80,10,(0,204,0)) + fill_rect(95,80,2,4,(0,0,0)) + fill_rect(95,86,2,4,(0,0,0)) + fill_rect(100,84,4,2,(255,0,0)) + fill_rect(104,82,2,2,(255,0,0)) + fill_rect(104,86,2,2,(255,0,0)) + printLetter([1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],110,80,10,(0,0,0)) + printLetter([1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],150,80,10,(0,0,0)) + printLetter([1,0,1,1,0,1,1,1,0,1,0,1,1,0,1],190,80,10,(0,0,0)) + printLetter([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1],230,80,10,(0,0,0)) + +def printLetter(letter,x,y,size,color): + for yi in range(5): + for xi in range(3): + if letter[yi*3+xi]==1: + fill_rect(x+(xi*size),y+(yi*size),size,size,color) + +def menu(): + clearDraw() + printLetter([1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],110,80,10,(0,0,0)) + printLetter([1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],150,80,10,(0,0,0)) + printLetter([1,0,1,1,0,1,1,1,0,1,0,1,1,0,1],190,80,10,(0,0,0)) + printLetter([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1],230,80,10,(0,0,0)) + anim=[1,1,1,1,1,1,1,1,1,4,4,3,3,4,4,1,1] + ax=0 + ay=120 + aendx=-110 + aendy=120 + u=1 + aback=0 + for i in range(len(anim)): + ax=ax+((anim[i]==1)-(anim[i]==3))*10 + ay=ay+((anim[i]==2)-(anim[i]==4))*10 + if aendx<0: + aendx=aendx+10 + else: + aendx=aendx+((anim[i-11]==1)-(anim[i-11]==3))*10 + aendy=aendy+((anim[i-11]==2)-(anim[i-11]==4))*10 + fill_rect(aendx,aendy,10,10,(255,255,255)) + fill_rect(ax,ay,10,10,(0,204,0)) +# aback=lastPos(anim,ax,ay) +# if u==26 or u==24: +# fill_rect(ax-1,ay-1,3,1,(0,0,0)) +# fill_rect(ax-1,ay+1,3,1,(0,0,0)) +# fill_rect(aback[0],aback[1],10,10,(0,204,0)) +# elif u==34 or u==25: +# fill_rect(ax-1,ay-1,1,3,(0,0,0)) +# fill_rect(ax+1,ay-1,1,3,(0,0,0)) +# fill_rect(aback[0]-2,aback[1]-2,5,5,(0,204,0)) + sleep(0.05) + fill_rect(ax+5,ay,2,4,(0,0,0)) + fill_rect(ax+5,ay+6,2,4,(0,0,0)) + fill_rect(ax+10,ay+4,4,2,(255,0,0)) + fill_rect(ax+14,ay+2,2,2,(255,0,0)) + fill_rect(ax+14,ay+6,2,2,(255,0,0)) + draw_string("(DELETE to exit)",0,0) + draw_string("> Play <",125,140,oscolor()) + draw_string(" Options ",110,165) + darkMode=0 + Speed=0.05 + power=5 + score=1 + exit=0 + sel=1 + while keydown(KEY_OK)!=True and exit==0: + if keydown(KEY_DOWN) and sel==1: + draw_string(" Play ",125,140) + draw_string("> Options <",110,165,oscolor()) + sel=2 + elif keydown(KEY_UP) and sel==2: + draw_string("> Play <",125,140,oscolor()) + draw_string(" Options ",110,165) + sel=1 + if keydown(KEY_LEFTPARENTHESIS) and keydown(KEY_RIGHTPARENTHESIS): + draw_string("Dark mode enabled !",80,195) + darkMode=1 + if keydown(KEY_BACKSPACE): + exit=1 + sleep(0.1) + if sel==2 and exit!=1: + fill_rect(0,130,300,60,(255,255,255)) + Speed=0.05 + power=5 + score=1 + draw_string("Speed:"+str(Speed),50,140,oscolor(),'white') + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170) + sel=1 + sleep(0.2) + while keydown(KEY_OK)!=True or sel!=4: + if keydown(KEY_RIGHT): + sel=sel+1 + elif keydown(KEY_DOWN): + sel=sel+2 + elif keydown(KEY_LEFT): + sel=sel-1 + elif keydown(KEY_UP): + sel=sel-2 + if sel<0: + sel=0 + if sel>4: + sel=4 + if sel==1: + draw_string("Speed:"+str(Speed),50,140,oscolor(),'white') + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170) + if keydown(KEY_OK): + clearHome() + Speed=input("Speed:") + redraw() + elif sel==2: + draw_string("Speed:"+str(Speed),50,140) + draw_string("Power:+"+str(power),200,140,oscolor(),'white') + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170) + if keydown(KEY_OK): + clearHome() + power=int(input("Power:+")) + redraw() + elif sel==3: + draw_string("Speed:"+str(Speed),50,140) + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170,oscolor(),'white') + draw_string("Play",220,170) + if keydown(KEY_OK): + clearHome() + score=int(input("Score:")) + redraw() + elif sel==4: + draw_string("Speed:"+str(Speed),50,140) + draw_string("Power:+"+str(power),200,140) + draw_string("Score:+"+str(score),50,170) + draw_string("Play",220,170,oscolor(),'white') + if (keydown(KEY_LEFTPARENTHESIS) and keydown(KEY_RIGHTPARENTHESIS)) or darkMode==1: + draw_string("Dark mode enabled !",80,195) + darkMode=1 + if keydown(KEY_BACKSPACE): + exit=1 + break + sleep(0.1) + if exit!=1: + if darkMode==1: + launch(1,Speed,power,score) + elif darkMode==0: + launch(0,Speed,power,score) + elif exit==1: + clearDraw() + return + +def launch(darkmode=0,speed=0.05,applePower=5,appleScore=1): + bgC=(248,252,248) + borderC=(0,0,0) + snakeC=(0,204,0) + appleC=(248,0,0) + if darkmode==1: + bgC=(0,0,0) + borderC=(0,0,204) + fill_rect(0,0,320,222,bgC) +# fill_rect(315,0,5,222,borderC) +# fill_rect(0,0,5,222,borderC) +# fill_rect(0,0,320,1,(197,52,49)) + fill_rect(0,221,320,1,(0,0,0)) + try: + get_keys() + except: + fill_rect(0,0,320,1,(255,181,49)) + else: + fill_rect(0,0,320,1,(197,52,49)) + snake=[3,3,3,3,3] + x=154 + y=115 + endx=104 + endy=115 + u,v=3,3 + length=5 + applex,appley=newApple(appleC,bgC) + score,touched=0,0 + while touched!=borderC and touched!=snakeC: + if keydown(KEY_LEFT) or keydown(KEY_UP) or keydown(KEY_DOWN) or keydown(KEY_RIGHT): + u=getMove(u) + if keydown(KEY_BACKSPACE): + while keydown(KEY_BACKSPACE): + sleep(0.1) + while keydown(KEY_BACKSPACE)!=True: + sleep(0.1) + while keydown(KEY_BACKSPACE): + sleep(0.1) + snake.append(u) + if x==applex and y==appley: + length=length+float(applePower) + applex,appley=newApple(appleC,bgC) + score=score+int(appleScore) + x=x+((u==3)-(u==0))*10 + y=y+((u==2)-(u==1))*10 + x,y=checkTeleport(x,y) + if length: + length=length-1 + else: + snake.remove(snake[0]) + endx=endx+((v==3)-(v==0))*10 + endy=endy+((v==2)-(v==1))*10 + endx,endy=checkTeleport(endx,endy) + v=snake[0] + fill_rect(endx-4,endy-4,10,10,bgC) + touched=get_pixel(x,y) + if x<0 or x>320 or y<0 or y>220: + touched=borderC + if touched!=appleC and touched!=bgC: + touched=borderC + fill_rect(x-4,y-4,10,10,snakeC) + back=lastPos(snake,x,y) + if u==3 or u==0: + fill_rect(x,y-4,2,4,(0,0,0)) + fill_rect(x,y+2,2,4,(0,0,0)) + fill_rect(back[0]-4,back[1]-4,10,10,snakeC) + elif u==2 or u==1: + fill_rect(x-4,y,4,2,(0,0,0)) + fill_rect(x+2,y,4,2,(0,0,0)) + fill_rect(back[0]-4,back[1]-4,10,10,snakeC) + sleep(float(speed)) +# EPILEPSY WARNING !!! +# snakeC=(randint(0,255),randint(0,255),randint(0,255)) + while snakeC==appleC or snakeC==bgC: + snakeC=(randint(0,255),randint(0,255),randint(0,255)) + # beau() + if len(snake)==640: + if darkmode==1: + draw_string("You win !",120,100,'white','black') + draw_string("(You reached the max length)",20,120,'white','black') + else: + draw_string("You win !",120,100) + draw_string("(You reached the max length)",20,120) + touched=borderC + if darkmode==1: + draw_string("Score:"+str(score),10,10,'white','black') + draw_string("(OK=play again, DELETE=Menu)",10,30,'white','black') + else: + draw_string("Score:"+str(score),10,10) + draw_string("(OK=play again, DELETE=Menu)",10,30) + choice=0 + while choice==0: + if keydown(KEY_OK): + choice=1 + launch(darkmode,speed,applePower,appleScore) + elif keydown(KEY_BACKSPACE): + choice=2 + menu() + print("Score:",score) + + +try: + CGEXT_Set_Margin_Color( "black" ) +except: + print("fxCG Extension not supported") + +menu() diff --git a/ports/sh/examples/cg_NW_time_test.py b/ports/sh/examples/numworks/cg_NW_time_test.py similarity index 100% rename from ports/sh/examples/cg_NW_time_test.py rename to ports/sh/examples/numworks/cg_NW_time_test.py