jeu_de_la_vie.py

Created by antarctus

Created on March 17, 2021

2.73 KB

Jeu de la vie de John Conway (Une fois la simulation lancée. Appuyez sur OK pour modifier et une fois en mode modification déplacer le curseur avec les flèches et appuyer sur HOME pour créer une case vide et ONOFF pour la remplir. Appuyez sur SHIFT pour faire apparaitre un glisseur et ALPHA pour un Canon.)


#Projet commeence le 1/2/21 par ANTARCTUS

from random import *
from kandinsky import *
from ion import *
from time import *

x,y=0,0
width=64
height=44
width_c=5
height_c=5

world=[False,]*height
for i in range(height):
  world[i]=[False,]*width

world2=[False,]*height
for i in range(height):
  world2[i]=[False,]*width


def draw_all():
  for i in range(width):
    for j in range(height):
      fill_rect(i*width_c,j*height_c,width_c,height_c,[(255,255,255),(0,0,0)][world[j][i]])

def step_up():
  global world
  for i in range(width):
    for j in range(height):
      t=0
      t+=world[(j-1)%height][(i-1)%width]
      t+=world[(j-1)%height][(i)%width]
      t+=world[(j-1)%height][(i+1)%width]
      t+=world[(j)%height][(i-1)%width]
      t+=world[(j)%height][(i+1)%width]
      t+=world[(j+1)%height][(i-1)%width]
      t+=world[(j+1)%height][(i)%width]
      t+=world[(j+1)%height][(i+1)%width]

      world2[j][i]=[False,False,world[j][i],True,False,False,False,False,False][t]

  for i in range(0,height):
    world[i]=world2[i][:]

plans=(
((0,0),(2,0),(2,1),(1,1),(1,2)),
((0,1),(0,2),(1,1),(1,2),(1,3),(2,3),(2,2),(3,2),(3,1),(4,1),(3,0),(2,0)),
#((0,4),(0,5),(1,4),(1,5),(10,4),(10,5),(10,6),(11,3),(11,7),(12,2),(12,8),(13,8),(13,2),(14,5),(15,3),(15,7),(16,4),(16,5),(16,6),(17,5),(20,2),(20,3),(20,4),(21,2),(21,3),(21,4),(22,1),(22,5),(24,0),(24,1),(24,5),(24,6),(34,2),(34,3),(35,2),(35,3)),
)

def apply_plan(x=0,y=0,n_plan=0):
  global world
  plan=plans[n_plan]
  for i in plan:
    world[(y+i[1])%height][(x+i[0])%width]=1

draw_string("LE JEU DE LA VIE",50,50)
draw_string("(de John Conway)",70,70)
draw_string("Appuies sur OK pour editer",30,90)

draw_string("=> Quand vous editer appuies sur\n       pour mettre une cellule",0,110)
draw_string("ONOFF",10,130,(255,255,255),(0,0,0))

draw_string("=> Quand vous editer appuies sur\n      pour enlever la cellule",0,150)
draw_string("HOME",10,170,(255,255,255),(255,185,50))

sleep(1)
while not keydown(KEY_OK):
  sleep(0.1)

draw_all()

while True:
  step_up()
  draw_all()
  if keydown(KEY_OK):
    while keydown(KEY_OK):
      fill_rect(width_c*x,height_c*y,width_c,height_c,[(100,100,100),(200,200,200)][world[y][x]])
      pass
    while not keydown(KEY_OK):
      fill_rect(x*width_c,y*height_c,width_c,height_c,[(255,255,255),(0,0,0)][world[y][x]])
      for i in range(4):
        if keydown(i):
          x=(x+(-1,0,0,1)[i])%width
          y=(y+(0,-1,1,0)[i])%height
      fill_rect(width_c*x,height_c*y,width_c,height_c,[(100,100,100),(200,200,200)][world[y][x]])

      for i in range(3):
        if keydown(i+12):
          apply_plan(x,y,i)
          draw_all()

      if keydown(KEY_ONOFF):
        world[y][x]=1
      if keydown(KEY_HOME):
        world[y][x]=0
      sleep(0.1)