snake.py

Created by randompenguin

Created on September 28, 2023

2.06 KB

Snake Calculator edition :)


'''
Thanks for choosing my Snake game! I made
this during high school, because I was
bored :) Have fun playing this (not so)
beautiful game!

Merci d'avoir choisi mon jeu de Snake !
J'ai fait ce jeu pendant le lycee parce
que je m'ennuyais :) Amuse-toi bien en
jouant ce jeu (pas tres) beau !
'''

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

def snake():
  xcl=[140]
  ycl=[91]
  dirx=20
  diry=0
  crash=0
  fruit=1
  flag=0
  start=1
  score=-1
  fill_rect(0,0,320,222,'gray')

  while crash!=1:
    fill_rect(60,11,200,200,'white')
    for i in range(len(xcl)):
      fill_rect(xcl[i-1],ycl[i-1],20,20,'black')
    if start==1:
      draw_string("START",135,140)
      sleep(1)
      fill_rect(135,140,50,15,'white')
      start=0
    if len(xcl)==100:
      break
    if fruit==1:
      xr=randint(0,4)*40+66
      yr=randint(0,4)*40+17
      score+=1
      fruit=0
    fill_rect(xr,yr,8,8,'yellow')
    draw_string("Score:",0,0)
    draw_string(str(score),0,15)
    flag=0
    sleep(0.5)

    if keydown(0) and dirx!=20:
      if dirx!=-20:
        flag=1
      dirx=-20
      diry=0
    if keydown(3) and dirx!=-20:
      if dirx!=20:
        flag=1
      dirx=20
      diry=0
    if keydown(1) and diry!=20 and flag!=1:
      dirx=0
      diry=-20
    if keydown(2) and diry!=-20 and flag!=1:
      dirx=0
      diry=20

    if keydown(4):
      draw_string("PAUSED",130,106)
      a=0
      while keydown(4):
        a+=1
      while keydown(4)==False:
        a+=1
      draw_string("3",155,136)
      sleep(0.5)
      draw_string("2",155,136)
      sleep(0.5)
      draw_string("1",155,136)
      sleep(0.5)

    else:
      xcl.insert(0,xcl[0]+dirx)
      ycl.insert(0,ycl[0]+diry)
      if xcl[0]+6==xr and ycl[0]+6==yr:
        fruit=1
      if fruit!=1:
        xcl.pop()
        ycl.pop()
      if not 60<=xcl[0]<260 or not 11<=ycl[0]<211 or get_pixel(xcl[0],ycl[0])==(0,0,0):
        crash=1

  if crash==1:
    draw_string("GAME",10,101)
    draw_string("OVER",270,101)
  else:
    draw_string("YOU",15,101)
    draw_string("WIN",275,101)