-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
156 lines (114 loc) · 6.47 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 4 12:50:20 2020
@author: Daniel
"""
import sys
from Cell_State import CellState
from colors import ConsoleColor
from colors import get_random_color
import pygame
from Game_Of_Life_Simulation import GameOfLifeSimulation
from Rule import Rule
from Rule import Ruleset
from random import seed
# from enum import Enum
from Simulation_States import SimulationStates
from Cellular_Automaton_Execution_Stage import CellularAutomatonExecutionStage
# AUTOMATON EXECUTION PREFERENCES --------------------------------------------------------------------------------------------------------------------- #
ROW_AMOUNT = 30 # SET THE AMOUNT OF ROWS OF THE CELL MATRIX
COLL_AMOUNT = 30 # SET THE AMOUNT OF COLLUMNS OF THE CELL MATRIX
START_MATRIX_EMPTY = False # DECIDES IF THE MATRIX OF CELLS STARTS EMTPY OR WITH A RANDOM STATE (PER CELL)
MATRIX_INITIALIZATION_SEED = 0 # SET THE SEED FOR THE INITIALIZATION OF THE GRID
STARTING_INTERACTION_STATE = CellularAutomatonExecutionStage.PLAY_MODE
"""
CellularAutomatonExecutionStage.EDIT_MODE --> TO PAINT FILLED AND EMPTY CELLS ONTO THE MATRIX
CellularAutomatonExecutionStage.PLAY_MODE --> TO EXECUTE THE AUTOMATON
"""
# START PYGAME ------------------------------------------------------------------------------------------------------------------ #
# Initialize Screen
pygame.init() # INITIALIZE ENGINE
SCREEN_BASE_SIZE = (920,920)
SCREEN_TITLE = "Game_Of_Life"
SCREEN_SIZE_MULTIPLIER = (1,1)
SCREEN_COMPUTED_SIZE = (int(SCREEN_BASE_SIZE[0] * SCREEN_SIZE_MULTIPLIER[0]) ,int(SCREEN_BASE_SIZE[1] * SCREEN_SIZE_MULTIPLIER[1]))
clock = pygame.time.Clock()
TICK_RATE = 60
is_simulation_over = False
# create the new window
screen = pygame.display.set_mode(( SCREEN_COMPUTED_SIZE[0] ,SCREEN_COMPUTED_SIZE[1])) # INITIALIZE WINDOW
screen.fill((255, 255, 255))
pygame.display.set_caption(SCREEN_TITLE)
# CREATE THE RULES -------------------------------------------------------------------------------------------------------------- #
# Here we create the rules that the system will be using
simulation_rules = []
simulation_rules.append(Rule("Death by underpopulation",CellState.FILLED,CellState.FILLED,0,1,CellState.EMPTY))
simulation_rules.append(Rule("sustainable life",CellState.FILLED,CellState.FILLED,2,3,CellState.FILLED))
simulation_rules.append(Rule("Death by overpopulation",CellState.FILLED,CellState.FILLED,4,8,CellState.EMPTY)) # -1 == infinito a la hora de comprobarlo
simulation_rules.append(Rule("Birth",CellState.EMPTY,CellState.FILLED,3,3,CellState.FILLED))
# and pack them into the ruleset object
game_of_life_ruleset = Ruleset(simulation_rules)
sys.stdout.write(ConsoleColor.YELLOW + ConsoleColor.BOLD) # set the color of the text
print ("\t Ruleset configured succesfully")
sys.stdout.write(ConsoleColor.RESET) # RESET the color of the text
# CREATE SIMULATION ------------------------------------------------------------------------------------------------------------- #
sys.stdout.write(ConsoleColor.BLUE)
seed(MATRIX_INITIALIZATION_SEED) # set the seed for the random initialization
game_of_life = GameOfLifeSimulation(COLL_AMOUNT, ROW_AMOUNT,SCREEN_COMPUTED_SIZE[0],SCREEN_COMPUTED_SIZE[1],game_of_life_ruleset, START_MATRIX_EMPTY)
sys.stdout.write(ConsoleColor.YELLOW + ConsoleColor.BOLD) # set the color of the text
print ("\t Setup performed succesfully")
sys.stdout.write(ConsoleColor.RESET) # RESET the color of the text
# # MAIN SYSTEM LOOP -------------------------------------------------------------------------------------------------------------
current_interaction_state = STARTING_INTERACTION_STATE # state of the simulation (edit or play)
current_simulation_state = SimulationStates.COMPUTING # internal state of the simulation (computing or applying states)
is_mouse_Up = True # is the mouse up after a click?
targeted_cell = None # selected cell
previously_targeted_cell = None # previously selected cell
is_space_pressed = False
while is_simulation_over == False:
e = pygame.event.poll()
if e.type == pygame.QUIT: # EXIT event
is_simulation_over = True
elif e.type == pygame.KEYUP: # EDIT/PLAY toggle event
if e.key == pygame.K_SPACE:
# make this more elegant dani
if current_interaction_state == CellularAutomatonExecutionStage.EDIT_MODE:
current_interaction_state = CellularAutomatonExecutionStage.PLAY_MODE
else:
current_interaction_state = CellularAutomatonExecutionStage.EDIT_MODE
is_space_pressed = True
elif e.type == pygame.MOUSEBUTTONUP: # edit mode CELL STATE toggle
is_mouse_Up = True
pygame.display.update()
clock.tick(TICK_RATE)
# tick rate testing (background)
# screen.fill(get_random_color(False))
# execution of the automata
if current_interaction_state == CellularAutomatonExecutionStage.PLAY_MODE:
# ACT DEPENDING THE SIMULATION STATE
if current_simulation_state == SimulationStates.COMPUTING:
# if the return of the function is not none is because it just completed all the cells
if game_of_life.compute_cell_per_cell() != None:
current_simulation_state = SimulationStates.UPDATING
elif current_simulation_state == SimulationStates.UPDATING:
# if the return of the function is not none is because it just completed all the cells
if game_of_life.update_cell_per_cell() != None:
current_simulation_state = SimulationStates.COMPUTING
# Automata edit state
else:
# mouse primary button pressed
if pygame.mouse.get_pressed()[0]:
# get mouse position
mouse_screen_position = pygame.mouse.get_pos()
if targeted_cell is not None:
previously_targeted_cell = targeted_cell
targeted_cell = game_of_life.cell_matrix.get_cell_at_pixel(mouse_screen_position[0],mouse_screen_position[1],SCREEN_COMPUTED_SIZE[0],SCREEN_COMPUTED_SIZE[1])
if targeted_cell != None:
if (is_mouse_Up == True):
# targeted_cell.toggle_highlighted_state()
targeted_cell.toggle_cell_state()
is_mouse_Up = False
# redraw the matrix
game_of_life.draw_cells(screen)
print ("Exiting window")
pygame.quit()