-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (57 loc) · 2.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pygame
from Grille import *
from constantes import *
from Cellule import *
import sys
import time
class Jeu:
def __init__(self):
self.ecran = pygame.display.set_mode(TAILLEFENETRE)
pygame.display.set_caption('Jeu de la vie')
self.jeu_encours = True
self.grille = Grille(NBCASEX, NBCASEY)
self.grille.init()
self.pause = True
self.saved_grille = Grille(NBCASEX,NBCASEY)
self.saved_grille.init()
def boucle_principale(self):
while self.jeu_encours:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if self.pause :
self.pause = False
self.saved_grille.copy(self.grille)
else:
self.pause = True
if event.key == pygame.K_ESCAPE:
self.grille.clear()
self.ecran.fill(BLANC)
self.grille.afficherGrille(self.ecran)
pygame.display.flip()
self.pause = True
if event.key == pygame.K_s:
self.grille.copy(self.saved_grille)
if event.type == pygame.MOUSEBUTTONDOWN and self.pause == True:
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
if 0 <= x <= NBCASEX*TAILLECARRE and 0 <= y <= NBCASEY*TAILLECARRE:
if self.grille.tabCellules[int(x/TAILLECARRE)][int(y/TAILLECARRE)].etat == 1 :
self.grille.tabCellules[int(x/TAILLECARRE)][int(y/TAILLECARRE)].etat = 0
elif self.grille.tabCellules[int(x/TAILLECARRE)][int(y/TAILLECARRE)].etat == 0 :
self.grille.tabCellules[int(x/TAILLECARRE)][int(y/TAILLECARRE)].etat = 1
self.grille.verifVoisines()
if not(self.pause):
self.grille.verifVoisines()
self.grille.verifEtats()
time.sleep(0.5)
self.ecran.fill(BLANC)
self.grille.afficherGrille(self.ecran)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
Jeu().boucle_principale()
pygame.quit()