skeleton_snake.py

Created by fixem

Created on February 27, 2025

1.87 KB


from modules import *
ax,ay=[],[]
noir=(0,)*3
blanc=(255,)*3
for i in range(28):
  ax.append(10*(i+2))
for i in range(18):
  ay.append(2+10*(i+2))
fill_rect(0,0,320,222,noir)
apple=True
coo,p=[],2
x,y=10,12
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)
    if i==0:
      fill_rect(bx,by,10,10,blanc)
      fill_rect(bx+1,by+1,8,8,(0,)*3)
    else:
      fill_rect(bx,by,10,10,(randint(25,50),)*3)  
      fill_rect(bx+2,by+2,6,6,(randint(155,255),)*3)
    if len(coo)>1:
      bx,by=coo[-1].split()
      bx,by=int(bx),int(by)
      fill_rect(bx,by,10,10,(randint(25,50),)*3)
      fill_rect(bx+2,by+2,6,6,noir)
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,blanc)
      fill_rect(a+1,b+1,8,8,(randint(25,50),)*3)
      fill_rect(a+2,b+2,8,8,blanc)
      break
def go():
  draw_string("GAME OVER",115,50,blanc,noir)
  draw_string("Score="+str(p-2),125,75,blanc,noir)
  draw_string("Temps="+str(round(tta,3)),125,100,blanc,noir)
Dapple()
tt=monotonic()
while 1:
  tta=monotonic()-tt
  if x==0:
    x=300
  if x==310:
    x=10
  if y==2:
    y=202
  if y==212:
    y=12
  draw_string(str(p-2),0,0,blanc,noir)
  sleep(0.04-0.0004*p)
  if get_pixel(a,b)!=blanc:
    apple=False
  if apple:
    coo.pop()
  if not apple:
    apple=True
    p+=1
    Dapple()
  if str(x)+" "+str(y)in coo:
    if coo.index(str(x)+" "+str(y))!=len(coo)-1:
      go()
      break
  coo.insert(0,str(x)+" "+str(y))
  snake()
  if keydown(1)and di!="d":di="u"
  elif keydown(2)and di!="u":di="d"
  elif keydown(0)and di!="r":di="l"
  elif keydown(3)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.