platformer.py

Created by maelg0000

Created on December 09, 2025

4.29 KB


from kandinsky import fill_rect as f,set_pixel as p,draw_string as ds,color as c
from ion import keydown as k
from time import sleep

W,H=320,222
WH=(255,255,255)
BL=(50,120,255)
BK=(0,0,0)
O=10
Wd=W-O*2
Hd=60
Wy=W//4
Hy=60
AS=0.08

def spk(x,y,t="d"):
 if t=="d":
  for i in range(5):f(x+i,y+i*2,10-i*2,2,BK)
 if t=="r":
  for i in range(6):f(x+i*2,y+i,2,10-i*2,BK)

def arr(s):
 n=0 if s=="u" else 23
 f(238,90,22,40,WH)
 spk(W-O-Wy+15,H-O*2-Hd-Hy+13+n,"r")

def nxt(y=0):
 f(W-O*4+3,H-O*3-3+y-3,10,16,WH)
 spk(W-O*4+3,H-O*3-3+y)

def anxt(d=0.2):
 for i in[0,1,2,3,2,1]:
  nxt(i)
  sleep(d)
  if k(4)or k(48):return True
 return False

def wn():
 while True:
  if anxt(0.2):break

def box(t="d"):
 e,cl=0,0
 if t=="d":
  for i in range(4):
   f(O+e,H-Hd-O+e,Wd-e*2,Hd-e*2,c(cl,cl,cl))
   e+=2;cl+=80
  f(O+e,H-Hd-O+e,Wd-e*2,Hd-e*2,WH)
 if t=="y":
  for i in range(4):
   f(W-O-Wy+e,H-O*2-Hd-Hy+e,Wy-e*2,Hy-e*2,c(cl,cl,cl))
   e+=2;cl+=80
  f(W-O-Wy+e,H-O*2-Hd-Hy+e,Wy-e*2,Hy-e*2,WH)

def txt(u="",t="",nx=False):
 box()
 if u!="":t=u+": "+t
 if len(t)>56:ds("trop long",0,0);sleep(0.1);return
 if len(t)>28:
  sp=28
  for i in range(28,0,-1):
   if t[i]==" ":
    sp=i
    break
  t1,t2=t[:sp],t[sp+1:]
  for i in range(len(t1)+1):ds(t1[:i],O*2,H-Hd+5);sleep(AS)
  for i in range(len(t2)+1):ds(t2[:i],O*2,H-Hd+20);sleep(AS)
 else:
  for i in range(len(t)+1):ds(t[:i],O*2,H-Hd+5);sleep(AS)
 if nx:nxt()

def ask(tu="OUI",td="NON"):
 box("y")
 arr("u")
 ds(tu,W-O-Wy+30,H-O*2-Hd-Hy+34)
 ds(td,W-O-Wy+30,H-O*2-Hd-Hy+11)
 pos="u"
 ch=False
 sleep(0.2)
 while not ch:
  if k(2):
   if pos=="u":arr("d");pos="d";sleep(0.2)
  if k(1):
   if pos=="d":arr("u");pos="u";sleep(0.2)
  if k(4):ch=True;sleep(0.2)
 f(0,0,W,H,WH)
 return pos

px,py=150.0,50.0
vx,vy=0.0,0.0
og=0
drx=0
npx,npy=240,172
dia=0
shw=0

f(0,0,W,H,WH)
f(0,198,W,24,(50,200,50))
RD=(255,50,50)

def dnpc(x,y,ex):
 f(x,y,20,20,RD)
 for i in range(2):
  f(x-i,y-i,20+2*i,1,BK)
  f(x-i,y+20-1+i,20+2*i,1,BK)
  f(x-i,y-i,1,20+2*i,BK)
  f(x+20-1+i,y-i,1,20+2*i,BK)
 ey=y+20//3
 esz=3
 if ex==1:
  ex1,ex2=x+20//2+2,x+3*20//4+1
 elif ex==-1:
  ex1,ex2=x+20//4-3,x+20//2-4
 else:
  ex1,ex2=x+20//3,x+2*20//3-esz
 for dy in range(esz):
  for dx in range(esz):
   p(ex1+dx,ey+dy,BK)
   p(ex2+dx,ey+dy,BK)

dnpc(npx,npy,-1)

while 1:
 ox,oy=int(px),int(py)
 opw,oph=pw if 'pw' in dir() else 20,ph if 'ph' in dir() else 20
 odrx=drx if 'drx' in dir() else 0
 
 if og:
  pw,ph=20,20
 else:
  if vy<-3:
   pw,ph=16,26
  elif vy>3:
   pw,ph=24,16
  else:
   pw,ph=20,20
 
 ocx=ox+(20-opw)//2
 ocy=oy+(20-oph)//2
 
 if not og:vy+=0.4
 
 vx=0
 if k(0):vx=-2.5;drx=-1
 elif k(3):vx=2.5;drx=1
 elif k(1) or k(2):drx=0
 
 if k(4) and og:
  vy=-8
  og=0
 
 px+=vx
 py+=vy
 
 if px<0:px=0
 if px>W-pw:px=W-pw
 
 og=0
 if py>=200-ph-3:
  py=200-ph-3
  vy=0
  og=1
 
 cx=int(px)+(20-pw)//2
 cy=int(py)+(20-ph)//2
 
 dist=abs(px-npx)+abs(py-npy)
 oshw=shw
 shw=1 if dist<50 else 0
 
 if ox!=int(px) or oy!=int(py) or opw!=pw or oph!=ph or odrx!=drx:
  f(ocx-2,ocy-2,opw+4,oph+4,WH)
 
 if not og:vy+=0.4
 
 vx=0
 if k(0):vx=-2.5;drx=-1
 elif k(3):vx=2.5;drx=1
 elif k(1) or k(2):drx=0
 
 if k(4) and og:
  vy=-8
  og=0
 
 px+=vx
 py+=vy
 
 if px<0:px=0
 if px>W-pw:px=W-pw
 
 og=0
 if py>=200-ph-3:
  py=200-ph-3
  vy=0
  og=1
 
 cx=int(px)+(20-pw)//2
 cy=int(py)+(20-ph)//2
 
 f(cx,cy,pw,ph,BL)
 
 for i in range(2):
  f(cx-i,cy-i,pw+2*i,1,BK)
  f(cx-i,cy+ph-1+i,pw+2*i,1,BK)
  f(cx-i,cy-i,1,ph+2*i,BK)
  f(cx+pw-1+i,cy-i,1,ph+2*i,BK)
 
 ey=cy+ph//3
 esz=3
 
 if drx==1:
  ex1=cx+pw//2+2
  ex2=cx+3*pw//4+1
 elif drx==-1:
  ex1=cx+pw//4-3
  ex2=cx+pw//2-4
 else:
  ex1=cx+pw//3
  ex2=cx+2*pw//3-esz
 
 for dy in range(esz):
  for dx in range(esz):
   p(ex1+dx,ey+dy,BK)
   p(ex2+dx,ey+dy,BK)
 
   f(0,198,W,24,(50,200,50))
 
 sleep(0.03)
 
 if k(5):break
 
 if k(12) and abs(px-npx)<50 and dia==0:
  txt("Voyageur","Bonjour aventurier! Je suis perdu.",True)
  wn()
  txt("","Peux-tu m'aider a retrouver mon chemin?",True)
  wn()
  rep=ask("OUI","NON")
  if rep=="u":
   txt("","Merci beaucoup! Va vers l'est.",True)
  else:
   txt("","Dommage... Bonne route alors.",True)
  wn()
  dia=1
  f(0,0,W,H,WH)
  f(0,198,W,24,(50,200,50))
  dnpc(npx,npy,0)
 
 if k(42):
  txt("Hero","Ceci est un dialogue de test!",True)
  wn()
  rep=ask("OUI","NON")
  txt("Hero","Tu as choisi: "+rep,True)
  wn()
  f(0,0,W,H,WH)
  f(0,198,W,24,(50,200,50))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.