simulate The wind blows the sand on the beach and changes the shape of the beach

To simulate the wind blowing the sand on the beach and changing the shape of the beach using Python 3, we can create a program that uses Perlin noise to generate a heightmap of the sand and simulates the wind’s effect on the heightmap.

Here’s an example program that uses the noise library to generate Perlin noise and pygame to display the heightmap as an image:

pythonCopy codeimport pygame
import noise

# Set up constants
WIDTH = 800
HEIGHT = 600
SCALE = 0.01  # Controls the size of the sand particles
OFFSET = (0, 0)  # Controls the starting position of the noise
WIND = (0.01, 0)  # Controls the direction and strength of the wind

# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Generate the heightmap using Perlin noise
def generate_heightmap(width, height, scale, offset):
    heightmap = []
    for y in range(height):
        row = []
        for x in range(width):
            value = noise.pnoise2((x + offset[0]) * scale, (y + offset[1]) * scale)
            row.append(value)
        heightmap.append(row)
    return heightmap

# Normalize the heightmap to the range 0-255 and convert it to a Pygame surface
def create_surface_from_heightmap(heightmap):
    surface = pygame.Surface((len(heightmap[0]), len(heightmap)))
    for y in range(len(heightmap)):
        for x in range(len(heightmap[y])):
            value = int((heightmap[y][x] + 1) * 127.5)
            color = pygame.Color(value, value, value)
            surface.set_at((x, y), color)
    return surface

# Simulate the wind's effect on the heightmap
def simulate_wind(heightmap, wind):
    new_heightmap = []
    for y in range(len(heightmap)):
        row = []
        for x in range(len(heightmap[y])):
            dx = wind[0] * (y - HEIGHT/2)
            dy = wind[1] * (x - WIDTH/2)
            x2 = int(x + dx)
            y2 = int(y + dy)
            if x2 >= 0 and x2 < WIDTH and y2 >= 0 and y2 < HEIGHT:
                row.append(heightmap[y2][x2])
            else:
                row.append(heightmap[y][x])
        new_heightmap.append(row)
    return new_heightmap

# Generate the initial heightmap
heightmap = generate_heightmap(WIDTH, HEIGHT, SCALE, OFFSET)

# Main loop
running = True
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Simulate the wind's effect on the heightmap
    heightmap = simulate_wind(heightmap, WIND)

    # Convert the heightmap to a Pygame surface and display it
    surface = create_surface_from_heightmap(heightmap)
    screen.blit(surface, (0, 0))
    pygame.display.flip()

# Quit Pygame
pygame.quit()

In this program, we use Perlin noise to generate a heightmap of the sand particles, which we then convert to a Pygame surface and display on the screen. We also simulate the effect of the wind on the heightmap by shifting the sand particles in the direction of the wind, and then updating the heightmap and displaying it again.

Leave a Comment