Skip to content

Instantly share code, notes, and snippets.

@delijati
Created March 27, 2019 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delijati/8a425dcd811048910626720ff51ac36f to your computer and use it in GitHub Desktop.
Save delijati/8a425dcd811048910626720ff51ac36f to your computer and use it in GitHub Desktop.
fluidsim
# air on higher value squares will try to flow to lower value squares
# a square can only hold a certain amount of air ( Depends on square value
# ). If square is full, air will not flow into it.
import sys
import pygame
import random
from noise import pnoise2
noise_X = random.randint(-1000, 1000)
noise_Y = random.randint(-1000, 1000)
# Modifiers. Chage these to chage window size, zoom, and particle size
noise_zoom = 0.05
size = 7
pygame.init()
info = pygame.display.Info()
screen_width, screen_height = info.current_w, info.current_h
window_width, window_height = screen_width - 10, screen_height - 50
mapsize = window_height
screen = pygame.display.set_mode(
(window_width, window_height), pygame.FULLSCREEN)
# screen = pygame.display.set_mode(
# (mapsize * size, mapsize * size), pygame.FULLSCREEN)
air = []
heightmap = []
def update_air_index(i, j):
if i == 0 or i == (mapsize - 1) or j == 0 or j == (mapsize - 1):
d1 = [255, 255, 255, 255]
else:
d1 = [heightmap[i][j - 1], heightmap[i][j + 1],
heightmap[i - 1][j], heightmap[i + 1][j]]
target = min(d1)
target_location = d1.index(min(d1))
if target < 14:
target_location = random.randint(0, 3)
if air[i][j] >= 1:
if heightmap[i][j] >= target:
if air[i - 1][j] <= 4:
if target_location == 2:
air[i][j] -= 1
air[i - 1][j] += 1
if air[i + 1][j] <= 4:
if target_location == 3:
air[i][j] -= 1
air[i + 1][j] += 1
if air[i][j - 1] <= 4:
if target_location == 0:
air[i][j] -= 1
air[i][j - 1] += 1
if air[i][j + 1] <= 4:
if target_location == 1:
air[i][j] -= 1
air[i][j + 1] += 1
def update_air():
for i in range(mapsize):
for j in range(mapsize):
update_air_index(i, j)
def generate_map():
for i in range(mapsize):
air.append([])
heightmap.append([])
for j in range(mapsize):
height = pnoise2(
(i + (noise_X)) * noise_zoom,
(j + (noise_Y)) * noise_zoom) * 10
height += 15
heightmap[i].append(abs(height))
air[i].append(random.randint(0, 5))
def render():
for i in range(mapsize):
for j in range(mapsize):
aircolor = air[j][i] * 25
heightcolor = int(heightmap[j][i] * 3)
if aircolor > 255:
aircolor = 255
pygame.draw.rect(
screen, (heightcolor, heightcolor, aircolor), (j * size, i * size, size, size))
def run():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c and pygame.key.get_mods() & pygame.KMOD_CTRL:
print("pressed CTRL-C as an event")
sys.exit(0)
generate_map()
while True:
update_air()
render()
run()
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment