gd2.py

Created by maelg0000

Created on October 04, 2025

5.49 KB


from kandinsky import *
from ion import *
from time import *
from random 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  # mode normal (carré) ou ball mode (cercle)
ball_gravity = 2
ball_direction = 1  # 1 = vers le bas, -1 = vers le haut

# Sol et plateformes
ground_y = 200
obstacles = []   # spikes
platforms = []   # plateformes
coins = []       # pièces
portals = []     # portails
speed = 5
score = 0

# Hitbox des spikes
SPIKE_HITBOX_SIZE = 20

# ---- Fonctions ----

def draw_player(x, y):
    if is_ball:
        # dessine un cercle (boule)
        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:
        # carré
        fill_rect(x, y, player_size, player_size, (0,0,255))

def create_spike():
    max_base = 30
    base = randint(15, max_base)
    h = base
    x = WIDTH
    y = ground_y - h
    
    hitbox_w = min(base, SPIKE_HITBOX_SIZE)
    hitbox_h = SPIKE_HITBOX_SIZE
    hitbox_x = x + (base - hitbox_w) // 2
    hitbox_y = ground_y - hitbox_h
    
    obstacles.append([x, y, base, h, False, hitbox_x, hitbox_y, hitbox_w, hitbox_h])

def draw_triangle(x, y, base, h, color):
    for i in range(h):
        w = int(base * (i / h))
        start_x = x + (base // 2) - (w // 2)
        fill_rect(start_x, y + i, w, 1, color)

def create_platform():
    w = randint(40, 80)
    h = 10
    x = WIDTH
    y = randint(ground_y-80, ground_y-30)
    platforms.append([x, y, w, h])
    # chance d’ajouter une pièce
    if randint(0,1) == 0:
        coins.append([x + w//2 - 5, y - 15, 10, False])  # x,y,size, collected

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

def draw_coin(x, y, size):
    fill_rect(x, y, size, size, (255,215,0))

def create_portal(mode):
    # mode = "ball" ou "normal"
    portals.append([WIDTH, ground_y-40, 20, 40, mode])

def draw_portal(x, y, w, h, mode):
    color = (0,200,200) if mode == "ball" else (200,0,200)
    fill_rect(x, y, w, h, color)

# ---- Boucle de jeu ----

tick = 0
while True:
    fill_rect(0, 0, WIDTH, HEIGHT, (255,255,255))
    draw_ground()
    
    # Gestion joueur
    if is_ball:
        # ball mode: inversion gravité au clic
        if keydown(KEY_OK):
            ball_direction *= -1
        velocity_y += ball_gravity * ball_direction
        player_y += velocity_y
        # Limites sol/plafond
        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:
        # mode normal
        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 = ground_y - player_size
            velocity_y = 0
        # plateformes
        on_platform = False
        for plat in platforms:
            px, py, pw, ph = plat
            if (player_x + player_size > px and player_x < px+pw and
                player_y + player_size >= py and player_y + player_size <= py+ph+5 and
                velocity_y >= 0):
                player_y = py - player_size
                velocity_y = 0
                on_platform = True
    
    draw_player(player_x, player_y)
    
    # Génération obstacles/plateformes/portails
    tick += 1
    if tick % 60 == 0:
        r = randint(0,4)
        if r == 0:
            create_spike()
        elif r == 1:
            create_platform()
        elif r == 2:
            create_portal("ball")
        elif r == 3:
            create_portal("normal")
    
    # Obstacles
    for obs in obstacles:
        obs[0] -= speed
        draw_triangle(obs[0], obs[1], obs[2], obs[3], (255,0,0))
        obs[5] = obs[0] + (obs[2] - obs[8]) // 2
        obs[6] = ground_y - obs[7]
    
    # Plateformes
    for plat in platforms:
        plat[0] -= speed
        fill_rect(plat[0], plat[1], plat[2], plat[3], (0,200,0))
    
    # Coins
    for coin in coins:
        x,y,s,collected = coin
        coin[0] -= speed
        if not collected:
            draw_coin(x, y, s)
            if (player_x + player_size > x and player_x < x+s and
                player_y + player_size > y and player_y < y+s):
                score += 1
                coin[3] = True
    
    # Portails
    for portal in portals:
        portal[0] -= speed
        draw_portal(portal[0], portal[1], portal[2], portal[3], portal[4])
        if (player_x + player_size > portal[0] and player_x < portal[0]+portal[2]):
            if portal[4] == "ball":
                is_ball = True
                velocity_y = 0
                ball_direction = 1
            elif portal[4] == "normal":
                is_ball = False
                velocity_y = 0
    
    # Collision spikes + score
    for obs in obstacles:
        x,y,base,h,passed,hit_x,hit_y,hit_w,hit_h = obs
        if (player_x + player_size > hit_x and player_x < hit_x+hit_w and
            player_y + player_size > hit_y):
            draw_string("GAME OVER", 100, 100, (0,0,0), (255,255,255))
            sleep(2)
            
        if not passed and player_x > x+base:
            score += 1
            obs[4] = True
    
    # Score affiché
    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.