dm11.py

Created by adam-y

Created on May 29, 2023

1.14 KB

MX DM 11 : “Un feu d’artifice en Python”. Titre : “Un voyage au travers des étincelles”. L’animation est moins saccadée sur une calculatrice physique.


from kandinsky import fill_rect as fr
from random import randint as rand
from random import choice as ch

class Etoile:

  def __init__(self, x, y, z, d, v, c):
    self.x = x
    self.y = y
    self.z = z
    self.direction = d
    self.vitesse = v
    self.couleur = c
    
  def afficher(self, couleur):
    fr(self.x + 160 - self.z//2, self.y + 110 - self.z//2, self.z, self.z, couleur)

fr(0, 0, 320, 230, 'black')

palette = [(255, 173, 173), (255, 214, 165), (253, 255, 182), (202, 255, 191), (155, 246, 255), (160, 196, 255), (189, 178, 255), (255, 198, 255), (255, 255, 252)]
directions = [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]
etoiles = [Etoile(0, 0, 0, (ch(directions), ch(directions)), rand(3, 9), ch(palette)) for _ in range(20)]

while True:
  for obj in etoiles:
    obj.afficher('black')
    if abs(obj.x) >= 160 or abs(obj.y) >= 120:
      obj.x, obj.y, obj.z, obj.direction, obj.vitesse, obj.couleur = 0, 0, 0, (ch(directions), ch(directions)), rand(3, 9), ch(palette)
    else:
      obj.x += obj.direction[0] * obj.vitesse
      obj.y += obj.direction[1] * obj.vitesse
      obj.afficher(obj.couleur)
      if obj.z < 3:
        obj.z += 1