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)

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.