platformer.py

Created by ississkerrouche67

Created on January 17, 2026

5.01 KB


from kandinsky import fill_rect, draw_string
from ion import *
from random import randint
from time import sleep

# =====================
# CONSTANTES
# =====================
W = 320
H = 222

PLAYER_W = 10
PLAYER_H = 10

PLAT_W = 30
PLAT_H = 6

GRAVITY = 0.3
JUMP_VEL = -14
VITESSE = 0.4

NB_PLAT = 10

ITERATIONS_MOYENNAGE = 2
COEFF_PLATS = 0.5
SEGMENTATION = 150 

# =====================
# NIVEAUX
# =====================

ls_diff = ["Facile","Moyen","Difficile","Hardcore","Personnalise"]
DIFFICULTE=ls_diff[0]

nF=[40,0.2,-12,0.3,10]
nM=[30,0.3,-14,0.4,10]
nD=[25,0.4,-15,0.5,6]
nH=[20,0.5,-15,0.6,6]
nP=[PLAT_W,GRAVITY,JUMP_VEL,VITESSE,NB_PLAT]

ls_crit=[nF,nM,nD,nH,nP]

# =====================
# INITIALISATION
# =====================
px = W // 2
py = 0
vy = 0
ind_diff=0
s=0

score = 0

plats = []

# =====================
# MENU NIVEAUX
# =====================

def menu_niveaux():
    global ind_diff
    draw_string("PLATFORMER", 110, 60)
    draw_string("OK pour commencer", 76, 80)
    draw_string("^",int(W/2-6),113)
    draw_string(ls_diff[ind_diff], int(W/2-len(ls_diff[ind_diff])*4.5-3), 130)
    draw_string("v",int(W/2-6),150)
            
            
    if keydown(KEY_UP):
        while keydown(KEY_UP): pass
        ind_diff=(ind_diff+1)%5
        clear()
    if keydown(KEY_DOWN):
        while keydown(KEY_DOWN): pass
        ind_diff=(ind_diff-1)%5
        clear()

# =====================
# DEGRADE
# =====================

def lerp(c1, c2, t):
    return tuple(int(c1[i] + (c2[i] - c1[i]) * t) for i in range(3))


points = [
    (150, 255, 0),    # vert-jaune
    (0, 50, 200),    # bleu
    (128, 0, 200),  # violet
    (200, 50, 0),     # rouge
    (150, 255, 0)     # retour départ
]


segments = len(points) - 1
step = SEGMENTATION // segments

colors = [
    lerp(points[i], points[i + 1], j / step)
    for i in range(segments)
    for j in range(step)
][:SEGMENTATION]

def inverse(c):
    return (255-c[0],255-c[1],255-c[2])

def moyennage(c):
    for i in range(ITERATIONS_MOYENNAGE):
        m = (c[0]+c[1]+c[2])/3
        c = (int((c[0]-m)/2+m),int((c[1]-m)/2+m),int((c[2]-m)/2+m))
    return c

def somme_tuple(c2,c1):
    return ((c1[0]-c2[0]),(c1[1]-c2[1]),(c1[2]-c2[2]))

def coeff_tuple(c,coeff):
    return (int(c[0]*coeff),int(c[1]*coeff),int(c[2]*coeff))

def coloration_plats(c):
    return somme_tuple(coeff_tuple(inverse(c),COEFF_PLATS),moyennage(c))


# =====================
# DESSIN
# =====================

def clear():
    fill_rect(0, 0, W, H, (255,255,255))

def draw_player():
    fill_rect(int(px), int(py), PLAYER_W, PLAYER_H, (0,0,0))

def draw_plats():
    for p in plats:
        fill_rect(int(p[0]), int(p[1]), PLAT_W, PLAT_H, coloration_plats(colors[int(((score//30)%(SEGMENTATION-5)))]))

# =====================
# RESET / START
# =====================
def reset():
    global px, py, vy, score, plats, PLAT_W,GRAVITY,JUMP_VEL,VITESSE,NB_PLAT

    plats = []
    base_y = H - 40

    # plateforme de départ (garantie)
    start_x = W//2 - PLAT_W//2
    plats.append([start_x, base_y + PLAYER_H])

    # autres plateformes
    for i in range(1, NB_PLAT):
        plats.append([
            randint(0, W-PLAT_W),
            base_y - i*30
        ])

    px = start_x + PLAT_W//2 - PLAYER_W//2
    py = base_y
    vy = 0

    score = 0
    PLAT_W,GRAVITY,JUMP_VEL,VITESSE,NB_PLAT=ls_crit[ind_diff]

    clear()

# =====================
# PROGRAMME PRINCIPAL
# =====================

while keydown(KEY_OK): pass
while not keydown(KEY_OK):
    menu_niveaux()
while keydown(KEY_OK): pass

reset()

while True:

    # ----- INPUT -----
    if keydown(KEY_LEFT):
        px -= 3
    if keydown(KEY_RIGHT):
        px += 3

    if px < 0:
        px = 0
    if px > W - PLAYER_W:
        px = W - PLAYER_W

    # ----- PHYSIQUE -----
    vy += GRAVITY
    py += vy*VITESSE

    # ----- COLLISIONS -----
    if vy > 0:
        for p in plats:
            if (px + PLAYER_W > p[0] and px < p[0] + PLAT_W and
                py + PLAYER_H >= p[1] and py + PLAYER_H <= p[1] + PLAT_H):
                vy = JUMP_VEL
                break


    
    # ----- SCROLL -----
    if py < H // 3:
        dy = (H // 3) - py
        py = H // 3
        score += dy

        for p in plats:
            p[1] += dy
            if p[1] > H:
                p[0] = randint(0, W-PLAT_W)
                p[1] = randint(-20, 0)

    # ----- GAME OVER -----
    if py > H:
        clear()
        draw_string("GAME OVER", 113, 90)
        draw_string("Score: " + str(int(score)), 120-len(str(int(score)))*4, 110)
        draw_string("OK pour rejouer", 85, 140)

        while not keydown(KEY_OK):
            pass
        while keydown(KEY_OK):
            pass

        clear()
        
        while not keydown(KEY_OK):
            menu_niveaux()
        while keydown(KEY_OK):
            pass
        
        reset()
    
    if s==0:
      fill_rect(0, 0, W, H, colors[int(((score//30)%(SEGMENTATION-5)))])
      s=3
    else:
      s-=1
    # ----- AFFICHAGE -----
    
    draw_plats()
    draw_player()
    draw_string(str(int(score)), 5, 5)
    sleep(0.001)

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.