snek_lite.py

Created by squarepoint

Created on September 25, 2022

1.55 KB

A nice little snek - Made during class™


#Snek LITE - by Squarepoint
from kandinsky import *
from ion import *
from time import sleep
from random import randint
def dsnek(x,y):
  fill_rect(x,y,10,10,color(200,255,50))
  fill_rect(x+3,y+3,4,4,color(0,255,0))
def dhead(x,y):
  fill_rect(x,y,10,10,color(150,255,100))
  fill_rect(x+3,y+3,4,4,color(255,255,255))
  fill_rect(x+5,y+5,2,2,color(0,0,0))
def dfood(x,y):
  fill_rect(x,y,10,10,color(255,150,0))
def game():
  x,y=10,10
  snek,food=[[x,y]],[]
  odir,dir=3,3
  fill_rect(0,0,320,222,color(100,100,100))
  fill_rect(10,10,300,180,color(220,220,220))
  while True:
    draw_string("Snek LITE    Score: "+str(len(snek)),50,200,color(255,255,255),color(100,100,100))
    odir=dir
    if len(food)==0:food.append([randint(1,30)*10,randint(1,18)*10])
    if keydown(KEY_LEFT):dir=1
    elif keydown(KEY_RIGHT):dir=3
    elif keydown(KEY_DOWN):dir=4
    elif keydown(KEY_UP):dir=2
    if odir%2==dir%2: dir=odir
    fill_rect(snek[0][0],snek[0][1],10,10,color(220,220,220))
    snek.reverse()
    snek.pop()
    snek.reverse()
    snek.append([x,y])
    if dir%2==1:x+=(dir-2)*10
    else:y+=(dir-3)*10
    if get_pixel(x+5,y+5)!=color(220,220,220):
      if get_pixel(x+5,y+5)==color(255,150,0):
        snek.append([x,y])
        food.pop()
      else: break
    for i in range(len(snek)):dsnek(snek[i][0],snek[i][1])
    dhead(x,y)
    for i in range(len(food)):dfood(food[i][0],food[i][1])
    sleep(0.1)
while True:
  game()
  draw_string("Press OK to play again",50,100,color(255,255,255),color(100,100,100))
  while not keydown(KEY_OK):
    sleep(0.1)

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.