jeu de la vie
Actions
Vote :
ScreenshotAperçu
Informations
Auteur Author: louisleplusbeau
Type : Texte
Taille Size: 2.72 Ko KB
Mis en ligne Uploaded: 04/08/2025 - 13:10:48
Uploadeur Uploader: louisleplusbeau (Profil)
Téléchargements Downloads: 1
Visibilité Visibility: Archive publique
Shortlink : https://tipla.net/a4816999
Type : Texte
Taille Size: 2.72 Ko KB
Mis en ligne Uploaded: 04/08/2025 - 13:10:48
Uploadeur Uploader: louisleplusbeau (Profil)
Téléchargements Downloads: 1
Visibilité Visibility: Archive publique
Shortlink : https://tipla.net/a4816999
Description
from time import *
from random import random, randint
from kandinsky import fill_rect, get_pixel
from ion import *
# Configuration
d = 10 # Taille des cellules (10 permet une grille 32x24 sur écran 320x240)
BL, GR, NR, FO, CL = (255,255,255), (200,200,200), (0,0,0), (0,0,255), (255,255,0)
COLS, LIGN = 320 // d, 220 // d # Nombre de colonnes/lignes
def grille():
"""Dessine la grille de fond"""
for i in range(COLS):
fill_rect(d*i, 0, 1, d*LIGN, GR)
for j in range(LIGN):
fill_rect(0, d*j, d*COLS, 1, GR)
def rect(x, y, c):
"""Dessine une cellule à la position (x,y)"""
fill_rect(x*d + 1, y*d + 1, d-2, d-2, c)
def alea(p=0.2):
"""Initialisation aléatoire"""
for i in range(COLS):
for j in range(LIGN):
rect(i, j, NR if random() < p else BL)
def lancer(fig, x=None, y=None):
"""Place une figure au centre par défaut"""
x = LIGN//2 - len(fig)//2 if x is None else x # Centre vertical
y = (COLS - len(fig[0]))//2 if y is None else y # Centre horizontal
for i, ligne in enumerate(fig):
for j, v in enumerate(ligne):
rect(y + j, x + i, NR if v == "1" else BL)
def noir(x, y):
"""Vérifie si une cellule est vivante"""
return get_pixel(x*d + d//2, y*d + d//2) == NR
def voisins(x, y):
"""Compte les voisins vivants (avec bordure toroïdale)"""
nb = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if (i or j) and noir((x + i) % COLS, (y + j) % LIGN):
nb += 1
return nb
def suivant():
"""Calcule la génération suivante"""
changements = []
for x in range(COLS):
for y in range(LIGN):
v = voisins(x, y)
if noir(x, y):
if v < 2 or v > 3:
changements.append((x, y, BL)) # Meurt
elif v == 3:
changements.append((x, y, NR)) # Naît
for (x, y, c) in changements:
rect(x, y, c)
def jeu():
"""Boucle principale"""
grille()
alea(0) # Grille vide au départ
while True:
if keydown(KEY_BACK):
break
if keydown(KEY_ONE):
lancer(["010", "001", "111"]) # Planeur
if keydown(KEY_TWO):
lancer(["00000", "01111", "10001", "00001", "10010"]) # Canon à planeurs
if keydown(KEY_THREE):
lancer(["11111111", "10111101", "11111111"]) # Vaisseau
if keydown(KEY_FOUR):
lancer(["111"]) # Bateau
if keydown(KEY_DOT):
alea() # Aléatoire
if keydown(KEY_ZERO):
alea(0) # Vide
suivant()
sleep(0.1) # Ralentir l'exécution
jeu()
from random import random, randint
from kandinsky import fill_rect, get_pixel
from ion import *
# Configuration
d = 10 # Taille des cellules (10 permet une grille 32x24 sur écran 320x240)
BL, GR, NR, FO, CL = (255,255,255), (200,200,200), (0,0,0), (0,0,255), (255,255,0)
COLS, LIGN = 320 // d, 220 // d # Nombre de colonnes/lignes
def grille():
"""Dessine la grille de fond"""
for i in range(COLS):
fill_rect(d*i, 0, 1, d*LIGN, GR)
for j in range(LIGN):
fill_rect(0, d*j, d*COLS, 1, GR)
def rect(x, y, c):
"""Dessine une cellule à la position (x,y)"""
fill_rect(x*d + 1, y*d + 1, d-2, d-2, c)
def alea(p=0.2):
"""Initialisation aléatoire"""
for i in range(COLS):
for j in range(LIGN):
rect(i, j, NR if random() < p else BL)
def lancer(fig, x=None, y=None):
"""Place une figure au centre par défaut"""
x = LIGN//2 - len(fig)//2 if x is None else x # Centre vertical
y = (COLS - len(fig[0]))//2 if y is None else y # Centre horizontal
for i, ligne in enumerate(fig):
for j, v in enumerate(ligne):
rect(y + j, x + i, NR if v == "1" else BL)
def noir(x, y):
"""Vérifie si une cellule est vivante"""
return get_pixel(x*d + d//2, y*d + d//2) == NR
def voisins(x, y):
"""Compte les voisins vivants (avec bordure toroïdale)"""
nb = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if (i or j) and noir((x + i) % COLS, (y + j) % LIGN):
nb += 1
return nb
def suivant():
"""Calcule la génération suivante"""
changements = []
for x in range(COLS):
for y in range(LIGN):
v = voisins(x, y)
if noir(x, y):
if v < 2 or v > 3:
changements.append((x, y, BL)) # Meurt
elif v == 3:
changements.append((x, y, NR)) # Naît
for (x, y, c) in changements:
rect(x, y, c)
def jeu():
"""Boucle principale"""
grille()
alea(0) # Grille vide au départ
while True:
if keydown(KEY_BACK):
break
if keydown(KEY_ONE):
lancer(["010", "001", "111"]) # Planeur
if keydown(KEY_TWO):
lancer(["00000", "01111", "10001", "00001", "10010"]) # Canon à planeurs
if keydown(KEY_THREE):
lancer(["11111111", "10111101", "11111111"]) # Vaisseau
if keydown(KEY_FOUR):
lancer(["111"]) # Bateau
if keydown(KEY_DOT):
alea() # Aléatoire
if keydown(KEY_ZERO):
alea(0) # Vide
suivant()
sleep(0.1) # Ralentir l'exécution
jeu()