snake.py

Created by hugeliott

Created on October 16, 2024

3.09 KB

Une version minimaliste(sans menue) du jeux snake crée pour les calculatrices sur une calculatrice


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

snake=[[0,0],[0,0],[0,0]]
x=5
y=5
cordpom=[5,5]
score=-1
game=True
left=-1
up=0
draw=True
memotime=0
memrot=0
notsupr=False
pomplus=True

#w=False ou True False=horizontale True=verticale
def segmentdroit(x,y,w):
  if w:
    fill_rect(x*10,y*10+2,10,8,(100,230,0))
  else:
    fill_rect(x*10+1,y*10+1,8,10,(100,230,0))
  xp=randint(1,6)
  yp=randint(1,6)
  fill_rect(x*10+xp,y*10+yp+1,3,3,(200,0,150))

#xw=True si la courbe passe par le cote gauche
#yn=True si la courbe passe par le cote du haut
#si une valleur est False la courbe passe par le cote oposee
def segmentourne(x,y,xw,yn):
  if yn:
    fill_rect(x*10+1,y*10+1,8,8,(100,230,0))
  else:
    fill_rect(x*10+1,y*10+3,8,8,(100,230,0))
  if xw:
    fill_rect(x*10,y*10+2,8,8,(100,230,0))
  else:
    fill_rect(x*10+2,y*10+2,8,8,(100,230,0))

def head(x,y,xw,yn):
  print()

def pomme(x,y):
  fill_rect(int(x)*10+3,int(y)*10+2,6,8,(255,0,0))
  fill_rect(int(x)*10+2,int(y)*10+3,8,6,(255,0,0))

def dell(a):
  fill_rect(int(a[0]*10),a[1]*10+1,10,10,(255,255,255))

def choosecopom():
  a=True
  while a:
    xp=randint(0,31)
    yp=randint(0,21)
    if get_pixel(xp*10+4,yp*10+4)!=(255,255,255):
      a=True
    else:
      return [xp,yp]

while game:
  if keydown(KEY_LEFT) and left==0 and draw:
    up=0
    left=1
    draw=False
  elif keydown(KEY_RIGHT) and left==0 and draw:
    up=0
    left=-1
    draw=False
  elif keydown(KEY_UP) and up==0 and draw:
    up=1
    left=0
    draw=False
  elif keydown(KEY_DOWN) and up==0 and draw:
    up=-1
    left=0
    draw=False

  if [x,y]==cordpom:
    dell(cordpom)
    cordpom=[]
    notsupr=True
    score=score+1
    pomplus=True

  if notsupr and pomplus:
    cordpom=choosecopom()
    pomme(cordpom[0],cordpom[1])
    pomplus=False

  if monotonic()-0.15>=memotime:
    snake.insert(0,[x,y])
    if notsupr==False:
      dell(snake[score+3])
      snake.pop()
    if draw:
      if up==0:
        segmentdroit(x,y,True)
        draw=True
      else:
        segmentdroit(x,y,False)
        draw=True
    else:
      if up==1:
        if snake[1][0]-snake[0][0]==-1:
          segmentourne(x,y,True,True)
        else:
          segmentourne(x,y,False,True)
        draw=True
      if up==-1:
        if snake[1][0]-snake[0][0]==-1:
          segmentourne(x,y,True,False)
        else:
          segmentourne(x,y,False,False)
        draw=True
      if left==1:
        if snake[1][1]-snake[0][1]==-1:
          segmentourne(x,y,True,True)
        else:
          segmentourne(x,y,True,False)
        draw=True
      if left==-1:
        if snake[1][1]-snake[0][1]==-1:
          segmentourne(x,y,False,True)
        else:
          segmentourne(x,y,False,False)
        draw=True
    memotime=monotonic()
    if up!=0:
      y=y-up
    if left!=0:
      x=x-left
    notsupr=False

  if x<0 or x>31 or y<0 or y>21:
    game=False
  if get_pixel((x)*10+4,(y)*10+4)!=(255,255,255) and get_pixel((x)*10+4,(y)*10+4)!=(255,0,0) and score>0 and x>-1 and x<32 and y>-1 and y<22:
    game=False
print(score)

#creer par Eliott HUG-2023 en 142 lignes

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.