snake.py

Created by yannis300307

Created on November 24, 2024

2.47 KB


from kandinsky import *
from ion import *
from time import *
from random import *

TILE_S=20
SW=16
SH=11

Y_OFFSET = 1

GO=\
".XX...X..XX..XX. "\
"X....X.X.X.X.X.. "\
"X.XX.XXX.X.X.XX. "\
"X..X.X.X.X.X.X.. "\
".XX..X.X.X.X.XX. "\
"................ "\
".XX..X.X.XX.XX.. "\
"X..X.X.X.X..X.X. "\
"X..X.X.X.XX.XX.. "\
"X..X.X.X.X..X.X. "\
".XX...X..XX.X.X. "

WIN=\
"................ "\
"................ "\
"................ "\
".X..X..X.X.X..X. "\
".X..X..X.X.XX.X. "\
".X.X.X.X.X.X.XX. "\
".X.X.X.X.X.X..X. "\
"..X...X..X.X..X. "\
"................ "\
"................ "\
"................"

def tile(x,y,t):
  if t==1:
    c=(255,0,0)
  elif t==2:
    c=(0,255,0)
  elif t==3:
    c=(0,255,255)
  elif t==4:
    c=(200,0,0)
  elif t==5:
    c=(0,200,0)
  else:
    c=(150,150,150)
  fill_rect(x*TILE_S,Y_OFFSET+y*TILE_S,TILE_S,TILE_S,(100,100,100))
  fill_rect(x*TILE_S+TILE_S//20,Y_OFFSET+y*TILE_S+TILE_S//20,TILE_S-TILE_S//20*2,TILE_S-TILE_S//20*2,c)

def full_board():
  fill_rect(0, 0, 340, 222, (100,100,100))
  for x in range(SW):
    for y in range(SH):
      tile(x,y,0)

def spawn_bonus():
  global bx,by
  available = []
  for x in range(SW):
    for y in range(SH):
      if (x, y) not in q: available.append((x,y))
  bx, by = choice(available)
  tile(bx,by,3)

def show_go():
  lines = GO.split()
  for x in range(SW):
    for y in range(SH):
      if lines[y][x] == "X":
        tile(x, y, 4)
      else:
        tile(x, y, 0)
  
def show_win():
  lines = WIN.split()
  for x in range(SW):
    for y in range(SH):
      if lines[y][x] == "X":
        tile(x, y, 5)
      else:
        tile(x, y, 0)

def death_anim():
  tile(*q[-1],t=1)
  for i in q:
    tile(*i, t=0)
    sleep(0.1)
  show_go()

while True:
  hx,hy,d=7,5,0
  dl=5
  q=[(7,5)]*dl
  l=dl

  bx,by=0,0

  full_board()
  spawn_bonus()
  alive=True
  while alive:
    if keydown(KEY_BACKSPACE):
      running=False
    if keydown(KEY_RIGHT) and d!=2:d=0
    if keydown(KEY_LEFT) and d!=0:d=2
    if keydown(KEY_UP) and d!=1:d=3
    if keydown(KEY_DOWN) and d!=3:d=1
    
    tile(hx,hy,2)
    q.append((hx,hy))
    tile(*q.pop(0),t=0)
    
    if hx==bx and hy==by:
      spawn_bonus()
      q.append(q[-1])
    
    if d==0:hx+=1
    if d==1:hy+=1
    if d==2:hx-=1
    if d==3:hy-=1

    tile(hx,hy,1)
    if (hx, hy) in q or hx > SW-1 or hx < 0 or hy > SH-1 or hy < 0:
      death_anim()
      alive = False
    
    if len(q) == SW * SH:
      show_win()
      alive = False

    sleep(0.2)

  while not keydown(KEY_OK):
    sleep(0.05)

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.