fromkandinskyimport*fromionimport*fromtimeimportsleep,monotonicpac_man_pos=[8,8]vitesse=2depart=0precedent=0defcreation_terrain():globalterraingrille=[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],[1,0,0,0,0,0,0,0,0,0,0,0,0,1,0],[1,0,1,1,1,1,0,1,1,1,1,1,0,1,0],[1,0,1,1,1,1,0,1,1,1,1,1,0,1,0],[1,0,1,1,1,1,0,1,1,1,1,1,0,1,0],[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[1,0,1,1,1,1,0,1,1,0,1,1,1,1,0],[1,0,1,1,1,1,0,1,1,0,1,1,1,1,0],[1,0,0,0,0,0,0,1,1,0,0,0,0,1,0],[1,1,1,1,1,1,0,1,1,1,1,1,0,1,0],[1,1,1,1,1,1,0,1,1,1,1,1,0,0,0],[1,1,1,1,1,1,0,1,1,0,0,0,0,0,0],[1,1,1,1,1,1,0,1,1,0,1,1,1,1,0],[1,1,1,1,1,1,0,1,1,0,1,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0]]terrain=[[0for_inrange(29)]for_inrange(29)]foriinrange(len(grille)):forjinrange(len(grille[i])):terrain[i][j]=grille[i][j]terrain[len(terrain)-1-i][j]=grille[i][j]terrain[i][len(terrain[i])-1-j]=grille[i][j]terrain[len(terrain)-1-i][len(terrain[i])-1-j]=grille[i][j]defcrea_boules():globalterrain,boules_restantes,nb_boules_restantesboules_restantes=[]nb_boules_restantes=0foriinrange(len(terrain)):boules_restantes.append([])forjinterrain[i]:ifnotj:boules_restantes[i].append(True)nb_boules_restantes+=1else:boules_restantes[i].append(False)defboule(i,j):fill_rect(8*j+1,8*i+2,6,4,(255,255,255))fill_rect(8*j+2,8*i+1,4,6,(255,255,255))defdessine_boules():globalboules_restantesforiinrange(len(boules_restantes)):forjinrange(len(boules_restantes[i])):ifboules_restantes[i][j]:boule(i,j)defdessine_terrain():fill_rect(0,0,320,225,(0,0,0))draw_string("Temps",240,50,(255,255,0),(0,0,0))draw_string("Reste :",240,10,(255,255,0),(0,0,0))foriinrange(len(terrain)):forjinrange(len(terrain[i])):ifterrain[i][j]:couleur=(0,0,255)else:couleur=(0,0,0)largeur=3hauteur=3posx=8*j+3posy=8*i+3ifterrain[i][j]:ifterrain[i-1][j]:posy-=3hauteur+=3ifterrain[(i+1)%len(terrain)][j]:hauteur+=3ifterrain[i][j-1]:posx-=3largeur+=3ifterrain[i][(j+1)%len(terrain[i])]:largeur+=3fill_rect(posx,posy,largeur,hauteur,couleur)defpacman():globalterrain,pac_man_pos,boules_restantes,nb_boules_restantes,depart,precedentdeplacement=[keydown(KEY_UP),keydown(KEY_DOWN),keydown(KEY_LEFT),keydown(KEY_RIGHT)]ifprecedent!=int(monotonic()-depart):draw_string(str(int(monotonic()-depart))+"",240,70,(255,255,0),(0,0,0))precedent=int(monotonic()-depart)ifmax(deplacement):#on ne dessine que si une touche est appuyée
#on efface la stripe précédent
fill_rect(pac_man_pos[0]-2,pac_man_pos[1]-2,12,11,(0,0,0))#on définit la prochaine position
pac_man_pos_temp=[pac_man_pos[0]+vitesse*(deplacement[3]-deplacement[2]),pac_man_pos[1]+vitesse*(deplacement[1]-deplacement[0])]#on regarde si cette nouvelle position est possible, si oui, on l'affecte
ifdeplacement[3]andnotterrain[((pac_man_pos[1])//8)%29][((pac_man_pos_temp[0]+7)//8)%29]:pac_man_pos[0]=pac_man_pos_temp[0]%(29*8)elifdeplacement[2]andnotterrain[((pac_man_pos[1])//8)%29][((pac_man_pos_temp[0]+1)//8)%29]:pac_man_pos[0]=pac_man_pos_temp[0]%(29*8)elifdeplacement[1]andnotterrain[((pac_man_pos_temp[1]+7)//8)%29][((pac_man_pos[0])//8)%29]:pac_man_pos[1]=pac_man_pos_temp[1]%(29*8)elifdeplacement[0]andnotterrain[((pac_man_pos_temp[1]+1)//8)%29][((pac_man_pos[0])//8)%29]:pac_man_pos[1]=pac_man_pos_temp[1]%(29*8)#on regarde si cette nouvelle position correspond à celle d'une boule
ifboules_restantes[pac_man_pos[1]//8][pac_man_pos[0]//8]:boules_restantes[pac_man_pos[1]//8][pac_man_pos[0]//8]=Falsefill_rect((pac_man_pos[0]>>3)<<3,(pac_man_pos[1]>>3)<<3,8,8,(0,0,0))nb_boules_restantes-=1#on redessine les boules éventuellement effacées par le passage de pacman
ifboules_restantes[pac_man_pos[1]//8-1][pac_man_pos[0]//8]:boule(pac_man_pos[1]//8-1,pac_man_pos[0]//8)ifboules_restantes[(pac_man_pos[1]//8+1)%29][pac_man_pos[0]//8]:boule(pac_man_pos[1]//8+1,pac_man_pos[0]//8)ifboules_restantes[pac_man_pos[1]//8][pac_man_pos[0]//8-1]:boule(pac_man_pos[1]//8,pac_man_pos[0]//8-1)ifboules_restantes[pac_man_pos[1]//8][(pac_man_pos[0]//8+1)%29]:boule(pac_man_pos[1]//8,pac_man_pos[0]//8+1)draw_string(str(nb_boules_restantes)+"",240,30,(255,255,0),(0,0,0))ifdeplacement[3]:fill_rect(pac_man_pos[0]+1,pac_man_pos[1],8,8,(255,255,0))fill_rect(pac_man_pos[0]+4,pac_man_pos[1]-2,3,2,(255,255,0))fill_rect(pac_man_pos[0]+6,pac_man_pos[1]+2,2,2,(0,0,0))fill_rect(pac_man_pos[0]+5,pac_man_pos[1]+4,3,3,(255,0,0))elifdeplacement[2]:fill_rect(pac_man_pos[0]-1,pac_man_pos[1],8,8,(255,255,0))fill_rect(pac_man_pos[0]+2,pac_man_pos[1]-2,3,2,(255,255,0))fill_rect(pac_man_pos[0],pac_man_pos[1]+2,2,2,(0,0,0))fill_rect(pac_man_pos[0],pac_man_pos[1]+4,3,3,(255,0,0))elifdeplacement[0]:fill_rect(pac_man_pos[0]+1,pac_man_pos[1],8,8,(255,255,0))fill_rect(pac_man_pos[0]-1,pac_man_pos[1]+3,2,3,(255,255,0))fill_rect(pac_man_pos[0]+3,pac_man_pos[1]+2,2,2,(0,0,0))fill_rect(pac_man_pos[0]+5,pac_man_pos[1]+1,3,3,(255,0,0))elifdeplacement[1]:fill_rect(pac_man_pos[0],pac_man_pos[1],8,8,(255,255,0))fill_rect(pac_man_pos[0]+8,pac_man_pos[1]+3,2,3,(255,255,0))fill_rect(pac_man_pos[0]+4,pac_man_pos[1]+4,2,2,(0,0,0))fill_rect(pac_man_pos[0]+1,pac_man_pos[1]+4,3,3,(255,0,0))defmain():globaldepart,nb_boules_restantes,precedentcreation_terrain()dessine_terrain()crea_boules()dessine_boules()fill_rect(pac_man_pos[0],pac_man_pos[1],8,8,(255,255,0))fill_rect(pac_man_pos[0]+3,pac_man_pos[1]-2,3,2,(255,255,0))fill_rect(pac_man_pos[0]+5,pac_man_pos[1]+2,2,2,(0,0,0))fill_rect(pac_man_pos[0]+4,pac_man_pos[1]+4,3,3,(255,0,0))depart=monotonic()whilenb_boules_restantes>0:pacman()sleep(1/20)fill_rect(0,0,320,230,(0,0,0))draw_string("Votre temps :",100,70,(255,255,0),(0,0,0))draw_string(str(precedent),120,70,(255,255,0),(0,0,0))main()
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.