fromkandinskyimport*fromionimport*fromtimeimport*# Dimensions écran
WIDTH,HEIGHT=320,222# Joueur
player_x,player_y=20,180player_size=20gravity=2jump_force=-10velocity_y=0is_ball=Falseball_gravity=2ball_direction=1# Sol et objets
ground_y=200objects=[]# contiendra tous les éléments du niveau
score=0speed=5SPIKE_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=Nonewhilechosen_levelisNone:ifkeydown(KEY_ONE):chosen_level=LEVEL1elifkeydown(KEY_TWO):chosen_level=LEVEL2elifkeydown(KEY_THREE):chosen_level=LEVEL3sleep(0.1)LEVEL=chosen_level# Le niveau choisi devient la map du jeu
# ---- Fonctions ----
defdraw_player(x,y):ifis_ball: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:fill_rect(x,y,player_size,player_size,(0,0,255))defadd_tile(symbol,x):ifsymbol=="S":# Spike
base=30h=basey=ground_y-hhitbox_w,hitbox_h=SPIKE_HITBOX_SIZE,SPIKE_HITBOX_SIZEhitbox_x,hitbox_y=x+(base-hitbox_w)//2,ground_y-hitbox_hobjects.append(["spike",x,y,base,h,hitbox_x,hitbox_y,hitbox_w,hitbox_h,False])elifsymbol=="P":# Plateforme
w,h=60,10y=ground_y-50objects.append(["platform",x,y,w,h])elifsymbol=="C":# Coin
s=10y=ground_y-70objects.append(["coin",x,y,s,False])elifsymbol=="B":# Ball portal
objects.append(["portal",x,ground_y-40,20,40,"ball"])elifsymbol=="N":# Normal portal
objects.append(["portal",x,ground_y-40,20,40,"normal"])defdraw_ground():fill_rect(0,ground_y,WIDTH,HEIGHT-ground_y,(100,100,100))# ---- Construire le niveau ----
tile_size=40fori,syminenumerate(LEVEL):add_tile(sym,WIDTH+i*tile_size)# ---- Boucle de jeu ----
whileTrue:fill_rect(0,0,WIDTH,HEIGHT,(255,255,255))draw_ground()# Joueur
ifis_ball:ifkeydown(KEY_OK):ball_direction*=-1velocity_y+=ball_gravity*ball_directionplayer_y+=velocity_yifplayer_y<0:player_y,velocity_y=0,0ifplayer_y+player_size>ground_y:player_y,velocity_y=ground_y-player_size,0else:ifkeydown(KEY_OK)andvelocity_y==0:velocity_y=jump_forceplayer_y+=velocity_yvelocity_y+=gravityifplayer_y+player_size>ground_y:player_y,velocity_y=ground_y-player_size,0draw_player(player_x,player_y)# Objets
forobjinobjects:ifobj[0]=="spike":obj[1]-=speedx,y,base,h=obj[1],obj[2],obj[3],obj[4]foriinrange(h):w=int(base*(i/h))ifi>0else1start_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])//2obj[6]=ground_y-obj[7]if (player_x+player_size>obj[5]andplayer_x<obj[5]+obj[7]andplayer_y+player_size>obj[6]):draw_string("GAME OVER",100,100,(0,0,0),(255,255,255))sleep(2);quit()ifnotobj[9]andplayer_x>x+base:score+=1;obj[9]=Trueelifobj[0]=="platform":obj[1]-=speedfill_rect(obj[1],obj[2],obj[3],obj[4],(0,200,0))elifobj[0]=="coin":obj[1]-=speedifnotobj[4]:fill_rect(obj[1],obj[2],obj[3],obj[3],(255,215,0))if (player_x+player_size>obj[1]andplayer_x<obj[1]+obj[3]andplayer_y+player_size>obj[2]andplayer_y<obj[2]+obj[3]):score+=1obj[4]=Trueelifobj[0]=="portal":obj[1]-=speedcolor=(0,200,200)ifobj[5]=="ball"else (200,0,200)fill_rect(obj[1],obj[2],obj[3],obj[4],color)if (player_x+player_size>obj[1]andplayer_x<obj[1]+obj[3]):ifobj[5]=="ball":is_ball,velocity_y,ball_direction=True,0,1else: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:
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.