Guest User

Untitled

a guest
Jan 11th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. import pygame
  2. import numpy as np
  3. import pygame.surfarray as surfarray
  4. from scipy.ndimage import convolve
  5. import random
  6.  
  7.  
  8. # Constants =======================================================================================
  9. size_x = size_y = 500
  10. laplace_mat = np.array([[0.05, 0.2, 0.05],
  11.                         [0.2, -1, 0.2],
  12.                         [0.05, 0.2, 0.05]])
  13.  
  14. # options are always of the shape:
  15. # [f, k]
  16. options = {
  17.     "coral": [0.0545, 0.062],
  18.     "mitosis": [0.0367, 0.0649],
  19.     "random": [random.random() / 10, random.random() / 10]
  20. }
  21.  
  22. option = "coral"
  23.  
  24. # Setup
  25. pygame.init()
  26.  
  27. screen = pygame.display.set_mode((size_x, size_y))
  28. pygame.display.set_caption("Test")
  29. pxarray = surfarray.pixels3d(screen)
  30.  
  31. initscreen = pygame.Surface((size_x, size_y))
  32. initarray = surfarray.pixels2d(initscreen)
  33.  
  34. D_A = 1
  35. D_B = 0.5
  36.  
  37.  
  38. f = options[option][0]
  39. k = options[option][1]
  40.  
  41. grid = np.array([[[1, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
  42. n_grid = np.array([[[0, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
  43.  
  44.  
  45. # Setup
  46. # pygame.draw.rect(initscreen, 1, pygame.Rect(120, 120, 60, 60))
  47. pygame.draw.circle(initscreen, 1, (250, 250), 50, 1)
  48. grid[:, :, 1] = initarray
  49. original = 0
  50. buffer = [grid, n_grid]
  51.  
  52.  
  53. loop = True
  54. while loop:
  55.     # time.sleep(0.05)
  56.     orig = buffer[original]
  57.     new = buffer[not original]
  58.  
  59.     for event in pygame.event.get():  # User did something
  60.         if event.type == pygame.QUIT:  # If user clicked close
  61.             loop = False
  62.         if event.type == pygame.KEYDOWN:
  63.             if event.key == pygame.K_SPACE:
  64.                 # original = 0
  65.                 # grid[:] = np.array([[[1, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
  66.                 # n_grid[:] = np.array([[[0, 0, 0] for i in range(size_x)] for j in range(size_y)], dtype=float)
  67.                 # grid[:, :, 1] = initarray
  68.                 #
  69.                 # f = random.random() / 10
  70.                 # k = random.random() / 10
  71.                 #
  72.                 # buffer = [grid, n_grid]
  73.                 pass
  74.  
  75.     # Updating
  76.     new[:, :, 0] = orig[:, :, 0] * (1 - f - np.square(orig[:, :, 1])) + f + D_A * convolve(orig[:, :, 0], laplace_mat)
  77.     new[:, :, 1] = orig[:, :, 1] * (1 - k - f + orig[:, :, 1] * orig[:, :, 0]) + D_B * convolve(orig[:, :, 1], laplace_mat)
  78.  
  79.     # Rendering
  80.  
  81.     pxarray[:] = np.transpose(np.tile(np.multiply(new, 255)[:, :, 0], (3, 1, 1)))
  82.     pygame.display.flip()
  83.     original = not original
  84. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment