fromkandinskyimport*fromionimport*fromtimeimport*fromrandomimport*# Dimensions écran
WIDTH,HEIGHT=320,222# Joueur
player_x,player_y=20,180player_size=20gravity=2jump_force=-10velocity_y=0is_ball=False# mode normal (carré) ou ball mode (cercle)
ball_gravity=2ball_direction=1# 1 = vers le bas, -1 = vers le haut
# Sol et plateformes
ground_y=200obstacles=[]# spikes
platforms=[]# plateformes
coins=[]# pièces
portals=[]# portails
speed=5score=0# Hitbox des spikes
SPIKE_HITBOX_SIZE=20# ---- Fonctions ----
defdraw_player(x,y):ifis_ball:# dessine un cercle (boule)
foriinrange(player_size):forjinrange(player_size):dx,dy=i-player_size//2,j-player_size//2ifdx*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))defcreate_spike():max_base=30base=randint(15,max_base)h=basex=WIDTHy=ground_y-hhitbox_w=min(base,SPIKE_HITBOX_SIZE)hitbox_h=SPIKE_HITBOX_SIZEhitbox_x=x+(base-hitbox_w)//2hitbox_y=ground_y-hitbox_hobstacles.append([x,y,base,h,False,hitbox_x,hitbox_y,hitbox_w,hitbox_h])defdraw_triangle(x,y,base,h,color):foriinrange(h):w=int(base*(i/h))start_x=x+(base//2)-(w//2)fill_rect(start_x,y+i,w,1,color)defcreate_platform():w=randint(40,80)h=10x=WIDTHy=randint(ground_y-80,ground_y-30)platforms.append([x,y,w,h])# chance d’ajouter une pièce
ifrandint(0,1)==0:coins.append([x+w//2-5,y-15,10,False])# x,y,size, collected
defdraw_ground():fill_rect(0,ground_y,WIDTH,HEIGHT-ground_y,(100,100,100))defdraw_coin(x,y,size):fill_rect(x,y,size,size,(255,215,0))defcreate_portal(mode):# mode = "ball" ou "normal"
portals.append([WIDTH,ground_y-40,20,40,mode])defdraw_portal(x,y,w,h,mode):color=(0,200,200)ifmode=="ball"else (200,0,200)fill_rect(x,y,w,h,color)# ---- Boucle de jeu ----
tick=0whileTrue:fill_rect(0,0,WIDTH,HEIGHT,(255,255,255))draw_ground()# Gestion joueur
ifis_ball:# ball mode: inversion gravité au clic
ifkeydown(KEY_OK):ball_direction*=-1velocity_y+=ball_gravity*ball_directionplayer_y+=velocity_y# Limites sol/plafond
ifplayer_y<0:player_y,velocity_y=0,0ifplayer_y+player_size>ground_y:player_y,velocity_y=ground_y-player_size,0else:# mode normal
ifkeydown(KEY_OK)andvelocity_y==0:velocity_y=jump_forceplayer_y+=velocity_yvelocity_y+=gravityifplayer_y+player_size>ground_y:player_y=ground_y-player_sizevelocity_y=0# plateformes
on_platform=Falseforplatinplatforms:px,py,pw,ph=platif (player_x+player_size>pxandplayer_x<px+pwandplayer_y+player_size>=pyandplayer_y+player_size<=py+ph+5andvelocity_y>=0):player_y=py-player_sizevelocity_y=0on_platform=Truedraw_player(player_x,player_y)# Génération obstacles/plateformes/portails
tick+=1iftick%60==0:r=randint(0,4)ifr==0:create_spike()elifr==1:create_platform()elifr==2:create_portal("ball")elifr==3:create_portal("normal")# Obstacles
forobsinobstacles:obs[0]-=speeddraw_triangle(obs[0],obs[1],obs[2],obs[3],(255,0,0))obs[5]=obs[0]+(obs[2]-obs[8])//2obs[6]=ground_y-obs[7]# Plateformes
forplatinplatforms:plat[0]-=speedfill_rect(plat[0],plat[1],plat[2],plat[3],(0,200,0))# Coins
forcoinincoins:x,y,s,collected=coincoin[0]-=speedifnotcollected:draw_coin(x,y,s)if (player_x+player_size>xandplayer_x<x+sandplayer_y+player_size>yandplayer_y<y+s):score+=1coin[3]=True# Portails
forportalinportals:portal[0]-=speeddraw_portal(portal[0],portal[1],portal[2],portal[3],portal[4])if (player_x+player_size>portal[0]andplayer_x<portal[0]+portal[2]):ifportal[4]=="ball":is_ball=Truevelocity_y=0ball_direction=1elifportal[4]=="normal":is_ball=Falsevelocity_y=0# Collision spikes + score
forobsinobstacles:x,y,base,h,passed,hit_x,hit_y,hit_w,hit_h=obsif (player_x+player_size>hit_xandplayer_x<hit_x+hit_wandplayer_y+player_size>hit_y):draw_string("GAME OVER",100,100,(0,0,0),(255,255,255))sleep(2)ifnotpassedandplayer_x>x+base:score+=1obs[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:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
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.