[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 pygame
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
from functions import calc_check_area
|
||||||
|
from functions import randomise
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
width = 1000
|
width = 500
|
||||||
height = 1000
|
height = 500
|
||||||
|
|
||||||
square_size = 10
|
window = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Conways Game Of Life")
|
||||||
|
|
||||||
rows = height // square_size
|
# Colors
|
||||||
columns = width // square_size
|
|
||||||
|
|
||||||
black = (0, 0, 0)
|
black = (0, 0, 0)
|
||||||
white = (255, 255, 255)
|
white = (255, 255, 255)
|
||||||
|
@ -20,35 +22,34 @@ red = (255, 0, 0)
|
||||||
green = (0, 255, 0)
|
green = (0, 255, 0)
|
||||||
blue = (0, 0, 255)
|
blue = (0, 0, 255)
|
||||||
|
|
||||||
color_status = [[white for _ in range(columns)] for _ in range(rows)]
|
# Colors End
|
||||||
|
|
||||||
window = pygame.display.set_mode((width, height))
|
# Variables
|
||||||
pygame.display.set_caption("Conways Game Of Life")
|
|
||||||
|
|
||||||
# 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
|
# Variables End
|
||||||
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
|
|
||||||
|
|
||||||
|
color_status = [[dead_color for _ in range(columns)] for _ in range(rows)]
|
||||||
|
|
||||||
random.seed(int(time.time()))
|
random.seed(int(time.time()))
|
||||||
|
|
||||||
def randomise():
|
randomise(columns, rows, spawn_probability, color_status, alive_color)
|
||||||
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()
|
old_color_status = list()
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
window.fill(white)
|
window.fill(dead_color)
|
||||||
|
|
||||||
temp = color_status
|
temp = color_status
|
||||||
|
|
||||||
|
@ -56,65 +57,47 @@ while running:
|
||||||
for column in range(columns):
|
for column in range(columns):
|
||||||
living_cells_around = 0
|
living_cells_around = 0
|
||||||
tile_color = color_status[row][column]
|
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:
|
pygame.draw.rect(window, tile_color, (column * square_size, row * square_size, square_size, square_size))
|
||||||
for check_column in check_columns:
|
|
||||||
if not (check_row == row and check_column == column):
|
check_list = calc_check_area(row, column, rows, columns)
|
||||||
check_list.append((check_row, check_column))
|
|
||||||
for check_pos in check_list:
|
for check_pos in check_list:
|
||||||
check_tile_color = color_status[check_pos[0]][check_pos[1]]
|
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
|
living_cells_around += 1
|
||||||
|
|
||||||
new_color = tile_color
|
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
|
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])):
|
||||||
elif (tile_color == green and living_cells_around < 2) or (tile_color == green and living_cells_around > 3):
|
new_color = alive_color
|
||||||
new_color = white
|
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:
|
if random.randint(0,10001) <= die_probability*100:
|
||||||
new_color = white
|
new_color = dead_color
|
||||||
|
|
||||||
temp[row][column] = new_color
|
temp[row][column] = new_color
|
||||||
|
|
||||||
color_status = temp
|
color_status = temp
|
||||||
'''
|
|
||||||
if color_status == old_color_status:
|
|
||||||
randomise()
|
|
||||||
else:
|
|
||||||
old_color_status = color_status
|
|
||||||
'''
|
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
running = False
|
running = False
|
||||||
elif event.type == pygame.KEYUP:
|
elif event.type == pygame.KEYUP:
|
||||||
|
if event.key == pygame.K_ESCAPE:
|
||||||
|
running = False
|
||||||
|
|
||||||
if event.key == pygame.K_r:
|
if event.key == pygame.K_r:
|
||||||
randomise()
|
randomise(columns, rows, spawn_probability, color_status, alive_color)
|
||||||
elif event.key == pygame.K_UP:
|
|
||||||
|
if event.key == pygame.K_UP and die_probability < 100:
|
||||||
die_probability += 1
|
die_probability += 1
|
||||||
print(f'Die Prob: {die_probability}')
|
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
|
die_probability -= 1
|
||||||
print(f'Die Prob: {die_probability}')
|
print(f'Die Prob: {die_probability}')
|
||||||
|
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
clock.tick(120)
|
clock.tick(fps)
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
Loading…
Reference in New Issue