ConwaysGameOfLife/main.py

103 lines
3.4 KiB
Python
Raw Normal View History

2023-06-27 16:46:56 +00:00
import pygame
import random
import time
2023-06-27 18:15:34 +00:00
from functions import calc_check_area
from functions import randomise
2023-06-27 16:46:56 +00:00
pygame.init()
clock = pygame.time.Clock()
2023-06-27 18:15:34 +00:00
width = 500
height = 500
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Conways Game Of Life")
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
# Colors
2023-06-27 16:46:56 +00:00
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
2023-06-27 18:15:34 +00:00
# Colors End
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
# Variables
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
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)
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
# Variables End
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
color_status = [[dead_color for _ in range(columns)] for _ in range(rows)]
2023-06-27 16:46:56 +00:00
random.seed(int(time.time()))
2023-06-27 18:15:34 +00:00
randomise(columns, rows, spawn_probability, color_status, alive_color)
2023-06-27 16:46:56 +00:00
old_color_status = list()
running = True
while running:
2023-06-27 18:15:34 +00:00
window.fill(dead_color)
2023-06-27 16:46:56 +00:00
temp = color_status
for row in range(rows):
for column in range(columns):
living_cells_around = 0
tile_color = color_status[row][column]
2023-06-27 18:15:34 +00:00
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)
2023-06-27 16:46:56 +00:00
for check_pos in check_list:
check_tile_color = color_status[check_pos[0]][check_pos[1]]
2023-06-27 18:15:34 +00:00
if check_tile_color == alive_color:
2023-06-27 16:46:56 +00:00
living_cells_around += 1
2023-06-27 18:15:34 +00:00
2023-06-27 16:46:56 +00:00
new_color = tile_color
2023-06-27 18:15:34 +00:00
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
2023-06-27 16:46:56 +00:00
if random.randint(0,10001) <= die_probability*100:
2023-06-27 18:15:34 +00:00
new_color = dead_color
2023-06-27 16:46:56 +00:00
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:
2023-06-27 18:15:34 +00:00
if event.key == pygame.K_ESCAPE:
running = False
2023-06-27 16:46:56 +00:00
if event.key == pygame.K_r:
2023-06-27 18:15:34 +00:00
randomise(columns, rows, spawn_probability, color_status, alive_color)
if event.key == pygame.K_UP and die_probability < 100:
2023-06-27 16:46:56 +00:00
die_probability += 1
print(f'Die Prob: {die_probability}')
2023-06-27 18:15:34 +00:00
elif event.key == pygame.K_DOWN and die_probability > 0:
2023-06-27 16:46:56 +00:00
die_probability -= 1
print(f'Die Prob: {die_probability}')
pygame.display.update()
2023-06-27 18:15:34 +00:00
clock.tick(fps)
2023-06-27 16:46:56 +00:00
2023-06-27 18:15:34 +00:00
pygame.quit()