stars.py

Created by schraf

Created on September 28, 2020

673 Bytes

Explications en vidéo ici

Simulation de l’économiseur d’écran “Starfield” bien connu sous les anciennes versions de Windows.

Autres versions

Effet 1

Modifiez uniquement la ligne qui génère les positions initiales des points (on les met tous très loin entre 50 et 55) :

def point():
  return [randrange(-40,40),randrange(-40,40),randrange(50,55)]


Testez avec >>go()

Effet 2

Modifiez alors les lignes :

px = min(320, max(-1,st[i][0] * (1 - z / 10)**2 + 160))
py = min(240, max(-1, 5 * st[i][1] / z + 120))

Effet 3

On peut aussi changer les lignes px = et py = du programme ci-dessous pour obtenir de nombreux autres effets, par exemples :

 px = min(320, max(-1,st[i][0] * sqrt(z/5) + 160))
 py = min(240, max(-1,st[i][1] / z + 120))


Effet 4

 px = min(320, max(-1,st[i][0] * (10 - z) / 10 + 160))
 py = min(240, max(-1,st[i][1] * cos(z / 2) + 120))


Sans oublier de mettre from math import sqrt, cos au début du programme.

Version "Neige"

from random import *
from kandinsky import *
from time import sleep

nb = 120
st = [0]*nb
pr = [0]*nb
NR = (0,0,0)

def point():
  return [randrange(320),0,randrange(1,10)]

def init():
  fill_rect(0,0,320,222,NR)
  for i in range(nb): st[i] = point()

def go():
  init()
  while True:
    sleep(.05)
    for i in range(nb):
      st[i][1] += randrange(1,int(50/st[i][2]))
      z = st[i][2]
      px = st[i][0]
      py = min(240,st[i][1])
      if py >= 240: st[i] = point()
      if pr[i] != 0: set_pixel(pr[i][0],pr[i][1],NR)
      pr[i] = [int(px),int(py)]
      c = int((1 - z/10) * 255)
      set_pixel(pr[i][0],pr[i][1],color(c,c,c))


from random import *
from kandinsky import *

nb = 120
st = [0]*nb
pr = [0]*nb

def point():
  return [randrange(-160,160),randrange(-120,120),randrange(1,10)]

def init():
  fill_rect(0,0,320,222, (0,0,0))
  for i in range(nb): st[i] = point()

def go():
  init()
  while True:
    for i in range(nb):
      st[i][2] -= 0.2
      if st[i][2] <= 0: st[i] = point()
      z  = st[i][2]
      px = min(320, max(-1,st[i][0] / z + 160))
      py = min(240, max(-1,st[i][1] / z + 120))
      if pr[i] != 0: set_pixel(pr[i][0],pr[i][1],(0,0,0))
      pr[i] = [int(px),int(py)]
      c = int((1 - z / 10) * 255)
      set_pixel(pr[i][0], pr[i][1], color(255-c,c,3*c))