snake.py

Created by fixem

Created on January 25, 2025

1.46 KB

Jeu de snake très basique avec graphique de base.


from kandinsky import *
from ion import *
from random import *
from color import *
"""
Jeu codé par FIXEM
"""
ax,ay=[],[]
for i in range(30):
  ax.append(10*(i+1))
for i in range(20):
  ay.append(1+10*(i+1))
fill_rect(0,0,320,222,bleuClair)
fill_rect(10,11,300,200,vertClair)
apple=True
coo,p=[],2
x,y=10,11
di="r"
for i in range(p):
  coo.append(str(20+10*i)+" "+str(31))
def snake():
  global coo
  for i in range(len(coo)):
    bx,by=coo[i].split()
    bx,by=int(bx),int(by)
    fill_rect(bx,by,10,10,orange)  
    if len(coo)>1:
      bx,by=coo[-1].split()
      bx,by=int(bx),int(by)
      fill_rect(bx,by,10,10,vertClair)
def Dapple():
  global ax,ay,coo,a,b
  while 1:
    a=choice(ax)
    b=choice(ay)
    if str(a)+" "+str(b) in coo:
      continue
    else:
      fill_rect(a,b,10,10,rouge)
      break
def go():
  draw_string("GAME OVER",115,50,noir,vertClair)
  draw_string("Score="+str(p-2),125,75,noir,vertClair)
Dapple()
while 1:
  if y>201 or y<11 or x>300 or x<10: 
    go()
    break
  if get_pixel(a,b)!=rouge:
    apple=False
  sleep(0.065)
  if apple:
    coo.pop()
  if not apple:
    apple=True
    p+=1
    Dapple()
  if str(x)+" "+str(y)in coo:
    go()
    break
  coo.insert(0,str(x)+" "+str(y))
  snake()
  if keydown(KEY_UP)and di!="d":di="u"
  elif keydown(KEY_DOWN)and di!="u":di="d"
  elif keydown(KEY_LEFT)and di!="r":di="l"
  elif keydown(KEY_RIGHT)and di!="l":di="r"
  if di=="r":x+=10
  elif di=="l":x-=10
  elif di=="u":y-=10
  elif di=="d":y+=10

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

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.