tron.py

Created by schraf

Created on September 01, 2023

1.31 KB

Explications en vidéo

Pour lancer le programme avec 2 joueurs, tapez >> go() et pour 3 joueurs tapez >> go(3)


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

FOND = (41, 10, 41)

class moto:
    dir = [[0,1],[1,0],[0,-1],[-1,0]]
    def __init__(self, nom, x, y, d, coul, ga, dr):
        self.nom = nom
        self.pos = [x, y]
        self.d = d
        self.coul = coul
        self.gauche = ga
        self.droite = dr
        self.tps = monotonic()
        self.fin=False
        
    def tourne(self, t):
      if monotonic() - self.tps > 0.3:
        self.tps = monotonic()
        self.d = (self.d + t) % 4

    def maj(self):
      if keydown(self.gauche): self.tourne(-1)
      if keydown(self.droite): self.tourne(1) 
      [x, y] = self.pos
      [dx, dy] = self.dir[self.d]
      self.pos = [x + dx, y + dy]
      [x, y] = self.pos
      if get_pixel(x, y) != FOND:
        self.fin=True
      else:  
        fill_rect(x,y,1,1,self.coul)

def go(n = 2):
  fill_rect(0,0,320,222,FOND)
  j1 = moto('TRON',0,110,1,(230,180,60),KEY_ONE,KEY_SEVEN)
  j2 = moto('MCP',320,110,3,(100,190,230),KEY_RIGHTPARENTHESIS,KEY_MINUS)
  j3 = moto('YORI',160,220,2,(220,100,30),KEY_ANS,KEY_DOT)
  joueurs = [j1, j2]
  if n > 2: joueurs.append(j3)
  while len(joueurs) > 1:
    for j in joueurs:
     j.maj()
     if j.fin: joueurs.remove(j)
    sleep(.02)
  j = joueurs[0]
  draw_string(j.nom+" GAGNE !",100,200,j.coul, FOND)