[Code aufräumen]
This commit is contained in:
parent
177213c053
commit
f661990411
|
@ -1 +1,3 @@
|
|||
tester.py
|
||||
tester.py
|
||||
venv/
|
||||
__pycache__
|
|
@ -0,0 +1,38 @@
|
|||
import random
|
||||
import time
|
||||
|
||||
def calc_check_area(row: int, column: int, rows: int, columns: int) -> list:
|
||||
min_row = row-1
|
||||
if min_row < 0:
|
||||
min_row = 0
|
||||
|
||||
max_row = row+2
|
||||
if max_row >= rows:
|
||||
max_row = rows
|
||||
|
||||
min_column = column-1
|
||||
if min_column < 0:
|
||||
min_column = 0
|
||||
|
||||
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))
|
||||
|
||||
return check_list
|
||||
|
||||
def randomise(columns: int, rows: int, probability: int, color_status: list, green: tuple):
|
||||
random.seed(int(time.time()))
|
||||
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
|
105
main.py
105
main.py
|
@ -1,18 +1,20 @@
|
|||
import pygame
|
||||
import random
|
||||
import time
|
||||
from functions import calc_check_area
|
||||
from functions import randomise
|
||||
|
||||
pygame.init()
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
width = 1000
|
||||
height = 1000
|
||||
width = 500
|
||||
height = 500
|
||||
|
||||
square_size = 10
|
||||
window = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption("Conways Game Of Life")
|
||||
|
||||
rows = height // square_size
|
||||
columns = width // square_size
|
||||
# Colors
|
||||
|
||||
black = (0, 0, 0)
|
||||
white = (255, 255, 255)
|
||||
|
@ -20,35 +22,34 @@ red = (255, 0, 0)
|
|||
green = (0, 255, 0)
|
||||
blue = (0, 0, 255)
|
||||
|
||||
color_status = [[white for _ in range(columns)] for _ in range(rows)]
|
||||
# Colors End
|
||||
|
||||
window = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption("Conways Game Of Life")
|
||||
# Variables
|
||||
|
||||
# vars
|
||||
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)
|
||||
|
||||
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
|
||||
# Variables End
|
||||
|
||||
color_status = [[dead_color for _ in range(columns)] for _ in range(rows)]
|
||||
|
||||
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()
|
||||
randomise(columns, rows, spawn_probability, color_status, alive_color)
|
||||
|
||||
old_color_status = list()
|
||||
|
||||
running = True
|
||||
while running:
|
||||
window.fill(white)
|
||||
window.fill(dead_color)
|
||||
|
||||
temp = color_status
|
||||
|
||||
|
@ -56,65 +57,47 @@ while running:
|
|||
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))
|
||||
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 == green:
|
||||
if check_tile_color == alive_color:
|
||||
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 (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 = white
|
||||
new_color = dead_color
|
||||
|
||||
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_ESCAPE:
|
||||
running = False
|
||||
|
||||
if event.key == pygame.K_r:
|
||||
randomise()
|
||||
elif event.key == pygame.K_UP:
|
||||
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:
|
||||
elif event.key == pygame.K_DOWN and die_probability > 0:
|
||||
die_probability -= 1
|
||||
print(f'Die Prob: {die_probability}')
|
||||
|
||||
|
||||
pygame.display.update()
|
||||
clock.tick(120)
|
||||
clock.tick(fps)
|
||||
|
||||
pygame.quit()
|
||||
pygame.quit()
|
Loading…
Reference in New Issue