snake.py

Created by haxxyis

Created on December 06, 2023

1.78 KB

dqZDAZ


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

darkmode=0
bgC=(248,252,248)
borderC=(0,0,0)
snakeC=(0,252,0)
appleC=(248,0,0)
if darkmode==1:
  bgC=(0,0,0)
  borderC=(0,0,248)
  
fill_rect(0,0,320,222,bgC)
fill_rect(318,0,2,222,borderC)
fill_rect(0,0,320,3,borderC)
fill_rect(0,0,3,222,borderC)
snake=[26,26,26,26,26]
x=160
y=120
endx=135
endy=120
u,v=26,26
length=5
applex=randint(0,64)*5
appley=randint(0,44)*5
while get_pixel(applex,appley)!=bgC:
  applex=randint(0,64)*5
  appley=randint(0,44)*5
fill_rect(applex-2,appley-2,5,5,appleC)
score=0
touched=0
while touched!=borderC and touched!=snakeC:
  if (keydown(KEY_RIGHT) and not u==24) or (keydown(KEY_DOWN) and not u==25) or (keydown(KEY_LEFT) and not u==26) or (keydown(KEY_UP) and not u==34):
    if keydown(KEY_RIGHT):
      u=26
    if keydown(KEY_DOWN):
      u=34
    if keydown(KEY_LEFT):
      u=24
    if keydown(KEY_UP):
      u=25
  snake.append(u)
  if x==applex and y==appley:
    length=length+5
    applex=randint(0,64)*5
    appley=randint(0,44)*5
    while get_pixel(applex,appley)!=bgC:
      applex=randint(0,64)*5
      appley=randint(0,44)*5
    fill_rect(applex-2,appley-2,5,5,appleC)
    score=score+1
  x=x+((u==26)-(u==24))*5
  y=y+((u==34)-(u==25))*5
  if x<5:
    x=315
  if x>315:
    x=5
  if y<5:
    y=220
  if y>220:
    y=5
  if length:
    length=length-1
  else:
    snake.remove(snake[0])
    endx=endx+((v==26)-(v==24))*5
    endy=endy+((v==34)-(v==25))*5
    if endx<5:
      endx=315
    if endx>315:
      endx=5
    if endy<5:
      endy=220
    if endy>220:
      endy=5
    v=snake[0]
    fill_rect(endx-2,endy-2,5,5,bgC)
  touched=get_pixel(x,y)
  if x<0 or x>320 or y<0 or y>220:
    touched=borderC
  fill_rect(x-2,y-2,5,5,snakeC)
  sleep(0.05)
print("Score:",score)

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.