The sources that I used to write this code are https://web.archive.org/web/20160418004149/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm and https://www.youtube.com/watch?v=BZUdGqeOD0w. The problem I'm having with the algorithm is that it often produces negative numbers, which are not valid for colour. Here is my code (I know that I shouldn't use PyGame for pixel-pushing and I plan on using OpenGL in the future):
import pygame
pygame.init()
cols = 200
rows = 200
window = pygame.display.set_mode((cols, rows))
buffer1 = [[0 for _ in range(rows)] for _ in range(cols)]
buffer2 = buffer1.copy()
buffer1[100][100] = 255 #Raindrop
dampening = 0.97
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
for i in range(1, cols - 1):
for j in range(1, rows - 1):
buffer2[i][j] = ((buffer1[i+1][j] + buffer1[i-1][j] + buffer1[i][j+1] + buffer1[i][j-1]) / 2) - buffer2[i][j]
buffer2[i][j] = buffer2[i][j] * dampening
#Displaying the buffer
for i in range(cols):
for j in range(rows):
window.fill((buffer2[i][j], buffer2[i][j], buffer2[i][j]), (i, j, 1, 1))
#Swapping the buffers
buffer1, buffer2 = buffer2, buffer1
pygame.display.update()
pygame.display.quit()
pygame.quit()
I've been struggling with this for a while now and I'm now finally turning to you for help. Thanks in advance.