gd3.py

Created by maelg0000

Created on October 04, 2025

5.09 KB


from kandinsky import *
from ion import *
from time import *

# Dimensions écran
WIDTH, HEIGHT = 320, 222

# Joueur
player_x, player_y = 20, 180
player_size = 20
gravity = 2
jump_force = -10
velocity_y = 0
is_ball = False
ball_gravity = 2
ball_direction = 1

# Sol et objets
ground_y = 200
objects = []  # contiendra tous les éléments du niveau
score = 0
speed = 5

SPIKE_HITBOX_SIZE = 20

# ---- Définition des niveaux ----
LEVEL1 = "___S__P_C__B___S___N__S___PC___S___"
LEVEL2 = "__S__S__S__C__P__B__N__C__S__P___"
LEVEL3 = "___P__C__S___S__P__C__B__S__N___"

# ---- Menu de sélection ----
fill_rect(0,0,WIDTH,HEIGHT,(255,255,255))
draw_string("Choisis ton niveau :", 80, 50, (0,0,0), (255,255,255))
draw_string("1 - LEVEL1", 100, 80, (0,0,0), (255,255,255))
draw_string("2 - LEVEL2", 100, 110, (0,0,0), (255,255,255))
draw_string("3 - LEVEL3", 100, 140, (0,0,0), (255,255,255))

chosen_level = None
while chosen_level is None:
    if keydown(KEY_ONE): 
        chosen_level = LEVEL1
    elif keydown(KEY_TWO):
        chosen_level = LEVEL2
    elif keydown(KEY_THREE):
        chosen_level = LEVEL3
    sleep(0.1)

LEVEL = chosen_level  # Le niveau choisi devient la map du jeu

# ---- Fonctions ----
def draw_player(x, y):
    if is_ball:
        for i in range(player_size):
            for j in range(player_size):
                dx, dy = i - player_size//2, j - player_size//2
                if dx*dx + dy*dy <= (player_size//2)**2:
                    set_pixel(x+i, y+j, (0,0,255))
    else:
        fill_rect(x, y, player_size, player_size, (0,0,255))

def add_tile(symbol, x):
    if symbol == "S":  # Spike
        base = 30
        h = base
        y = ground_y - h
        hitbox_w, hitbox_h = SPIKE_HITBOX_SIZE, SPIKE_HITBOX_SIZE
        hitbox_x, hitbox_y = x+(base-hitbox_w)//2, ground_y-hitbox_h
        objects.append(["spike", x, y, base, h, hitbox_x, hitbox_y, hitbox_w, hitbox_h, False])
    elif symbol == "P":  # Plateforme
        w, h = 60, 10
        y = ground_y - 50
        objects.append(["platform", x, y, w, h])
    elif symbol == "C":  # Coin
        s = 10
        y = ground_y - 70
        objects.append(["coin", x, y, s, False])
    elif symbol == "B":  # Ball portal
        objects.append(["portal", x, ground_y-40, 20, 40, "ball"])
    elif symbol == "N":  # Normal portal
        objects.append(["portal", x, ground_y-40, 20, 40, "normal"])

def draw_ground():
    fill_rect(0, ground_y, WIDTH, HEIGHT-ground_y, (100,100,100))

# ---- Construire le niveau ----
tile_size = 40
for i, sym in enumerate(LEVEL):
    add_tile(sym, WIDTH + i*tile_size)

# ---- Boucle de jeu ----
while True:
    fill_rect(0, 0, WIDTH, HEIGHT, (255,255,255))
    draw_ground()
    
    # Joueur
    if is_ball:
        if keydown(KEY_OK):
            ball_direction *= -1
        velocity_y += ball_gravity * ball_direction
        player_y += velocity_y
        if player_y < 0: 
            player_y, velocity_y = 0, 0
        if player_y + player_size > ground_y: 
            player_y, velocity_y = ground_y-player_size, 0
    else:
        if keydown(KEY_OK) and velocity_y == 0:
            velocity_y = jump_force
        player_y += velocity_y
        velocity_y += gravity
        if player_y + player_size > ground_y:
            player_y, velocity_y = ground_y-player_size, 0
    
    draw_player(player_x, player_y)
    
    # Objets
    for obj in objects:
        if obj[0] == "spike":
            obj[1] -= speed
            x,y,base,h = obj[1], obj[2], obj[3], obj[4]
            for i in range(h):
                w = int(base * (i/h)) if i>0 else 1
                start_x = x + (base//2) - (w//2)
                fill_rect(start_x, y+i, w, 1, (255,0,0))
            obj[5] = obj[1] + (obj[3]-obj[7])//2
            obj[6] = ground_y - obj[7]
            if (player_x+player_size > obj[5] and player_x < obj[5]+obj[7] and
                player_y+player_size > obj[6]):
                draw_string("GAME OVER", 100,100,(0,0,0),(255,255,255))
                sleep(2); quit()
            if not obj[9] and player_x > x+base:
                score += 1; obj[9] = True
        
        elif obj[0] == "platform":
            obj[1] -= speed
            fill_rect(obj[1], obj[2], obj[3], obj[4], (0,200,0))
        
        elif obj[0] == "coin":
            obj[1] -= speed
            if not obj[4]:
                fill_rect(obj[1], obj[2], obj[3], obj[3], (255,215,0))
                if (player_x+player_size > obj[1] and player_x < obj[1]+obj[3] and
                    player_y+player_size > obj[2] and player_y < obj[2]+obj[3]):
                    score += 1
                    obj[4] = True
        
        elif obj[0] == "portal":
            obj[1] -= speed
            color = (0,200,200) if obj[5]=="ball" else (200,0,200)
            fill_rect(obj[1], obj[2], obj[3], obj[4], color)
            if (player_x+player_size > obj[1] and player_x < obj[1]+obj[3]):
                if obj[5] == "ball":
                    is_ball, velocity_y, ball_direction = True, 0, 1
                else:
                    is_ball, velocity_y = False, 0
    
    # Score
    draw_string("Score:"+str(score), 5,5,(0,0,0),(255,255,255))
    
    sleep(0.05)

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.