Пример #1
0
    def __init__(self, width_factor, seed=time.time(), prnt=False):
        '''Initialize random variables and generate image'''

        if width_factor <= 0:
            print("Whoops! First paramater must be greater than zero")
            exit(-1)

        ##-=-=-=- Set Random Seed -=-=-=-=-=-=-=-=-=-##
        self.rand_seed = int(seed)
        random.seed(self.rand_seed)

        ##-=-=-=- Tweakable Values -=-=-=-=-=-=-=-=-=##
        self.num_ranges = 3  # Number of mountain ranges to be generated
        self.num_sines = 10  # Number of sin values generated for each mountain range
        self.image_height = 150  # Consider this the amount of "detail" in the mountains
        self.shadow_angle = 2.5  # The angle of the shadow on the mountains

        ##-=-=-=- Constant Values -=-=-=-=-=-=-=-=-=##
        self.constant = 30  # Overall amplitude of superpositioned waves
        self.k = 0.04  # Streching factor of mountains
        self.pnf = perlin.SimplexNoise()  # Initialize perlin noise creator
        self.sky_color = [30, 30, 40]  # Set Color of sky and mountains
        self.color = [
            random.randint(0, 150),
            random.randint(0, 150),
            random.randint(0, 150)
        ]
        self.moon = moon.Moon(15, self.rand_seed)

        ##-=-=-=- Programatically Set Values -=-=-=-##
        self.image_width = int(width_factor * self.image_height)
        self.data = list  # Stored variables of sine wave functions
        self.output = [[
            tuple(self.sky_color) for x in range(self.image_width)
        ] for y in range(self.image_height)]
        self.moon_location = random.randint(0, self.image_width - 10)

        ##-=-=-=- Creation -=-=-=-=-=-=-=-=-=-=-=-=-##
        self.pnf.randomize()  # Randomize perlin noise
        self.generate_ranges()  # Generate variables for sine functions
        self.create_moon(self.moon_location, 2)
        self.create_scene()  # Assign values to pixels
        self.show_image()
        if prnt:
            print(self)
#%%
import numpy as np
from noise import perlin as pn
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.gridspec as gridspec
import time
import math
from typing import NamedTuple

map_size = 256
np.random.seed(int(time.time()))

# Generate terrain
sn = pn.SimplexNoise()
terrain_map = np.empty((map_size, map_size))
for i in range(map_size):
    for j in range(map_size):
        terrain_map[i, j] = (1 + sn.noise2(x=j / 128, y=i / 128)) / 2 + \
                            (1 + sn.noise2(x=j / 64, y=i / 64)) / 6 + \
                            (1 + sn.noise2(x=j / 32, y=i / 32)) / 12 + \
                            (1 + sn.noise2(x=j / 16, y=i / 16)) / 32
terrain_map *= 64
terrain_map = np.transpose(terrain_map)


class HeightAndGradient(NamedTuple):
    height: float
    gradient_x: float
    gradient_y: float
Пример #3
0
import pygame
from noise import perlin
import math
import random

pygame.init()

noise = perlin.SimplexNoise()

seed = random.randint(0, 100000)
amplitude = 100

myfont = pygame.font.SysFont('Comic Sans MS', 30)

width = 600
height = 400

smallHeight = height / 4

RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
thickness = 4
screen = pygame.display.set_mode((width, height))


def getValue(x):
    return noise.noise2(x / float(200), seed)