The snake game
from random import randint from ion import keydown,KEY_BACK,KEY_DOWN,KEY_UP,KEY_LEFT,KEY_RIGHT from kandinsky import * from time import sleep xmax,ymax=22,32 def dessine(x,y,c): for i in range(10): for j in range(10): set_pixel(x*10+i,y*10+j,c) def nouvbonbon(serpent,bonbons): while True: bonbon=(randint(0,xmax-1),randint(0,ymax-1)) if bonbon not in serpent and bonbon not in bonbons: return bonbon def jeu(): serpent=[(0,0),(0,1),(0,2)] tete=(0,2) sens="D" bonbons=set() bonbons={nouvbonbon(serpent,bonbons)} for x,y in serpent[:-1]: dessine(y,x,color(0,255,0)) dessine(tete[1],tete[0],color(0,0,255)) for bonbon in bonbons: dessine(bonbon[1],bonbon[0],color(255,0,0)) while True: if keydown(KEY_BACK): break if keydown(KEY_UP): sens="H" if keydown(KEY_RIGHT): sens="D" if keydown(KEY_DOWN): sens="B" if keydown(KEY_LEFT): sens="G" if sens=="H": tete=((tete[0]-1)%xmax,tete[1]) if sens=="D": tete=(tete[0],(tete[1]+1)%ymax) if sens=="B": tete=((tete[0]+1)%xmax,tete[1]) if sens=="G": tete=(tete[0],(tete[1]-1)%ymax) if tete in serpent: break serpent.append(tete) dessine(tete[1],tete[0],color(0,0,255)) if tete in bonbons: bonbons.discard(tete) bonbons.add(nouvbonbon(serpent,bonbons)) if not len(serpent)%10: bonbons.add(nouvbonbon(serpent,bonbons)) else: dessine(serpent[-2][1],serpent[-2][0],color(0,255,0)) dessine(serpent[0][1],serpent[0][0],color(255,255,255)) del serpent[0] for bonbon in bonbons: dessine(bonbon[1],bonbon[0],color(255,0,0)) dessine(serpent[-2][1],serpent[-2][0],color(0,255,0)) sleep(0.3/len(serpent)**.5) draw_string("FIN du jeu",100,100, color(200,0,0),color(200,200,200)) draw_string("Score=%d"%len(serpent),120,120, color(200,0,0),color(200,200,200))