103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
import pygame
|
|
import random
|
|
import time
|
|
from functions import calc_check_area
|
|
from functions import randomise
|
|
|
|
pygame.init()
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
width = 500
|
|
height = 500
|
|
|
|
window = pygame.display.set_mode((width, height))
|
|
pygame.display.set_caption("Conways Game Of Life")
|
|
|
|
# Colors
|
|
|
|
black = (0, 0, 0)
|
|
white = (255, 255, 255)
|
|
red = (255, 0, 0)
|
|
green = (0, 255, 0)
|
|
blue = (0, 0, 255)
|
|
|
|
# Colors End
|
|
|
|
# Variables
|
|
|
|
spawn_probability: float = 31.25 # standard 31.25 (%)
|
|
die_probability: float = 0.00 # standard 0.00 (%)
|
|
living_cells_around_needed_new_life: int = 3 # standard 3
|
|
living_cells_around_needed: tuple[int, int] = (2, 3) # standard (2, 3) (min, max)
|
|
alive_color: tuple[int, int, int] = green
|
|
dead_color: tuple[int, int, int] = white
|
|
square_size: int = 50
|
|
rows: int = height // square_size
|
|
columns: int = width // square_size
|
|
fps: int = 30 # standard 30 (FPS)
|
|
|
|
# Variables End
|
|
|
|
color_status = [[dead_color for _ in range(columns)] for _ in range(rows)]
|
|
|
|
random.seed(int(time.time()))
|
|
|
|
randomise(columns, rows, spawn_probability, color_status, alive_color)
|
|
|
|
old_color_status = list()
|
|
|
|
running = True
|
|
while running:
|
|
window.fill(dead_color)
|
|
|
|
temp = color_status
|
|
|
|
for row in range(rows):
|
|
for column in range(columns):
|
|
living_cells_around = 0
|
|
tile_color = color_status[row][column]
|
|
|
|
pygame.draw.rect(window, tile_color, (column * square_size, row * square_size, square_size, square_size))
|
|
|
|
check_list = calc_check_area(row, column, rows, columns)
|
|
|
|
for check_pos in check_list:
|
|
check_tile_color = color_status[check_pos[0]][check_pos[1]]
|
|
if check_tile_color == alive_color:
|
|
living_cells_around += 1
|
|
|
|
new_color = tile_color
|
|
|
|
if (tile_color == dead_color and living_cells_around == living_cells_around_needed_new_life) or (tile_color == alive_color and (living_cells_around == living_cells_around_needed[0] or living_cells_around == living_cells_around_needed[1])):
|
|
new_color = alive_color
|
|
elif (tile_color == alive_color and living_cells_around < living_cells_around_needed[0]) or (tile_color == alive_color and living_cells_around > living_cells_around_needed[1]):
|
|
new_color = dead_color
|
|
if random.randint(0,10001) <= die_probability*100:
|
|
new_color = dead_color
|
|
|
|
temp[row][column] = new_color
|
|
|
|
color_status = temp
|
|
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
elif event.type == pygame.KEYUP:
|
|
if event.key == pygame.K_ESCAPE:
|
|
running = False
|
|
|
|
if event.key == pygame.K_r:
|
|
randomise(columns, rows, spawn_probability, color_status, alive_color)
|
|
|
|
if event.key == pygame.K_UP and die_probability < 100:
|
|
die_probability += 1
|
|
print(f'Die Prob: {die_probability}')
|
|
elif event.key == pygame.K_DOWN and die_probability > 0:
|
|
die_probability -= 1
|
|
print(f'Die Prob: {die_probability}')
|
|
|
|
pygame.display.update()
|
|
clock.tick(fps)
|
|
|
|
pygame.quit() |