Advertisement
MrLunk

Simple life simulation python pygame

Feb 19th, 2023 (edited)
785
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | Science | 0 1
  1. """
  2. This code sets up the game window, initializes the grid of cells with random live/dead values, defines a function to count live neighbors, and implements the main game loop that updates the grid and draws the cells on the screen.
  3.  
  4. The simulation features a grid of cells that can be either alive or dead, and updates based on the following rules:
  5.  
  6.    If a cell is alive and has fewer than two live neighbors, it dies (underpopulation).
  7.    If a cell is alive and has two or three live neighbors, it continues to live.
  8.    If a cell is alive and has more than three live neighbors, it dies (overpopulation).
  9.    If a cell is dead and has exactly three live neighbors, it becomes alive (reproduction).
  10.  
  11. First, you'll need to install Pygame by running 'pip install pygame' in your terminal.
  12. """
  13.  
  14. import pygame
  15. import random
  16.  
  17. # Set up the game window
  18. WINDOW_WIDTH = 800
  19. WINDOW_HEIGHT = 800
  20. CELL_SIZE = 10
  21. ROWS = WINDOW_HEIGHT // CELL_SIZE
  22. COLS = WINDOW_WIDTH // CELL_SIZE
  23.  
  24. pygame.init()
  25. pygame.display.set_caption("Life Simulation")
  26. window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
  27.  
  28. # Initialize the grid of cells
  29. grid = [[random.choice([0, 1]) for _ in range(COLS)] for _ in range(ROWS)]
  30.  
  31. # Define a function to count the number of live neighbors of a given cell
  32. def count_live_neighbors(row, col):
  33.     count = 0
  34.     for i in range(-1, 2):
  35.         for j in range(-1, 2):
  36.             if not (i == 0 and j == 0) and 0 <= row + i < ROWS and 0 <= col + j < COLS:
  37.                 count += grid[row + i][col + j]
  38.     return count
  39.  
  40. # Define the main game loop
  41. while True:
  42.     # Handle events
  43.     for event in pygame.event.get():
  44.         if event.type == pygame.QUIT:
  45.             pygame.quit()
  46.             quit()
  47.  
  48.     # Update the grid
  49.     new_grid = [[0 for _ in range(COLS)] for _ in range(ROWS)]
  50.     for row in range(ROWS):
  51.         for col in range(COLS):
  52.             live_neighbors = count_live_neighbors(row, col)
  53.             if grid[row][col] == 1 and (live_neighbors < 2 or live_neighbors > 3):
  54.                 new_grid[row][col] = 0
  55.             elif grid[row][col] == 0 and live_neighbors == 3:
  56.                 new_grid[row][col] = 1
  57.             else:
  58.                 new_grid[row][col] = grid[row][col]
  59.     grid = new_grid
  60.  
  61.     # Draw the cells on the screen
  62.     window.fill((255, 255, 255))
  63.     for row in range(ROWS):
  64.         for col in range(COLS):
  65.             if grid[row][col] == 1:
  66.                 pygame.draw.rect(window, (0, 0, 0), (col * CELL_SIZE, row * CELL_SIZE, CELL_SIZE, CELL_SIZE))
  67.     pygame.display.update()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement