# Type your text here
fromtimeimportsleepfromionimportkeydownfromkandinskyimportfill_rect,draw_stringfromrandomimportrandintdefcouleur(x,y,p=0):if (x//3+y//3)%2==1:return (242-42*p,242-42*p,142-42*p)else:return (142-42*p,242-42*p,242-42*p)defdraw_grid(x,y,nb,gap,w,h):foriinrange(nb):forjinrange(nb):fill_rect(x+i*(w+gap),y+j*(h+gap),w,h,couleur(i,j))defquadrillage(xi,yi,xn,yn,larg,haut,e1,col):forxinrange(xn):foryinrange(yn):fill_rect(xi+x*(larg+e1),yi+y*(haut+e1),larg,haut,col)color=[(r,g,b)forrin[42,142,242]forgin[42,142,242]forbin[42,142,242]]defgrille(xi,yi,xn,yn,larg,haut,e1):forxinrange(xn):foryinrange(yn):fill_rect(xi+x*(larg+e1),yi+y*(haut+e1),larg,haut,couleur(x,y))defdessine_nb(x,y,nb):draw_string(str(nb),x*25+55,y*25+2,"black",couleur(x,y,2))grille(50,1,9,9,20,20,5)pos=[4,1]# départ
pmax=(9,9)# modulo
defwait(buttons=(0,1,2,3,4)):# Attends qu'une des touches soit pressée
whileTrue:foriinbuttons:ifkeydown(i):whilekeydown(i):True# Fonction anti-rebond
returnidefdeplacement():# On efface sélection précédente
x,y=pos_oldfill_rect(50+x*(20+5),1+y*(20+5),20,20,(255,255,255))fill_rect(50+x*(20+5),1+y*(20+5),20,20,couleur(x,y))x,y=posfill_rect(50+x*(20+5),1+y*(20+5),20,20,couleur(x,y,2))dessine_nb(x,y,9)defaffichage():draw_string(str(pos_old),0,0)draw_string(str(pos),0,20)#draw_string("couleur = "+str(color[pos[0]+pos[1]*pmax[1]])+" ", 40, 170)
whileTrue:key_pressed=Nonekey_pressed=wait()pos_old=list(pos)# On crée une copie indépendante
ifkey_pressed==1:# fleche haut
pos[1]=(pos[1]-1)%pmax[1]elifkey_pressed==2:# fleche bas
pos[1]=(pos[1]+1)%pmax[1]elifkey_pressed==3:# fleche droite
pos[0]=(pos[0]+1)%pmax[0]elifkey_pressed==0:# fleche gauche
pos[0]=(pos[0]-1)%pmax[0]elifkey_pressed==4:print (4)elifkey_pressed==5:print (5)elifkey_pressed==6:print (6)elifkey_pressed==7:print (7)elifkey_pressed==8:print (8)elifkey_pressed==9:print (9)elifkey_pressed==4:# Touche OK
pass# Pas d'action ici
ifkey_pressed!=None:deplacement()#affichage()
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.