snake.py

Created by alexandre-merle

Created on November 20, 2023

1.64 KB


from math import *
from random import *
from kandinsky import *
from ion import *
from time import *
def back_ground(all=True):
  if all:fill_rect(0,0,320,222,(50,230,50))
  fill_rect(10,10,300,200,(230,255,230))
class snake:
  def __init__(self,x,y,c=(0,200,0)):
    self.x=x
    self.y=y
    self.color=c
  def draw(self):fill_rect(self.x,self.y,10,10,self.color)
class aple:
  def __init__(self,c=(255,0,0)):
    self.x=randint(1,30)*10
    self.y=randint(1,20)*10
    self.color=c
  def draw(self):fill_rect(self.x,self.y,10,10,self.color)
while True:
  Move=(0,1)
  Snake=snake(160,100)
  Aple=aple()
  Segment=[[Snake.x,Snake.y],]*1
  back_ground()
  Run,fps,Score=True,10,0
  draw_string("3",155,90);sleep(1)
  draw_string("2",155,90);sleep(1)
  draw_string("1",155,90);sleep(1)
  back_ground()
  while Run:
    back_ground(False)
    if   keydown(KEY_UP)and Move[1]!=1:    Move=(0,-1)
    elif keydown(KEY_DOWN)and Move[1]!=-1: Move=(0,1)
    elif keydown(KEY_LEFT)and Move[0]!=1:  Move=(-1,0)
    elif keydown(KEY_RIGHT)and Move[0]!=-1:Move=(1,0)
    if Snake.x==Aple.x and Snake.y==Aple.y:
      Segment+=[[Snake.x,Snake.y],]
      Aple.__init__()
      Score+=1
      fps+=0.5
    for i in range(0,len(Segment)-1):Segment[i]=[Segment[i+1][0],Segment[i+1][1]]
    Segment[len(Segment)-1]=[Snake.x,Snake.y]
    Snake.x+=Move[0]*10;Snake.y+=Move[1]*10
    draw_string("SCORE:"+str(Score),10,10,(0,50,0),(230,255,230))
    Aple.draw()
    Snake.draw()
    for i in Segment:
      fill_rect(i[0],i[1],10,10,Snake.color)
      if Snake.x==i[0] and Snake.y==i[1]:Run=False
    if 10<=Snake.x<=300 and 10<=Snake.y<=200:pass
    else:Run=False
    sleep(1/fps)

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.