ConwaysGameOfLife/main.py

121 lines
3.6 KiB
Python
Raw Normal View History

2023-06-27 16:46:56 +00:00
import pygame
import random
import time
pygame.init()
clock = pygame.time.Clock()
width = 1000
height = 1000
square_size = 10
rows = height // square_size
columns = width // square_size
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
color_status = [[white for _ in range(columns)] for _ in range(rows)]
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Conways Game Of Life")
# vars
probability = 31.25 # standard 31.25
die_probability = 0
living_cells_around_new_life = 3 # standard 3
max_living_cells_around_con = 3 # standard 3
min_living_cells_arund_con = 2 # standard 2
random.seed(int(time.time()))
def randomise():
for column in range(columns):
for row in range(rows):
r = random.randint(0,10001)
if r <= int(probability*100):
color_status[row][column] = green
randomise()
old_color_status = list()
running = True
while running:
window.fill(white)
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))
pygame.draw.circle(window, tile_color, (column * square_size, row * square_size), square_size/2)
min_row = row-1
if min_row < 0:
min_row = 0
min_column = column-1
if min_column < 0:
min_column = 0
max_row = row+2
if max_row >= rows:
max_row = rows
max_column = column+2
if max_column >= columns:
max_column = columns
check_rows = list(range(min_row, max_row))
check_columns = list(range(min_column, max_column))
check_list = []
for check_row in check_rows:
for check_column in check_columns:
if not (check_row == row and check_column == column):
check_list.append((check_row, check_column))
for check_pos in check_list:
check_tile_color = color_status[check_pos[0]][check_pos[1]]
if check_tile_color == green:
living_cells_around += 1
new_color = tile_color
if (tile_color == white and living_cells_around == living_cells_around_new_life) or (tile_color == green and (living_cells_around == min_living_cells_arund_con or living_cells_around == max_living_cells_around_con)):
new_color = green
elif (tile_color == green and living_cells_around < 2) or (tile_color == green and living_cells_around > 3):
new_color = white
if random.randint(0,10001) <= die_probability*100:
new_color = white
temp[row][column] = new_color
color_status = temp
'''
if color_status == old_color_status:
randomise()
else:
old_color_status = color_status
'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_r:
randomise()
elif event.key == pygame.K_UP:
die_probability += 1
print(f'Die Prob: {die_probability}')
elif event.key == pygame.K_DOWN:
die_probability -= 1
print(f'Die Prob: {die_probability}')
pygame.display.update()
clock.tick(120)
pygame.quit()