|
| 1 | +import pygame |
| 2 | +import numpy as np |
| 3 | +import time |
| 4 | + |
| 5 | +# --- Configuration --- |
| 6 | +SQUARE_SIZE = 10 |
| 7 | +GRID_WIDTH = 60 |
| 8 | +GRID_HEIGHT = 40 |
| 9 | +SCREEN_WIDTH = GRID_WIDTH * SQUARE_SIZE |
| 10 | +SCREEN_HEIGHT = GRID_HEIGHT * SQUARE_SIZE |
| 11 | +COLOR_DEAD = (10, 10, 40) |
| 12 | +COLOR_ALIVE = (0, 255, 0) |
| 13 | +UPDATE_RATE = 10 # Generations per second |
| 14 | + |
| 15 | +# Initialize Pygame |
| 16 | +pygame.init() |
| 17 | +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) |
| 18 | +pygame.display.set_caption("Conway's Game of Life") |
| 19 | +clock = pygame.time.Clock() |
| 20 | + |
| 21 | +# Initialize Grid: 0 for dead, 1 for alive. |
| 22 | +# Use random initial state for a unique start. |
| 23 | +grid = np.random.randint(0, 2, size=(GRID_HEIGHT, GRID_WIDTH)) |
| 24 | + |
| 25 | +def draw_grid(surface, current_grid): |
| 26 | + """Draws the current state of the grid to the screen.""" |
| 27 | + for y in range(GRID_HEIGHT): |
| 28 | + for x in range(GRID_WIDTH): |
| 29 | + color = COLOR_ALIVE if current_grid[y, x] == 1 else COLOR_DEAD |
| 30 | + rect = pygame.Rect(x * SQUARE_SIZE, y * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE) |
| 31 | + pygame.draw.rect(surface, color, rect) |
| 32 | + |
| 33 | +def update_grid(current_grid): |
| 34 | + """Calculates the next generation of the grid based on the rules.""" |
| 35 | + new_grid = current_grid.copy() |
| 36 | + |
| 37 | + for y in range(GRID_HEIGHT): |
| 38 | + for x in range(GRID_WIDTH): |
| 39 | + # Count live neighbors, handling wrap-around for boundary conditions |
| 40 | + # Uses modulo operator (%) for toroidal (wrap-around) boundaries |
| 41 | + live_neighbors = ( |
| 42 | + current_grid[(y - 1) % GRID_HEIGHT, (x - 1) % GRID_WIDTH] + |
| 43 | + current_grid[(y - 1) % GRID_HEIGHT, x] + |
| 44 | + current_grid[(y - 1) % GRID_HEIGHT, (x + 1) % GRID_WIDTH] + |
| 45 | + current_grid[y, (x - 1) % GRID_WIDTH] + |
| 46 | + current_grid[y, (x + 1) % GRID_WIDTH] + |
| 47 | + current_grid[(y + 1) % GRID_HEIGHT, (x - 1) % GRID_WIDTH] + |
| 48 | + current_grid[(y + 1) % GRID_HEIGHT, x] + |
| 49 | + current_grid[(y + 1) % GRID_HEIGHT, (x + 1) % GRID_WIDTH] |
| 50 | + ) |
| 51 | + |
| 52 | + # Apply Conway's Rules: |
| 53 | + if current_grid[y, x] == 1: # ALIVE cell rules |
| 54 | + if live_neighbors < 2 or live_neighbors > 3: |
| 55 | + new_grid[y, x] = 0 # 1. Underpopulation or Overpopulation -> DEATH |
| 56 | + else: # DEAD cell rules |
| 57 | + if live_neighbors == 3: |
| 58 | + new_grid[y, x] = 1 # 2. Reproduction -> BIRTH |
| 59 | + |
| 60 | + return new_grid |
| 61 | + |
| 62 | +# --- Main Game Loop --- |
| 63 | +running = True |
| 64 | +paused = False |
| 65 | +while running: |
| 66 | + for event in pygame.event.get(): |
| 67 | + if event.type == pygame.QUIT: |
| 68 | + running = False |
| 69 | + if event.type == pygame.KEYDOWN: |
| 70 | + if event.key == pygame.K_SPACE: |
| 71 | + paused = not paused # Toggle pause with the spacebar |
| 72 | + |
| 73 | + # Fill the screen with the dead color |
| 74 | + screen.fill(COLOR_DEAD) |
| 75 | + |
| 76 | + # Update logic |
| 77 | + if not paused: |
| 78 | + grid = update_grid(grid) |
| 79 | + |
| 80 | + # Drawing logic |
| 81 | + draw_grid(screen, grid) |
| 82 | + |
| 83 | + # Update the display |
| 84 | + pygame.display.flip() |
| 85 | + |
| 86 | + # Control the speed of the simulation |
| 87 | + clock.tick(UPDATE_RATE) |
| 88 | + |
| 89 | +pygame.quit() |
0 commit comments