Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import numpy as np
- import pygame.surfarray as surfarray
- from scipy.ndimage import convolve
- import random
- # Constants =======================================================================================
- size_x = size_y = 500
- laplace_mat = np.array([[0.05, 0.2, 0.05],
- [0.2, -1, 0.2],
- [0.05, 0.2, 0.05]])
- # options are always of the shape:
- # [f, k]
- options = {
- "coral": [0.0545, 0.062],
- "mitosis": [0.0367, 0.0649],
- "random": [random.random() / 10, random.random() / 10]
- }
- option = "coral"
- # Setup
- pygame.init()
- screen = pygame.display.set_mode((size_x, size_y))
- pygame.display.set_caption("Test")
- pxarray = surfarray.pixels3d(screen)
- initscreen = pygame.Surface((size_x, size_y))
- initarray = surfarray.pixels2d(initscreen)
- D_A = 1
- D_B = 0.5
- f = options[option][0]
- k = options[option][1]
- grid = np.array([[[1, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
- n_grid = np.array([[[0, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
- # Setup
- # pygame.draw.rect(initscreen, 1, pygame.Rect(120, 120, 60, 60))
- pygame.draw.circle(initscreen, 1, (250, 250), 50, 1)
- grid[:, :, 1] = initarray
- original = 0
- buffer = [grid, n_grid]
- loop = True
- while loop:
- # time.sleep(0.05)
- orig = buffer[original]
- new = buffer[not original]
- for event in pygame.event.get(): # User did something
- if event.type == pygame.QUIT: # If user clicked close
- loop = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- # original = 0
- # grid[:] = np.array([[[1, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
- # n_grid[:] = np.array([[[0, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
- # grid[:, :, 1] = initarray
- #
- # f = random.random() / 10
- # k = random.random() / 10
- #
- # buffer = [grid, n_grid]
- pass
- # Updating
- new[:, :, 0] = orig[:, :, 0] * (1 - f - np.square(orig[:, :, 1])) + f + D_A * convolve(orig[:, :, 0], laplace_mat)
- new[:, :, 1] = orig[:, :, 1] * (1 - k - f + orig[:, :, 1] * orig[:, :, 0]) + D_B * convolve(orig[:, :, 1], laplace_mat)
- # Rendering
- pxarray[:] = np.transpose(np.tile(np.multiply(new, 255)[:, :, 0], (3, 1, 1)))
- pygame.display.flip()
- original = not original
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment