commit 177213c0537fbc83d61f2f9fde73b45e42273a5f Author: xoy Date: Tue Jun 27 18:46:56 2023 +0200 [Projekt erstellt] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03f2471 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tester.py \ No newline at end of file diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..c6f6273 --- /dev/null +++ b/README.MD @@ -0,0 +1,2 @@ +# Conways Game Of Life + diff --git a/main.py b/main.py new file mode 100644 index 0000000..957f002 --- /dev/null +++ b/main.py @@ -0,0 +1,120 @@ +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()