Created
November 4, 2020 19:44
-
-
Save glassus/7ba12fbe6e7db6e2cf73dc33cdb9167f to your computer and use it in GitHub Desktop.
Jeu de la Vie, réalisé par Léo Capes TermNSI 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code réalisé par Léo Capes TNSI | |
import pygame, sys | |
import time | |
from pygame.locals import * | |
from random import randint | |
clock = pygame.time.Clock() | |
FPS = 30 | |
ecran_x = 600 # plein de parametres ! | |
ecran_y = 600 # plus il y a de cellules plus le framerate sera forcé d'être lent | |
taille_x = 10 | |
taille_y = 10 | |
marge = 2 | |
naissance = (3,) #regles du jeu | |
survie = (2,) | |
nb_cell_x = ecran_x//taille_x | |
nb_cell_y = ecran_y//taille_y | |
work = False | |
pygame.display.init() | |
fenetre = pygame.display.set_mode([ecran_x,ecran_y]) | |
fenetre.fill([125,125,125]) | |
pygame.key.set_repeat(100,40) | |
class Cell : | |
def __init__(self,x,y) : | |
self.x = x | |
self.y = y | |
self.state = 0 | |
self.color = [0,0,0] | |
self.neighbors = 0 | |
def draw(self) : | |
self.color = [255 * self.state for i in range (3)] | |
pygame.draw.rect(fenetre,self.color,(self.x*taille_x+marge//2,self.y*taille_y+marge//2,taille_x-marge,taille_y-marge)) | |
def count_neighbors(self) : | |
n = 0 | |
for y in range (-1,2): | |
for x in range (-1,2): | |
n += allcells[(self.y-y)%(nb_cell_y)][(self.x-x)%(nb_cell_x)].state | |
n = n - self.state | |
self.neighbors = n | |
def survive (self) : | |
if self.neighbors in naissance : | |
self.state = 1 | |
elif self.neighbors in survie : | |
None | |
else : | |
self.state = 0 | |
allcells = [] # création des cellules | |
for y in range(nb_cell_y) : | |
l = [] | |
for x in range(nb_cell_x) : | |
l.append(Cell(x,y)) | |
allcells.append(l) | |
while True : | |
fenetre.fill([125,125,125]) | |
for ligne in allcells : | |
for cell in ligne : | |
cell.count_neighbors() | |
cell.draw() | |
if work : | |
for ligne in allcells : | |
for cell in ligne : | |
cell.survive() | |
pygame.display.flip() | |
# routine pour pouvoir fermer «proprement» la fenêtre Pygame | |
for event in pygame.event.get(): | |
if event.type == MOUSEBUTTONDOWN : | |
pos = pygame.mouse.get_pos() | |
allcells[pos[1]//taille_y][pos[0]//taille_x].state = not allcells[pos[1]//taille_y][pos[0]//taille_x].state | |
elif event.type == pygame.KEYDOWN : # espace pour commencer ! | |
if event.key == K_SPACE : | |
work = not work | |
elif event.key == K_UP : # haut et bas pour changer les FPS en pleine action | |
FPS = (FPS*1.2)%120 | |
elif event.key == K_DOWN : | |
FPS = (FPS/1.2)%120 | |
elif event.type == pygame.QUIT: | |
pygame.display.quit() | |
sys.exit() | |
clock.tick(FPS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment