snakk.py

Created by elnix91

Created on May 18, 2024

4.47 KB

Snake


from kandinsky import fill_rect as f,draw_string as d
from ion import keydown as k
from time import sleep
from random import randint as r,choice as c
# Constants
C=[(255,)*3,(0,)*3,(0,255,255),(200,50,50),(150,150,150)]
size=10
# Test if can write files
try:from os import *
except ImportError:OS=0
else:OS=1
# Save best score in file
def save():
  if OS==1:
    with open("snkscr.sav","w") as sc:
      sc.truncate(0)
      sc.write(str(bscore))
# Read best score
def load():
  if OS==1:
    with open("snkscr.sav","r") as sc:
      c=sc.read()
      if len(c)>0:return int(c)
  return 0
# Get a empty position
def pos():
    while 1:
      a=[r(0,31),r(0,21)]
      if   a in P:continue
      elif a in A:continue
      elif a in W:continue
      else:break
    return a
# Show the main menu
def menu():
  for i in range(321):f(0,0,i,10,C[2]);sleep(0.0001)
  for i in range(222):f(310,0,10,i,C[2]);sleep(0.0001)
  for i in range(321):f(320,212,-i,10,C[2]);sleep(0.0001)
  for i in range(222):f(0,222,10,-i,C[2]);sleep(0.0001)
  for i in range(301):f(10,10,i,202,C[1]);sleep(0.0001)
  for i in range(256):d("SNAKK!",40,40,(i,i,i),C[1]);sleep(0.0001)
  if bscore>1:
    for i in range(256):d("Score : "+str(points-1),40,90,(i,i,0),C[1]);sleep(0.0001)
    for i in range(256):d("Best score : "+str(bscore-1),40,120,(i,i,0),C[1]);sleep(0.0001)
  for i in range(256):d("Press [OK] or [EXE] to play",25,180,(i,i,i),C[1]);sleep(0.0001)
  while k(4)|k(52):1
  while not k(4)|k(52):1
  for i in range(221):f(0,1,320,i,C[1]);sleep(0.0001)

points=1
bscore=load()
# Initialize
while 1:
  X,Y=5,5
  P=[]
  A=[]
  W=[]
  D=3
  a=[]
  w=[]
  wall=0
  bot=0
  menu()
  points=1
# Game loop
  while 1:
# Direction system
    if bot==0:
      if   k(0)&(D!=3):D=0
      elif k(1)&(D!=2):D=1
      elif k(2)&(D!=1):D=2
      elif k(3)&(D!=0):D=3
    else:
      safePos=[]
      if (X+1<32)&([X+1,Y]not in P)&([X+1,Y]not in W)&(D!=0):safePos.append(3)
      if (X-1>-1)&([X-1,Y]not in P)&([X-1,Y]not in W)&(D!=3):safePos.append(0)
      if (Y+1<22)&([X,Y+1]not in P)&([X,Y+1]not in W)&(D!=1):safePos.append(2)
      if (Y-1>-1)&([X,Y-1]not in P)&([X,Y-1]not in W)&(D!=2):safePos.append(1)
      if len(A)>0:
        if A[0][0]<X:
          if A[0][1]<Y:
            if 1 in safePos:D=1
            elif 0 in safePos:D=0
            else:D=2
          elif A[0][1]>Y:
            if 2 in safePos:D=2
            elif 0 in safePos:D=0
            else:D=1
          else:
            if 0 in safePos:D=0
            elif 1 in safePos:D=1
            else:D=2
        elif A[0][0]>X:
          if A[0][1]<Y:
            if 1 in safePos:D=1
            elif 3 in safePos:D=3
            else:D=2
          elif A[0][1]>Y:
            if 2 in safePos:D=2
            elif 3 in safePos:D=3
            else:D=1
          else:
            if 3 in safePos:D=3
            elif 2 in safePos:D=2
            else:D=1
        else:
          if A[0][1]<Y:
            if 1 in safePos:D=1
            elif 3 in safePos:D=3
            else:D=2
          elif A[0][1]>Y:
            if 2 in safePos:D=2
            elif 3 in safePos:D=3
            else:D=1
      else:
        if 0 in safePos:D=0
        elif 1 in safePos:D=1
        elif 2 in safePos:D=2
        else:D=3
# Move system
    if   D==0:X-=1
    elif D==1:Y-=1
    elif D==2:Y+=1
    elif D==3:X+=1
# Apples system
    if len(A)!=1:A.append(pos())
    if [X,Y]in A:
      points+=1
      wall=1
      w.append(pos())
      A.pop(0)
# Walls system
    if wall>0:
      f(w[0][0]*size+5-wall,w[0][1]*size+6-wall,2*wall,2*wall,C[4])
      wall+=1
    if wall==6:
      wall=0
      f(w[0][0]*size,w[0][1]*size+1,size,size,C[4])
      W.append(w[0])
      w.pop(0)
# Bot system
    if not k(50):swich=0
    if k(50)&(swich==0)&(bot==0):
      swich=1
      C[0]=(255,255,0)
      bot=1
    if k(50)&(swich==0)&(bot==1):
      swich=1
      C[0]=(255,)*3
      bot=0
# Test if edge touched
    if (X<0)|(Y<0)|(X>31)|(Y>21):
      sleep(1)
      break
# Test if snake touched
    if (len(P)>1):
      if ([X,Y]in P[:-1]):
        sleep(1)
        break
# Test if wall touched
    if (len(W)>1):
      if ([X,Y]in W):
        sleep(1)
        break
# Draw head
    P.append([X,Y])
    f(X*size,Y*size+1,size,size,C[0])
# Erase tail
    if len(P)>points:
      f(P[0][0]*size,P[0][1]*size+1,size,size,C[1])
      P.pop(0)
# Draw apple
    if len(A)>0:f(A[0][0]*size,A[0][1]*size+1,size,size,C[3])
# Score system
    if bscore<=points:bscore=points
    save()
    sleep(0.1)
def reset():
  with open("snkscr.sav","w")as s:s.truncate()

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.