84 lines
2.9 KiB
Plaintext
84 lines
2.9 KiB
Plaintext
|
Sure, I can guide you through creating a simple implementation of Conway's Game of Life using Pygame in Python. The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway. It's a zero-player game, meaning its evolution is determined by its initial state, requiring no further input.
|
||
|
|
||
|
Make sure you have the `pygame` library installed. If not, you can install it using pip:
|
||
|
|
||
|
```bash
|
||
|
pip install pygame
|
||
|
```
|
||
|
|
||
|
Here is a sample code for the Game of Life using Pygame:
|
||
|
|
||
|
```python
|
||
|
import pygame
|
||
|
import numpy as np
|
||
|
|
||
|
# Initialize pygame
|
||
|
pygame.init()
|
||
|
|
||
|
# Define constants
|
||
|
WIDTH, HEIGHT = 800, 600
|
||
|
CELL_SIZE = 10
|
||
|
GRID_WIDTH, GRID_HEIGHT = WIDTH // CELL_SIZE, HEIGHT // CELL_SIZE
|
||
|
|
||
|
# Set up the display
|
||
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||
|
pygame.display.set_caption("Conway's Game of Life")
|
||
|
|
||
|
# Initialize the grid
|
||
|
def empty_grid(width, height):
|
||
|
return np.zeros((width, height), dtype=int)
|
||
|
|
||
|
def random_grid(width, height):
|
||
|
return np.random.randint(2, size=(width, height), dtype=int)
|
||
|
|
||
|
grid = random_grid(GRID_WIDTH, GRID_HEIGHT)
|
||
|
|
||
|
# Game of Life logic
|
||
|
def update_grid(grid):
|
||
|
new_grid = grid.copy()
|
||
|
for i in range(grid.shape[0]):
|
||
|
for j in range(grid.shape[1]):
|
||
|
# Count living neighbors
|
||
|
live_neighbors = np.sum(grid[i-1:i+2, j-1:j+2]) - grid[i, j]
|
||
|
|
||
|
if grid[i, j] == 1 and (live_neighbors < 2 or live_neighbors > 3):
|
||
|
new_grid[i, j] = 0
|
||
|
elif grid[i, j] == 0 and live_neighbors == 3:
|
||
|
new_grid[i, j] = 1
|
||
|
return new_grid
|
||
|
|
||
|
# Game Loop
|
||
|
running = True
|
||
|
clock = pygame.time.Clock()
|
||
|
|
||
|
while running:
|
||
|
screen.fill((0, 0, 0))
|
||
|
|
||
|
for event in pygame.event.get():
|
||
|
if event.type == pygame.QUIT:
|
||
|
running = False
|
||
|
|
||
|
# Update the grid and draw the new state
|
||
|
grid = update_grid(grid)
|
||
|
|
||
|
for i in range(GRID_WIDTH):
|
||
|
for j in range(GRID_HEIGHT):
|
||
|
color = (255, 255, 255) if grid[i, j] == 1 else (0, 0, 0)
|
||
|
rect = (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE)
|
||
|
pygame.draw.rect(screen, color, rect)
|
||
|
|
||
|
pygame.display.flip()
|
||
|
clock.tick(10) # control the simulation speed
|
||
|
|
||
|
# Quit Pygame
|
||
|
pygame.quit()
|
||
|
```
|
||
|
|
||
|
This code sets up a Pygame window with a grid of cells, each of which can be alive (white) or dead (black). The grid updates according to the rules of Conway's Game of Life:
|
||
|
|
||
|
1. Any live cell with fewer than two live neighbors dies, as if by underpopulation.
|
||
|
2. Any live cell with two or three live neighbors lives on to the next generation.
|
||
|
3. Any live cell with more than three live neighbors dies, as if by overpopulation.
|
||
|
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
|
||
|
|
||
|
The grid initially starts off with a random state. It updates with each tick of the game's clock and is displayed in the Pygame window. The simulation speed can be adjusted by changing the `clock.tick()` parameter.
|