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

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.