Open csmangum opened 9 months ago
To enhance the code, let's implement a more interactive and efficient approach. This version will include:
Here's an improved version of the code:
import pygame
import numpy as np
# Pygame setup
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
# Grid setup
cell_size = 10 # Size of the cells
grid_size = 100 # Initial grid size
grid = np.random.randint(2, size=(grid_size, grid_size)) # Random initial state
def update_visible_grid(offset_x, offset_y):
global grid
start_x, start_y = max(0, -offset_x // cell_size), max(0, -offset_y // cell_size)
end_x = min(grid_size, width // cell_size - offset_x // cell_size)
end_y = min(grid_size, height // cell_size - offset_y // cell_size)
# Apply the rule only on the visible portion of the grid
for x in range(start_x, end_x):
for y in range(start_y, end_y):
# Simple rule for demonstration
neighbors = np.sum(grid[max(0, x-1):min(grid_size, x+2), max(0, y-1):min(grid_size, y+2)]) - grid[x, y]
grid[x, y] = 1 if neighbors > 0 and grid[x, y] == 0 else 0
def draw_grid(offset_x, offset_y):
screen.fill((255, 255, 255)) # Fill screen with white
for x in range(width // cell_size):
for y in range(height // cell_size):
grid_x, grid_y = x + offset_x // cell_size, y + offset_y // cell_size
if 0 <= grid_x < grid_size and 0 <= grid_y < grid_size and grid[grid_x, grid_y] == 1:
rect = pygame.Rect(x * cell_size, y * cell_size, cell_size, cell_size)
pygame.draw.rect(screen, (0, 0, 0), rect) # Draw black cell
def expand_grid(direction):
global grid, grid_size
# Expand the grid based on the direction of movement
if direction == "left" or direction == "right":
new_column = np.random.randint(2, size=(grid_size, 1))
grid = np.hstack((new_column, grid)) if direction == "left" else np.hstack((grid, new_column))
grid_size += 1
elif direction == "up" or direction == "down":
new_row = np.random.randint(2, size=(1, grid_size))
grid = np.vstack((new_row, grid)) if direction == "up" else np.vstack((grid, new_row))
grid_size += 1
def main():
offset_x, offset_y = 0, 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# User input for movement
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
offset_y += 10
expand_grid("up")
if keys[pygame.K_s]:
offset_y -= 10
expand_grid("down")
if keys[pygame.K_a]:
offset_x += 10
expand_grid("left")
if keys[pygame.K_d]:
offset_x -= 10
expand_grid("right")
update_visible_grid(offset_x, offset_y)
draw_grid(offset_x, offset_y)
pygame.display.flip()
clock.tick(30) # Increased to 30 frames per second for smoother movement
pygame.quit()
if __name__ == "__main__":
main()
expand_grid
function adds new rows or columns to the grid as the user moves, allowing for an infinite exploration experience. The grid expands by adding a random row or column on the side the user is moving towards.update_visible_grid
function now updates only the part of the grid that's currently visible to the user, plus a small buffer to make the transition smoother. This reduces the computational load.Optimized Drawing: The draw_grid
function only attempts to draw cells that are
within the current view, significantly reducing the number of drawing operations.
This code forms a more interactive and performance-efficient foundation for your procedural generation project. You can further optimize and expand upon it by integrating more complex cellular automata rules and adding features like zooming or different terrains based on cell states.
Creating a procedurally generated image that updates based on a specific cellular automata rule, with movement controls for panning through the image, involves several steps in Pygame. First, you'll need to initialize Pygame and set up the main game loop. Then, implement the cellular automata logic, handle user input for movement, and draw the grid based on the automata's state. Below is a basic example to get you started, using a very simplified automata rule for demonstration purposes.
This example assumes you're familiar with Pygame basics. The cellular automata rule used here is a placeholder; you can replace it with any rule you like, such as Conway's Game of Life or something more complex.
Key Components Explained:
update_grid
Function: This updates the grid's state based on your cellular automata rule. This example inverts a cell's state based on a simple condition. Replace this with your specific rule.draw_grid
Function: Draws each cell of the grid on the screen, offset byoffset_x
andoffset_y
to simulate panning.offset_x
andoffset_y
, which are used to pan through the generated image.You can enhance this example by implementing more complex automata rules, optimizing performance (e.g., updating only a portion of the grid visible on the screen), or adding more controls for user interaction.