the_snake_module.py

Created by antarctus

Created on November 07, 2021

4.78 KB

Version 1.2 Béta publique


Vous rêvez de faire votre propre jeu snake ?
CECI vous le permet !!!
Une documentation arrive mais vous pouvez l’utilisez avec des script de ce compte !


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

def get_coords(n):
  return (n//32,(n-32*(n//32))%22)

def set_coords(x,y):
  return 32*x+y

class Snake():
  def __init__(self,name="snake",color=(0,255,0)):
    self.name=name
    self.color=color
    self.touch=[1,3,2,0]
    self.length=1
    self.x=randint(3,10)
    self.y=randint(3,16)
    self.queue=[]
    self.dir=1
    self.to_undraw=(-1,-1)
    self.S=None
    self.death=False
    self.back_dir_allowed=False
    self.border_pass=0

  def get_touch(self):
    last_dir=self.dir
    for i in range(4):
      if keydown(self.touch[i]):
        self.dir=i
    if (self.dir-2)%4==last_dir and not self.back_dir_allowed:
      self.dir=last_dir

  def draw_all(self):
    for i in self.queue:
      j=get_coords(i)
      fill_rect(j[0]*10,j[1]*10,10,10,self.color)

  def step(self):
    if self.death:
      return
    self.get_touch()
    self.x+=[0,1,0,-1][self.dir]
    self.y+=[-1,0,1,0][self.dir]

    if self.border_pass==0:
      if not 0<=self.x<32:
        self.death=True
        self.x,self.y=get_coords(self.queue[0])
      elif not 0<=self.y<22:
        self.death=True
        self.x,self.y=get_coords(self.queue[0])
    elif self.border_pass==1:
      self.x%=32
      self.y%=22
    for s in self.S.snakes:
      for q in s.queue[1:]:
        if (self.x,self.y)==get_coords(q):
          self.death=True
          s.queue.remove(set_coords(self.x,self.y))
      if (self.x,self.y)==(s.x,s.y):
        if self!=s:
          self.death=True

    self.queue=[self.x*32+self.y,]+self.queue
    if len(self.queue)>self.length:
      self.to_undraw=get_coords(self.queue[-1])
      del self.queue[-1]
      fill_rect(self.to_undraw[0]*10,self.to_undraw[1]*10,10,10,self.S.BACK)
#    elif len(self.queue)<self.length:
#      

    fill_rect(self.x*10,self.y*10,10,10,self.color)


class Scene():
  def __init__(self,apples=[],snakes=[]):
    self.snakes=snakes
    self.apples=apples
    self.dead=[]
    self.BACK=(255,)*3
    self.t=0.1
    self.key_pause=KEY_OK
    self.callback={
    "death":lambda x: True,
    "step":lambda x: True,
    "pause":lambda x: True,
    }

  def init(self):
    for a_i in range(len(self.apples)):
      self.apples[a_i]=list(self.apples[a_i])
      if len(self.apples[a_i])==2:
        self.apples[a_i]+=[(255,0,0),]
      if len(self.apples[a_i])==3:
        self.apples[a_i]+=[lambda x: True,]
      self.apples[a_i]=tuple(self.apples[a_i])

    fill_rect(0,0,320,222,self.BACK)
    self.draw_score()
    for i in range(len(self.apples)):
      fill_rect(self.apples[i][0]*10,self.apples[i][1]*10,10,10,self.apples[i][2])
    for s in self.snakes:
      s.S=self
      s.draw_all()
      
  def apples_coords(self):
    good=False
    while not good:
      x,y=randint(1,30),randint(1,20)
      good=True
      for s in self.snakes:
        for q in s.queue:
          if (x,y)==get_coords(q):
            good=False
        for apple in self.apples:
          if (x,y)==(apple[0],apple[1]):
            good=False
    return (x,y)

  def draw_score(self):
    for i in range(len(self.snakes)):
      draw_string(self.snakes[i].name+" : "+str(self.snakes[i].length),5,5+i*20,self.snakes[i].color,self.BACK)
      if self.snakes[i] in self.dead:
        fill_rect(5,14+i*20,len(self.snakes[i].name+" : "+str(self.snakes[i].length))*10,1,self.snakes[i].color)

  def negatif(self):
    return (255-self.BACK[0],255-self.BACK[1],255-self.BACK[2])

  def loop(self):
    self.run=True
    self.init()
    while keydown(self.key_pause): pass

    while self.run:
      if keydown(self.key_pause):
        fill_rect(120,81,20,60,self.negatif())
        fill_rect(160,81,20,60,self.negatif())
        self.callback["pause"](self)
        while keydown(self.key_pause): pass
        while not keydown(self.key_pause): pass
        while keydown(self.key_pause):pass
        self.init()
      m=monotonic()
      self.callback["step"](self)
      for i in range(len(self.snakes)):
        s=self.snakes[i]
        if s.death:
          if not s in self.dead:
            self.dead+=[s,]
            draw_string(s.name+" a perdu",135,101,self.negatif(),self.BACK)
            self.draw_score()
            if len(self.dead)==len(self.snakes):
              self.run=False
          continue

        for i in range(len(self.apples)):
          if (s.x,s.y)==(self.apples[i][0],self.apples[i][1]):
            self.apples[i][3](s)
            self.apples[i]=self.apples_coords()+(self.apples[i][2],self.apples[i][3])
            s.length+=1
            self.draw_score()
            fill_rect(self.apples[i][0]*10,self.apples[i][1]*10,10,10,self.apples[i][2])
        s.step()
      sleep(self.t-(monotonic()-m)*((self.t-(monotonic()-m))>0))
    return_score={}
    for i in self.snakes:
      return_score[i.name]=i.length
    return return_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.