plateformer.py

Created by dkfe123

Created on February 04, 2025

7.61 KB

C’est un jeu de plateforme classique. 13 niveaux et d’autres à venir. Flèches pour se déplacer, flèche haut ou OK pour sauter. Parenthèse gauche pour inverser la gravité dans le niveau 10.


from math import *
from kandinsky import *
from ion import *
from time import *
nm,ns,b,gr=0,0,0,1
k=[KEY_ONE,KEY_TWO,KEY_THREE,KEY_FOUR,KEY_FIVE,KEY_SIX,KEY_SEVEN,KEY_EIGHT,KEY_NINE,KEY_MULTIPLICATION,KEY_DIVISION]
def init():
  return(0,0,20,10,10,10,100,[(125,0,0),(255,0,0),(255,0,255),(255,255,0),(0,255,255),(0,255,0),(255,255,255),(125,0,0),(255,255,255)])
s,e,j,ax,ay,x,y,c = init()

def initniv(ni):
  if ni==1:
    return([0,0,150,320,150,2,315,0,30,150,9,80,150,0,0,11,0,145,320,240])
  if ni==2:
    return([0,0,150,320,150,0,100,30,5,120,11,105,25,5,5,0,60,60,40,5,0,40,100,60,5,2,315,0,30,145,11,0,145,105,5,11,105,145,220,5,11,65,55,40,5,11,45,95,60,5])
  if ni==3:
    return([0,0,150,320,150,1,60,140,10,5,1,120,140,10,5,1,180,140,10,5,1,240,140,10,5,2,315,0,30,150,11,0,145,325,5,9,40,150,0,0])
  if ni==5:
    return([11,0,145,320,5,2,315,0,30,150,0,0,150,320,150,1,0,60,320,10,0,0,0,320,60,3,50,140,20,5,3,140,140,20,5,3,220,140,20,5])
  if ni==4:
    return([0,0,150,320,150,11,0,145,85,5,0,80,50,5,120,11,90,145,240,5,11,85,45,5,5,0,50,110,30,5,11,55,105,30,5,0,0,60,20,5,11,0,55,25,5,3,100,135,30,10,1,140,140,50,5,2,315,0,30,150,8,250,150,0,0])
  if ni==6:
    return([0,0,150,130,150,11,0,145,135,5,4,130,160,120,150,0,250,150,140,150,11,255,145,135,5,2,315,0,30,150])
  if ni==7:
    return([0,0,130,60,5,11,0,125,65,5,1,0,200,320,50,0,120,130,5,5,11,125,125,5,5,0,170,130,5,5,11,175,125,5,5,0,220,130,5,5,11,225,125,5,5,0,260,130,5,5,11,265,125,5,5,3,305,129,15,5,2,300,0,20,15])
  if ni==8:
    return([0,0,150,320,150,6,80,70,150,70,2,315,0,5,150,0,100,0,5,150,11,0,145,320,5])
  if ni==9:
    return([4,0,30,320,240,2,315,0,30,30])
  if ni==10:
    return([0,0,150,70,150,11,1,145,75,5,3,50,140,20,5,1,70,155,250,100,2,315,0,5,155,1,50,0,15,5,1,130,0,15,5,1,210,0,15,5,1,290,0,20,5])
  if ni==11:
    return([0,0,150,320,150,11,0,145,85,5,0,80,50,5,100,11,85,45,5,100,1,85,70,115,80,0,200,50,5,100,11,205,45,5,5,0,140,60,5,5,11,145,55,5,5,11,210,145,120,5,2,315,0,5,150])
  if ni==12:
    return([0,0,150,100,150,1,100,200,50,40,7,100,149,50,151,0,150,150,170,150,2,315,0,5,150,8,80,150,0,0,11,0,145,320,5])
  if ni==13:
    return([0,0,150,100,110,1,100,170,220,70,2,315,0,5,170,12,100.1,100,250,1])
  return([0,0,150,320,150,11,0,145,325,5])

def niv(n):
  to = 0
  fill_rect(0,0,320,240,(0,0,0))
  for i in range(0,len(n)-4,5):
    if n[i]==6:
      fill_rect(n[i+1],n[i+2],5,5,(c[5]))
      fill_rect(n[i+3],n[i+4],5,5,c[5])
    else:
      if n[i]>7 and n[i]<12:
        decor(n[i+1],n[i+2],n[i],n[i+3])
      else:
        if n[i]!=12:
          fill_rect(n[i+1],n[i+2],n[i+3],n[i+4],c[n[i]])
  return None

def to(x,y,n):
  for i in range(0,len(n),5):
      if n[i]==0:
        if n[i+1]<x and n[i+3]+n[i+1]>x -20:
         if n[i+2]<y + 30 and n[i+4]+n[i+2]>y+10:
            return(1)
      else:
        if n[i]==12:
          if n[i+2]<x and 60+n[i+2]>x-20:
            pp=1000*(n[i+1]-floor(n[i+1]))
            if pp<y+30 and pp+5>y+10:
              return(1)
  return False

def f(e):
  return(floor(e))
 
def game(x,y,n,e,s,ns):
  t = 0
  d=0
  ax = x
  ay = y
  if keydown(KEY_UP) or keydown(KEY_OK):
    if s==0 or e*gr>0:
      if to(x+1,y,n) and keydown(KEY_RIGHT):
        s=1
        e=-2*gr
      if to(x-1,y,n) and keydown(KEY_LEFT):
        s=1
        e=-2*gr
    if s==0:
      if to(x,y+2,n)==1:
        ns=ns+1
        s = 1
        e = -3*gr
      else:
        if gr==-1 and y==-j/2:
          ns=ns+1
          s=1
          e=-3*gr
  e = e+gr/10
  y = y+f(e)
  if to(x,y,n)==1:
    y = y - f(e)
    if e>gr*0.1:
      s = 0
    e=0
  if keydown(KEY_LEFT):
    x = x-1
    if to(x,y,n)==1:
      x=x+1
  if keydown(KEY_RIGHT):
    x = x+1
    if to(x,y,n)==1:
      x =x-1
  if x<j:
    x = j
  if x>320:
    x = 320
  if y<0-j/2:
    if gr==-1:
      e=0
      s=0
    y = 0-j/2
  if y>200:
    y,e=200,0
  if ax !=x or ay != y:
    fill_rect(floor(ax-j),floor(ay+j/2),j,j,(0,0,0))
    fill_rect(floor(x-j),floor(y+j/2),j,j,(0,0,255))
  return(x,y,e,ns,s)

def change(ni,v):
  for v in range(0,len(k)):
    if keydown(k[v]):
      return(v+1)
  return(ni)

def rangg(note):
  r,n=[20,18,15,12,10,5,0],["Diamant","Platine","Or","Argent","Bronze","Carton","PVC"]
  for i in range(0,len(r)):
    if note>=r[i]:
      return(n[i])

def decor(xd,yd,ty,xdi):
  if ty==8:
    fill_rect(xd-2,yd-30,5,30,(125,0,0))
    fill_rect(xd-15,yd-60,30,30,(0,125,0))
  if ty==9:
    fill_rect(xd,yd-15,2,15,(125,0,0))
    fill_rect(xd-3,yd-21,8,8,(255,255,255))
    fill_rect(xd-2,yd-20,6,6,(255,255,0))
  #if ty==10:
   # fill_rect(xd,yd-40,120,80,(255,255,255))
    #fill_rect(xd-10,yd-60,140,30,(125,0,0))
  if ty==11:
    fill_rect(xd-5,yd,xdi,5,(0,125,0))
  return(0)
  
def score(ns,nm):
  if ch!=0:
    draw_string("Tricheur !!! 0/20 !!!",50,50)
    return(0)
  else:
    nn=20-0.125*(ns-28+abs(ns-28))-0.5*nm
    note=0.5*(nn+abs(nn))
    draw_string(("FIN"),50,50)
    return(note)

ni=10
t=0
d=0
n=initniv(ni)
niv(n)
fill_rect(floor(x-j),floor(y+j/2),j,j,(0,0,255))
ch=0
ns=0

def mplat(nn):
  for i in range(0,len(nn),5):
    if nn[i]==12:
      fill_rect(nn[i+2],floor(1000*(nn[i+1]-floor(nn[i+1]))),60,5,(0,0,0))
      pp=nn[i+2]
      nn[i+2]=pp+nn[i+4]
      if nn[i+2]>nn[i+3]:
        nn[i+2]=nn[i+3]
        nn[i+4]=-nn[i+4]
      else:
        if nn[i+2]<floor(nn[i+1]):
          nn[i+2]=floor(nn[i+1])
          nn[i+4]=-n[i+4]
      fill_rect(nn[i+2],floor(1000*(nn[i+1]-floor(nn[i+1]))),60,5,(255,255,255))
  return(nn)

while True:
  x,y,e,ns,s = game(x,y,n,e,s,ns)
  n=mplat(n)
  if to(x,y,n)==1:
    x=x+2*n[i+4]
  p,g=0,0
  if ni==10:
    draw_string("Pressez ( ", 100,120)
  for i in range(0,len(n)-4,5):
    if n[i]!=0 and n[i]!=12:
      if n[i]>7:
        decor(n[i+1],n[i+2],n[i],n[i+3])
      if n[i]==6:
        fill_rect(n[i+1],n[i+2],5,5,c[5])
        fill_rect(n[i+3],n[i+4],5,5,c[5])
        if n[i+1]<x and n[i+1]+5>x-20:
          if n[i+2]<y+30 and n[i+2]+5>y+10:
            fill_rect(floor(x-j),floor(y+j/2),j,j,(0,0,0))
            x,y=n[i+3],n[i+4]+20*gr
        if n[i+3]<x and n[i+3]+5>x-20:
          if n[i+2]<y+30 and n[i+2]+5>y+10:
            fill_rect(floor(x-j),floor(y+j/2),j,j,(0,0,0))
            x,y=n[i+1],n[i+2]+20
      else:
        if n[i]==3 or n[i]==7:
          fill_rect(n[i+1],n[i+2],n[i+3],n[i+4],c[n[i]])
        if n[i+1]<x and n[i+3]+n[i+1]>x-20:
          if n[i+2]<y+30 and n[i+2]+n[i+4]>y+10:
            if n[i]==4:
              fill_rect(n[i+1],n[i+2],n[i+3],n[i+4],c[n[i]])
              if x<j+1:
                e=0
              if e<-4*gr:
                e=-4*gr
              if e>2.5*gr:
                e=2.5*gr
              e=e-0.2*gr
              if keydown(KEY_DOWN):
                e=e+0.3*gr
              fill_rect(n[i+1],n[i+2],n[i+3],n[i+4],c[n[i]])
              fill_rect(floor(x-j),floor(y+j/2),j,j,(0,0,255))
              if keydown(KEY_DOWN):
                fill_rect(floor(x-j),floor(y+j/2),j,j,(0,0,255))
            if n[i]<3:
              s,e,j,ax,ay,x,y,c=init()
            if n[i]==2:
              g=1
            if n[i]==1:
              p=1
            if n[i]==3:
              e=-4
  if g==1:
    ni=ni+1
    gr=1
    n=initniv(ni)
    if False:
      print(ni,len(n),n)
    niv(n)
    if ni==14:
      note=score(ns,nm)
      print("Ta note est ",note)
      print("Tu as la medaille de ",rangg(note))
  if p==1:
    nm=nm+1
    niv(n)
    gr=1
  if ni==10:
    if keydown(KEY_LEFTPARENTHESIS):
      b=1
    else:
      if b==1:
        gr=-gr
      b=0
  if keydown(KEY_RIGHTPARENTHESIS) and keydown(KEY_EXE):
    v=0
    v=change(ni,v)
    if v!=ni:
      ch=ch+0.5*(v-ni+abs(v-ni))
      print("Changement de niveau vers le",v)
      ni,gr=v,1
      s,e,j,ax,ay,x,y,c=init()
      n=initniv(v)
      niv(n)

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.