jeuvie2d.py

Created by schraf

Created on July 23, 2023

1.77 KB

Explications en vidéo du jeu de la Vie et du programme.

  • Touche 1 pour lancer un “planeur”
  • Touche 2 pour un vaisseau “LWSS”
  • Touche 3 pour l’oscillateur “pentadécathlon”
  • Touche 0 pour effacer l’écran
  • Touche “.” pour remplir aléatoirement l’écran

J’ai mis (FO)ncé et (CL)air dans cet ordre pour que l’on voit les changements à l’écran, si vous voulez un affichage plus discret, il suffit d’inverser les valeurs de ces 2 variables à la ligne n°8 de sorte que CL = (8,)*3 et FO = (248,)*3

Vous pouvez aussi enlever la ligne alea() si vous ne voulez pas que le jeu débute par une grille remplie aléatoirement.

Clignotant : ["0100","1001","1001","0010"]


from kandinsky import fill_rect, get_pixel
from ion import *
from random import random, randint

d = 12 # taille de la grille

BL,CL,GR,NR,FO = (255,)*3, (255,251,255), (206,203,206), (0,)*3, (8,)*3
COLS, LIGN = 320 // d, 220 // d

def grille():
  for i in range(COLS):
    fill_rect(d * i, 0, 1,d * LIGN, GR)
    if d * i <= 220: fill_rect(0, d * i, 320, 1, GR)

def alea(p = .1):
  for i in range(COLS):
    for j in range(LIGN):
      rect(d * i, d * j, NR if random() < p else BL)

def lancer(fig, x = LIGN - 1):
  y = randint(0,COLS - len(fig[0]))
  for i,ligne in enumerate(fig):
    for j,v in enumerate(ligne):
      coul = NR if v == "1" else BL
      rect(d * (j + y), d * (x - i), coul )

def noir(x,y):
  return get_pixel(d * (x % COLS) + 1, d * (y % LIGN) + 1) == NR

def voisins(c,l):
  nb = 0
  for i in [-1,0,1]:
    for j in [-1,0,1]:
      if (i != 0 or j != 0) and noir(c + i, l + j): nb += 1
  return nb

def suivant():
  for c in range(COLS):
    for l in range(LIGN):
      a = noir(c,l)
      n = voisins(c,l)
      if n == 3 and not(a) : fill_rect(d * c + 2, d * l + 2, 1, 1, FO)
      elif a and (n<2 or n>3): fill_rect(d * c + 2, d * l + 2, 1, 1, CL)
  for c in range(COLS):
    for l in range(LIGN):
      u = get_pixel(d * c + 2, d * l + 2)
      if u == FO: rect(d * c, d * l, NR)
      elif u == CL: rect(d * c, d * l, BL)

def rect(x,y,c): fill_rect(x + 1, y + 1, d - 1, d - 1, c)

def jeu():
  grille()
  alea()
  while True:
    if keydown(KEY_BACK): break
    if keydown(KEY_ONE): lancer(["010","001","111"])
    if keydown(KEY_TWO): lancer(["00000","01111","10001","00001","10010"]) 
    if keydown(KEY_THREE): lancer(["11111111","10111101","11111111"], LIGN // 2)  
    if keydown(KEY_DOT) : alea() 
    if keydown(KEY_ZERO) : alea(0)     

    suivant()
   
jeu()