platformer.py

Created by golem64

Created on June 14, 2021

6.35 KB

Voici un petit jeu de plateforme personnalisable pour votre Numworks, Déplacez vous avec les flèches droite et gauche et sautez avec OK. Vous pouvez modifier les variables et arguments marqués d’un #! en bout de ligne pour créer votre propre niveau, les variables avec un #? en bout de ligne peuvent contenir soit un 0 soit un 1. Il existe plusieurs types de plateformes: Les classiques (0,0,0) qui sont des plateformes normales, la glace (0,255,255) qui vous fait glisser, le trampoline (255,0,255) qui vous fait automatiquement rebondir un peu plus haut que le saut classique, le miel (255,255,0) qui vous accroche et vous ralentis, la lave (255,0,0) qui vous ramène à votre point de départ, et l’arrivée (0,255,0) qui stoppe le jeu et vous fait gagner (vous pouvez également quitter le jeu à tout moment en appuyant sur le bouton On/Off). L’éditeur de niveau arrivera bientôt. Version 1.2


#Version 1.2
from kandinsky import *
from time import *
from ion import *
#change colors by atleast 8
#black=(0,0,0) / white=(248,252,248)
c={"normal":(0,0,0),"ice":(0,252,248),"bouncy":(248,0,248),"sticky":(248,252,0),"lava":(248,0,0),"goal":(0,252,0)}#!
playercolor=(255,10,0)#!
bgcolor=(200,200,200)#!
startpos=[310,200,10]#!
posx=startpos[0]
posy=startpos[1]
size=startpos[2]
oldpos=[posx,posy,size]
drawpos=[160,112,get_pixel(160,112)]
forcex=0#!
forcey=0#!
gravity=0.02#!
jump=8#!
jumpforce=0#?
speed=0.5#!
speedlimit=5#!
olforcex=forcex
onground=0#?
onice=0#?
#slopes=1
key=[]

#errors=0
if playercolor in c.values(): print("[!] Player color is equal to a platform color")
if bgcolor in c.values(): print("[!] Background color is equal to a platform color")
if startpos[0]>320 or startpos[0]<0: print("[!] X start position out of limits")
if startpos[1]>222 or startpos[0]<0: print("[!] Y start position out of limits")
if startpos[2]<1: print("[!] Start size is too small")
if startpos[2]>222: print("[!] Start size is too big")
if gravity>=1: print("[!] Gravity is too slow")
if jump>222: print("[!] Jump is too big")
if speed>320: print("[!] Speed is too big")
if speedlimit/speed!=int(speedlimit/speed): print("[!] Speed can't reach speed limit")
#def toolbox():

def touch():
  touched=[[],[],[],[]]
  #down,up,left,right
  for i in range(size):
    d=get_pixel(posx+i,posy+1)
    u=get_pixel(posx+i,posy-size)
    l=get_pixel(posx-1,posy-i)
    r=get_pixel(posx+size,posy-i)
    if not d in touched[0]: touched[0].append(d)
    if not u in touched[1]: touched[1].append(u)
    if not l in touched[2]: touched[2].append(l)
    if not r in touched[3] : touched[3].append(r)
  return touched
fill_rect(0,0,320,222,bgcolor)
fill_rect(posx,posy+1,size,-size,playercolor)
#while not "onOff" in key:
#  key=get_keys()
#  if key: set_pixel(drawpos[0],drawpos[1],drawpos[2])
#  if "left" in key: drawpos[0]-=1
#  if "right" in key: drawpos[0]+=1
#  if "up" in key: drawpos[1]-=1
#  if "down" in key: drawpos[1]+=1
#  drawpos[2]=get_pixel(drawpos[0],drawpos[1])
#  if "OK" in key: drawpos[2]=(0,0,0)
#  set_pixel(drawpos[0],drawpos[1],(0,0,255))
#  sleep(0.1)
fill_rect(0,210,31,1,c["normal"])#!
fill_rect(60,180,40,1,c["normal"])#!
fill_rect(100,148,30,1,c["normal"])#!
fill_rect(160,120,40,1,c["ice"])#!
fill_rect(200,90,30,1,c["normal"])#!
fill_rect(180,60,30,1,c["normal"])#!
fill_rect(130,30,30,1,c["normal"])#!
fill_rect(200,210,120,1,c["ice"])#!
fill_rect(200,210,5,1,c["bouncy"])#!
fill_rect(130,1,30,1,c["lava"])#!
fill_rect(80,30,20,1,c["goal"])#!
fill_rect(30,211,1,11,c["bouncy"])
fill_rect(200,211,1,11,c["bouncy"])
fill_rect(100,149,30,1,c["bouncy"])#!
fill_rect(1,0,1,210,c["sticky"])
fill_rect(220,180,30,1,c["sticky"])
#fill_rect(300,0,1,222,c["normal"])#!
#fill_rect(50,0,1,222,c["normal"])#!
key=[]
#sleep(1)
touched=touch()
while not "onOff" in key:
  key=[]
  if keydown(KEY_LEFT): key.append("left")
  if keydown(KEY_RIGHT): key.append("right")
  if keydown(KEY_OK): key.append("OK")
  if keydown(KEY_ONOFF): key.append("onOff")
  touched=touch()
  if key or forcex or forcey: oldpos=[posx,posy,size]
  if "left" in key and not posx==0: forcex-=speed+(forcex>0)*speed
  if "right" in key and not posx==320-size: forcex+=speed+(forcex<0)*speed
  if not onice:
    if not "left" in key and not "right" in key and ((jumpforce and onground) or (not jumpforce)):
      if forcex>0: forcex-=speed
      if forcex<0: forcex+=speed
    if forcex>speedlimit: forcex-=speed
    if forcex<-speedlimit: forcex+=speed
  if "OK" in key and onground:
    forcey=-jump
    if c["sticky"] in touched[2] or c["sticky"] in touched[3] and not forcex: forcey+=jump
    else: onground=0
#  if "toolbox" in key: toolbox()
  for i in range(forcey):
    for j in range(size):
      if get_pixel(posx+j,posy+i+1)!=bgcolor and not onground:
        onground=1
        posy+=i
        forcey=0
#  for i in range(0,forcey+(posy+forcey<0)*(-(forcey+posy)),-1):
  for i in range(0,forcey,-1):
    for j in range(size):
      if get_pixel(posx+j,posy-size+i)!=bgcolor and forcey:
        onground=0
        posy+=i
        forcey=0
  under=0
  for i in range(size):
    if get_pixel(posx+i,posy+1)!=bgcolor: under=1
  if not under: onground=0
  if c["sticky"] in touched[2] or c["sticky"] in touched[3]:
    if forcey>1: forcey=1
    if forcey<0: forcey=0
    onground=1
    if not "OK" in key: onground=0
  if c["sticky"] in touched[0]:
    forcey=0
  if c["sticky"] in touched[1]:
    forcey=0
    onground=1
    onice=0
  posy+=int(forcey)
  if not onground: forcey+=1
  olforcex=forcex
#  for i in range(forcex-(posx+forcex>320)*((posx+forcex)-320)):
  for i in range(forcex):
    for j in range(size):
      if get_pixel(posx+size+i,posy-j)!=bgcolor and forcex:
        posx+=i
        forcex=0
  for i in range(0,forcex,-1):
    for j in range(size):
      if get_pixel(posx+i-1,posy-j)!=bgcolor and forcex:
        posx+=i
        forcex=0
  if c["sticky"] in touched[0] or c["sticky"] in touched[1]:
    if forcex>1: forcex=1
    if forcex<-1: forcex=-1
  posx+=int(forcex)
  touched=touch()
  if c["ice"] in touched[0]: onice=1
  elif not bgcolor in touched[0]: onice=0
  if c["bouncy"] in touched[0] and onground:
    forcey-=jump*1.25
    onground=0
  if c["bouncy"] in touched[1] and not onground:
    forcey+=jump*1.25
  if c["bouncy"] in touched[2] and onground and olforcex: forcex=-olforcex+(speed*(forcex==speed)*2)
  if c["bouncy"] in touched[3] and onground and olforcex: forcex=-olforcex-(speed*(forcex==-speed)*2)
#  if forcex=speed
  death=c["lava"]
  if death in touched[0] or death in touched[1] or death in touched[2] or death in touched[3]:
    forcex,forcey=0,0
    posx,posy=startpos[0],startpos[1]
    size=startpos[2]
  win=c["goal"]
  if win in touched[0] or win in touched[1] or win in touched[2] or win in touched[3]:
    draw_string("You win !",230,0,(0,0,0),bgcolor)
    key={"onOff"}
#  if slopes and onground:
#    under=[]
#    for i in range(int(size/2)):
#      if get_pixel(,)
  if oldpos: fill_rect(oldpos[0],oldpos[1]+1,oldpos[2],-oldpos[2],bgcolor)
  fill_rect(posx,posy+1,size,-size,playercolor)
  oldpos=[]
#  draw_string(" "*20,220,0)
#  draw_string(str(forcex)+","+str(olforcex),220,0)
  sleep(gravity)
#fill_rect(160,179,40,1,(0,0,0))
#set_pixel(posx,posy,(0,0,255))
#for i in range(size):
#  set_pixel(posx+i,posy+1,(0,0,255))
#  set_pixel(posx+i,posy-size,(0,0,255))
#  set_pixel(posx-1,posy-i,(0,0,255))
#  set_pixel(posx+size,posy-i,(0,0,255))