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