fromkandinskyimport*fromtimeimportsleepfromionimport*fromrandomimportrandint# Couleurs aléatoires
j=randint(0,255)k=randint(0,255)l=randint(0,255)config={}# Configuration importée
UP,DOWN,LEFT,RIGHT=0,1,2,3# Positionnement des cases
defposx(x):return10*x-5defposy(y):return10*y-4classSnake:def__init__(self,config,high_score=0,init_len=3):self.imported_config=configself.high_score=high_scoreself.init_len=init_lenself.init_snk()self.show_score()self.start()defset_config(self):self.x,self.y=16,11self.body=[(self.x,self.y)]*self.init_lenself.sleep=0.01self.decrement=0.0003self.inc=0self.direction=UPself.tdirection=UPself.brd_co=(j,k,100)self.bg_co=(j,k,200)self.snk_co=(j,k,100)self.fd_co=(250,k,l)forkey,valueinself.imported_config.items():setattr(self,key,value)definit_snk(self):self.set_config()self.len=self.init_lenself.eat=[]fill_rect(0,0,320,222,self.bg_co)fill_rect(0,0,320,6,self.brd_co)fill_rect(0,0,5,222,self.brd_co)fill_rect(320-5,0,5,222,self.brd_co)fill_rect(0,222-6,320,6,self.brd_co)for_inrange(3):self.spawn_food()self.show_score()defspawn_food(self):whileTrue:x=randint(1,31)y=randint(1,21)if (x,y)notinself.bodyor(x,y)notinself.eat:breakfill_rect(posx(x),posy(y),10,10,self.fd_co)self.eat.append((x,y))defshow_score(self,sx=205,sy=6):draw_string("Score : {:0>2}".format(self.len-self.init_len),sx,sy)defstart(self):defsub_iter(i1,i2):returntuple(v1-v2forv1,v2inzip(i1,i2))whileTrue:ifself.len<len(self.body):self.body.pop(0)x,y=self.body[0]x,y=posx(x),posy(y)ddir=sub_iter(self.body[0],self.body[1])l,h=10,10ifddir[0]<0:# RIGHT
l=self.incifddir[0]>0:# LEFT
l=self.incx+=10-self.incifddir[1]<0:# DOWN
h=self.incifddir[1]>0:# UP
h=10y+=10-self.incfill_rect(x,y,l,h,self.bg_co)x,y=posx(self.x),posy(self.y)ddir=sub_iter(self.body[0],self.body[1])l,h=10,10ifself.direction==RIGHT:l=self.incifself.direction==LEFT:l=self.incx+=10-self.incifself.direction==DOWN:h=self.incifself.direction==UP:h=self.incy+=10-self.incfill_rect(x,y,l,h,self.snk_co)sleep(self.sleep)ifkeydown(KEY_EXE):whilekeydown(KEY_EXE):passwhilenotkeydown(KEY_EXE):passwhilekeydown(KEY_EXE):passifkeydown(KEY_UP)andself.direction!=DOWN:self.tdirection=UPifkeydown(KEY_DOWN)andself.direction!=UP:self.tdirection=DOWNifkeydown(KEY_RIGHT)andself.direction!=LEFT:self.tdirection=RIGHTifkeydown(KEY_LEFT)andself.direction!=RIGHT:self.tdirection=LEFTifself.inc==10:self.inc=0self.direction=self.tdirectionifself.direction==UP:self.y-=1ifself.direction==DOWN:self.y+=1ifself.direction==RIGHT:self.x+=1ifself.direction==LEFT:self.x-=1self.body.append((self.x,self.y))if (self.x,self.y)inself.eat:self.len+=1delself.eat[self.eat.index((self.x,self.y))]self.spawn_food()self.show_score()ifself.sleep>self.decrement:self.sleep-=self.decrementelif (self.x,self.y)inself.body[1:-1]ornot0<self.x<32ornot0<self.y<22:fill_rect(5,6,310,210,self.bg_co)draw_string("Game Over",120,100)draw_string("Press EXE to play again",55,120)draw_string("Press HOME to return home",55,137)self.high_score=max(self.len-self.init_len,self.high_score)draw_string("Highscore : "+str(self.high_score),80,190)self.show_score(120,170)whileTrue:ifkeydown(KEY_EXE):breaksleep(0.01)self.init_snk()self.inc+=1defmain_menu():fill_rect(0,0,320,222,(0,0,0))draw_string("Snake Game",120,50)draw_string("1. Taille petite",100,80)draw_string("2. Taille moyenne",100,100)draw_string("3. Taille grande",100,120)draw_string("Appuyez sur EXE pour commencer",55,150)current_option=0options=["Taille petite","Taille moyenne","Taille grande"]y_positions=[80,100,120]whileTrue:# Mettre en surbrillance l'option sélectionnée
fori,y_posinenumerate(y_positions):color=(255,255,255)ifi==current_optionelse (0,0,0)draw_string(options[i],100,y_pos,color)# Attente des entrées clavier
ifkeydown(KEY_UP):current_option=(current_option-1)%len(options)# Se déplacer vers le haut
whilekeydown(KEY_UP):pass# Attendre la fin de la pression
ifkeydown(KEY_DOWN):current_option=(current_option+1)%len(options)# Se déplacer vers le bas
whilekeydown(KEY_DOWN):pass# Attendre la fin de la pression
ifkeydown(KEY_EXE):# Démarrer le jeu avec la taille choisie
ifcurrent_option==0:Snake(config,high_score=0,init_len=3)# Taille petite
elifcurrent_option==1:Snake(config,high_score=0,init_len=5)# Taille moyenne
elifcurrent_option==2:Snake(config,high_score=0,init_len=7)# Taille grande
breaksleep(0.1)# Lancer le menu principal
main_menu()
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.