Advertisement
Guest User

Pygame Wave Equation

a guest
Dec 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. import pygame
  2. import math
  3. import random
  4. from pygame.locals import *
  5.  
  6. #setup
  7.  
  8. background_colour = (0,0,0)
  9. (width, height) = (500, 500)
  10.  
  11. screen = pygame.display.set_mode((width, height))#,pygame.FULLSCREEN)
  12. screen.fill(background_colour)
  13. pygame.display.set_caption('Wave equation')
  14. pygame.font.init()
  15.  
  16. myfont = pygame.font.SysFont("monospace", 20)
  17.  
  18. clock = pygame.time.Clock()
  19.  
  20. class particle:
  21. def __init__(self, i, j):
  22. self.x = i
  23. self.y = j
  24. self.value = 0
  25. self.velocity = 0
  26. self.neighbours = []
  27.  
  28. def get_neighbours(self):
  29. if self.x >= 1 and self.x <= size_x - 2 and self.y >= 1 and self.y <= size_y - 2:
  30. self.neighbours.append(values[self.x - 1][self.y])
  31. self.neighbours.append(values[self.x + 1][self.y])
  32. self.neighbours.append(values[self.x][self.y - 1])
  33. self.neighbours.append(values[self.x][self.y + 1])
  34.  
  35. def update(self):
  36. if self.x >= 1 and self.x <= size_x - 2 and self.y >= 1 and self.y <= size_y - 2:
  37. lap = 0
  38. for n in self.neighbours:
  39. lap += n.value
  40. lap -= 4*self.value
  41. self.velocity += 0.01*lap
  42. self.velocity *= 0.97
  43. self.value += self.velocity
  44. else:
  45. self.value = 0
  46.  
  47. size_x = 50
  48. size_y = 50
  49.  
  50. values = []
  51. for i in range(size_x):
  52. values.append([])
  53. for j in range(size_y):
  54. values[i].append(particle(i,j))
  55.  
  56. for i in range(size_x - 1):
  57. for j in range(size_y - 1):
  58. values[i][j].get_neighbours()
  59.  
  60. #values[int(size_x/2)][int(size_y/2)].value = 20
  61.  
  62.  
  63. running = True
  64. while running:
  65. for event in pygame.event.get():
  66. if event.type == pygame.QUIT:
  67. running = False
  68. elif event.type == KEYDOWN:
  69. if event.key == K_ESCAPE:
  70. running = False
  71.  
  72.  
  73. screen.fill(background_colour)
  74.  
  75. pos = pygame.mouse.get_pos()
  76. grid_pos = [int(pos[0]/10), int(pos[1]/10)]
  77. values[grid_pos[0]][grid_pos[1]].value = 20
  78.  
  79. for i in range(size_x):
  80. for j in range(size_x):
  81. values[i][j].update()
  82. pygame.draw.circle(screen,
  83. [30,min(255,max(0,100 - 10*values[i][j].value)),min(255,max(0,100 + 10*values[i][j].value))],
  84. [10*i,10*j], 7)
  85.  
  86. pygame.display.flip()
  87.  
  88. clock.tick(60)
  89.  
  90.  
  91.  
  92. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement