# from time import sleep from time import time from random import randint import pygame import sys screen = None world = None alive_color = None clock = None new_grid = [] WIDTH = 0 HEIGHT = 0 print(""" --------------------- GAME OF LIFE ---------------------- A pretty version by Wikle ;) --------------------------------------------------------- [!] when the board will be drawn you should be able to launch the simulation by clicking anywhere on it. [!] To stop the simulation click anywhere on the screen [!] [?] READY TO SEE BEAUTIFUL SCIENCE ?? [?] """) CELL_SIZE = input("[+] Choose the size of a cell in pixel(Default : 9) >") MAX_ITER = input("[+] How many generation do you wanna run ? (Default : 500) >") try: CELL_SIZE = int(CELL_SIZE) except Exception: CELL_SIZE = 9 try: MAX_ITER = int(MAX_ITER) except Exception: MAX_ITER = 500 graphic = True BLACK = (0, 0, 0) WHITE = (255, 255, 255) def createScreen(): print('available resolutions', pygame.display.list_modes(0)) # the next two lines set up full screen options, to run in a window see below screen_width, screen_height = pygame.display.list_modes(0)[0] # we use the 1st resolution which is the largest, and ought to give us the full multi-monitor a = str(input("fullscreen type 0 else type anything >")) if a == "0" and graphic: options = pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF # the next two lines set up windowed options - swap these with above to run full screen instead else: screen_width, screen_height = (1920, 1080) options = 0 # create the screen with the options screen = pygame.display.set_mode( (screen_width, screen_height), options) print("screen created, size is:", screen.get_size()) return screen def evolve_cell(alive, neighbours): return neighbours == 3 or (alive and neighbours == 2) def count_neighbours(grid, position): x, y = position neighbour_cells = [(x - 1, y - 1), (x - 1, y + 0), (x - 1, y + 1), (x + 0, y - 1), (x + 0, y + 1), (x + 1, y - 1), (x + 1, y + 0), (x + 1, y + 1)] count = 0 for x, y in neighbour_cells: if x >= 0 and y >= 0 and x < WIDTH and y < HEIGHT: count += grid[x][y] return count def evolve(alive_color=WHITE): global world, new_grid # new_grid = [[0 for j in range(HEIGHT)] for i in range(WIDTH)] for i in range(WIDTH): for j in range(HEIGHT): if evolve_cell(world[i][j], count_neighbours(world, (i, j))): new_grid[i][j] = 1 draw_block(i, j, alive_color) else: new_grid[i][j] = 0 draw_block(i, j, BLACK) for i in range(WIDTH): for j in range(HEIGHT): world[i][j] = new_grid[i][j] # return new_grid def draw_block(x, y, alive_color=WHITE): global screen x *= CELL_SIZE y *= CELL_SIZE center_point = (x + CELL_SIZE // 2, y + CELL_SIZE // 2) pygame.draw.circle(screen, alive_color, center_point, CELL_SIZE // 2, 0) # this is where we register our event listeners # yes, we're just calling methods def handleInputEvents(): for event in pygame.event.get(): if(event.type == pygame.MOUSEBUTTONDOWN): if(event.button == 1): # left click # global world # world = make_random_grid(WIDTH, HEIGHT) sys.exit(0) if(event.type == pygame.KEYDOWN): # sys.exit(0) # quit on any key pass if (event.type == pygame.QUIT): # pygame issues a quit event, for e.g. by closing the window print("[+] quitting type any key then enter to QUIT >") a = input() sys.exit(0) def drawBoard(alive, alive_color=WHITE): for i in range(WIDTH): for j in range(HEIGHT): draw_block(i, j, alive_color if alive[i][j] else BLACK) pygame.display.flip() def main(): global screen, world, WIDTH, HEIGHT, alive_color, clock, new_grid pygame.init() clock = pygame.time.Clock() screen = createScreen() (xmax, ymax) = screen.get_size() xmax, ymax = int(xmax), int(ymax) h = 0 alive_color = pygame.Color(0, 0, 0) alive_color.hsva = [h, 100, 100] WIDTH = xmax // CELL_SIZE # nb of cells in the horizontal way HEIGHT = ymax // CELL_SIZE # nb of cells in the vertical way new_grid = [[0 for j in range(HEIGHT)] for i in range(WIDTH)] print(f"WIDTH : {WIDTH}, HEIGHT : {HEIGHT}") world = [[randint(0, 1) for j in range(HEIGHT)] for i in range(WIDTH)] clock.tick(40) drawBoard(world, alive_color) test = True while test: for event in pygame.event.get(): if(event.type == pygame.MOUSEBUTTONDOWN): if(event.button == 1): # left click main_loop() test = False break def main_loop(): global screen, world, WIDTH, HEIGHT, alive_color, clock h = 0 time_start = time() loop_index = 0 a, b, d = 0, 0, 0 for loop_index in range(MAX_ITER): a = time() evolve(alive_color) b = time() if graphic: handleInputEvents() clock.tick(40) # drawBoard(world, alive_color) pygame.display.flip() h = (h + 2) % 360 # beauty trick to change the color ahah alive_color.hsva = (h, 100, 100) # d = time() print(f"loop {loop_index} time to calculate :{b - a}") print(f"TOTAL TIME TO LOOP {MAX_ITER} : {time() - time_start}") if __name__ == '__main__': main()