the_snake.py

Created by antarctus

Created on February 02, 2021

4.84 KB

Petit jeu du serpent (snake en anglais). Se lance avec la fonction start(). Une version (très) amélioré existe avec “The_snake_game.py”. Mon discord: ANTARCTUS#8195


from random import randint
from kandinsky import color,draw_string,fill_rect
from ion import *
from time import *

s=None

#125 30/09
#84 11/10
#81 18/09

#94 13/10 Thibault

def dur(t):
  if t<0:
    t=0
  return t

class Snake:
  def __init__(self,couleur=(0,255,0)):
    self.x=randint(0,10)
    self.y=10
    self.couleur=couleur
    self.orientation=(1,0)
    self.queue=[(-1,-1)]
    self.mort=False
    self.raison="Je ne sait pas pourquoi!!"
    self.pas=0

  def orienter(self):
    ex_orientation=self.orientation
    for i in range(4):
      if keydown(i):
        self.orientation=((-1,0),(0,-1),(0,1),(1,0))[i]
    if self.orientation==(-ex_orientation[0],-ex_orientation[1]):
      self.orientation=ex_orientation

  def bouger(self):
    self.pas+=1
    if len(self.queue)>1:
      i=len(self.queue)
      while i>0:
        self.queue[i-1]=self.queue[i-2]
        i+=-1
    self.queue[0]=(self.x,self.y)
    self.x+=self.orientation[0]
    self.y+=self.orientation[1]
    if not 0<self.x<32 or not 0<self.y<22:
      self.raison="Tu a touche un mur."
      self.mort=True
    for p in pommes:
      if self.x==p.x and self.y==p.y:
        p.bouffer(self)
    if (self.x,self.y) in self.queue:
      self.raison="Tu t'est recoupe."
      self.mort=True


  def draw(self):
    last=self.queue[-1]
    if type(self.couleur)==type([2,3]):
      fill_rect(self.x*10,self.y*10,10,10,self.couleur[self.pas%len(self.couleur)])
    else:
      fill_rect(self.x*10,self.y*10,10,10,self.couleur)
    fill_rect(last[0]*10,last[1]*10,10,10,couleur_fond)
    for p in pommes:      
      if p.x==last[0] and p.y==last[1]:
        p.draw()

  def draw_all(self):
    if type(self.couleur)==type([2,3]):
      for i in self.queue:
        fill_rect(i[0]*10,i[1]*10,10,10,self.couleur[0])
    else:
      for i in self.queue:
        fill_rect(i[0]*10,i[1]*10,10,10,self.couleur)
    self.draw()

  def pv(self,n):
    if n>0:
      try:
        for i in range(n):
          self.queue.append((-1,-1))
      except:
        pass
    if n<0:
      for i in range(-n):
        if not len(self.queue)<=1:
          fill_rect(self.queue[-1][0]*10,self.queue[-1][1]*10,10,10,couleur_fond)
          del self.queue[-1]
    draw_string("Score:"+str(len(self.queue))+" ",10,10)


class Pomme:
  def __init__(self,couleur=(255,0,0),pv=1,clonage=1,autres=[],tp=1):
    self.couleur=couleur
    self.pv=pv
    self.clonage=clonage
    self.autres=autres
    self.tp=tp
    self.novCoord()
    try:
      pommes.append(self)
    except:
      pass

  def bouffer(self,serpent):
    global pommes
    if self.pv==-1000:
      serpent.mort=True
      serpent.raison="Vous avez mangee \n\tune pomme pourrie."
      return
    serpent.pv(self.pv)
    if self.tp!=1:
      serpent.x+=serpent.orientation[0]*self.tp
      serpent.y+=serpent.orientation[1]*self.tp
      fill_rect(self.x*10,self.y*10,10,10,(255,255,255))
    for i in range(self.clonage):
      Pomme(self.couleur,self.pv,self.clonage,self.autres,self.tp)
      pommes[-1].draw()
    for i in self.autres:
      for j in range(i[0]):
        Pomme(i[1],i[2],i[3])
        pommes[-1].draw()

    pommes.remove(self)
    del self
    return

  def novCoord(self):
    x=2+randint(0,28)
    y=2+randint(0,18)
    for i in pommes:
      if (x,y)==(i.x,i.y):
        self.novCoord()
        return
    self.x=x
    self.y=y

  def draw(self):
    fill_rect(self.x*10,self.y*10,10,10,self.couleur)

def draw_all():
  global s
  fill_rect(0,0,320,222,couleur_fond) 
  s.draw_all()
  for p in pommes:
    p.draw()
  draw_string("Score:"+str(len(s.queue))+" ",10,10)

def commencer(m=((1,(255,0,0),1,1),),c=(0,255,0)):
  global s
  s=Snake(c)
  mode_pomme(m)

  draw_all()

def perdre(serpent):
  score=len(serpent.queue)
  draw_string("Perdu!!",100,70)
  draw_string(serpent.raison,60,100)
  draw_string("Score:"+str(score),80,140)
  print("Score:"+str(score))
  return(score)

couleur_fond=(255,255,255)

pommes=[]

fps=1
vitesse=1/fps

def mode_pomme(quoi):
  global pommes
  pommes=[]
  for type in quoi:
    if len(type)==4:
      for n in range(type[0]):
        Pomme(couleur=type[1],pv=type[2],clonage=type[3])
    if len(type)==5:
      for n in range(type[0]):
        Pomme(couleur=type[1],pv=type[2],clonage=type[3],autres=type[4])
    if len(type)==6:
      for n in range(type[0]):
        Pomme(couleur=type[1],pv=type[2],clonage=type[3],autres=type[4],tp=type[5])
    
def boucler(vitesse=vitesse):
  while True:
    t=monotonic()
    s.orienter()
    s.bouger()
    s.draw()
    if s.mort:
      return(perdre(s))
      break
    if keydown(KEY_ONOFF):
      fill_rect(0,0,320,222,(0,0,0))
      sleep(0.25)
      while not keydown(KEY_ONOFF):
        sleep(0.1)
      draw_all()
      sleep(0.25)  
    sleep(dur(t-monotonic()+vitesse))

def start():
  commencer()
  boucler()
  draw_string("Ressuciter: touche <=",80,160)
  while not keydown(KEY_BACKSPACE):
    sleep(0.05)
  start()

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.