# snake.py

Created by nicolas-patrois

Created on November 07, 2020

1.82 KB

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:
if not len(serpent)%10: