textured map
This commit is contained in:
parent
f67570eed4
commit
44ecb00701
6 changed files with 40 additions and 14 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
from noise import snoise2
|
|
||||||
import pydantic
|
import pydantic
|
||||||
import pygame
|
import pygame
|
||||||
|
from noise import snoise2
|
||||||
|
from PIL import Image, ImageFilter
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
@ -20,23 +21,27 @@ class Point(pydantic.BaseModel):
|
||||||
|
|
||||||
class Map:
|
class Map:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.resolution = 32
|
self.grass = pygame.image.load("grass.webp").convert_alpha()
|
||||||
self.scale = 0.1 # Determines the "smoothness" of the terrain
|
self.rock = pygame.image.load("rock.jpg").convert_alpha()
|
||||||
|
self.dirt = pygame.image.load("dirt.jpg").convert_alpha()
|
||||||
|
self.resolution = 16
|
||||||
|
self.scale = 0.14 # Determines the "smoothness" of the terrain
|
||||||
self.offset = Point(x=0, y=0)
|
self.offset = Point(x=0, y=0)
|
||||||
self.last_offset = self.offset
|
self.last_offset = self.offset
|
||||||
# self.width = int(screen_width / self.resolution)
|
# self.width = int(screen_width / self.resolution)
|
||||||
# self.height = int(screen_height / self.resolution)
|
# self.height = int(screen_height / self.resolution)
|
||||||
self.screen_width = 800
|
self.screen_width = 800
|
||||||
self.screen_height = 600
|
self.screen_height = 600
|
||||||
self.octaves = 6 # Number of layers of noise to combine
|
self.octaves = 2 # Number of layers of noise to combine
|
||||||
self.persistence = 0.5 # Amplitude of each octave
|
self.persistence = 0.05 # Amplitude of each octave
|
||||||
self.lacunarity = 2.0 # Frequency of each octave
|
self.lacunarity = 1.0 # Frequency of each octave
|
||||||
self.thresh = 128
|
self.thresh = 128
|
||||||
self.surf = pygame.Surface(
|
|
||||||
(self.screen_width / self.resolution, self.screen_height / self.resolution)
|
|
||||||
)
|
|
||||||
self.pre_draw()
|
self.pre_draw()
|
||||||
|
|
||||||
|
def refresh_surf(self):
|
||||||
|
self.surf = pygame.Surface((self.screen_width, self.screen_height))
|
||||||
|
# self.surf.blit(self.grass, (0, 0))
|
||||||
|
|
||||||
# self.noise_map = self.generate_noise_map()
|
# self.noise_map = self.generate_noise_map()
|
||||||
|
|
||||||
# def generate_noise_map(self):
|
# def generate_noise_map(self):
|
||||||
|
|
@ -66,14 +71,16 @@ class Map:
|
||||||
(0, 0),
|
(0, 0),
|
||||||
)
|
)
|
||||||
|
|
||||||
def point_check_collision(self, x, y):
|
def point_check_collision(self, x, y, thresh=None):
|
||||||
return self.get_noise(x / self.resolution, y / self.resolution) < self.thresh
|
return self.get_noise(x / self.resolution, y / self.resolution) < (
|
||||||
|
thresh or self.thresh
|
||||||
|
)
|
||||||
|
|
||||||
def pre_draw(self):
|
def pre_draw(self):
|
||||||
console.log("drawing")
|
console.log("drawing")
|
||||||
self.surf = pygame.Surface((self.screen_width, self.screen_height))
|
self.refresh_surf()
|
||||||
for x in range(self.screen_width):
|
for x in range(int(self.screen_width)):
|
||||||
for y in range(self.screen_height):
|
for y in range(int(self.screen_height)):
|
||||||
if not self.point_check_collision(x, y):
|
if not self.point_check_collision(x, y):
|
||||||
pygame.draw.rect(
|
pygame.draw.rect(
|
||||||
self.surf,
|
self.surf,
|
||||||
|
|
@ -88,3 +95,22 @@ class Map:
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
pygame.image.save(self.surf, "map.png")
|
pygame.image.save(self.surf, "map.png")
|
||||||
|
av1 = (
|
||||||
|
Image.open("rock.jpg")
|
||||||
|
.convert("RGB")
|
||||||
|
.resize((self.screen_width, self.screen_height))
|
||||||
|
)
|
||||||
|
av2 = (
|
||||||
|
Image.open("dirt.jpg")
|
||||||
|
.convert("RGB")
|
||||||
|
.resize((self.screen_width, self.screen_height))
|
||||||
|
)
|
||||||
|
mask = (
|
||||||
|
Image.open("map.png")
|
||||||
|
.convert("L")
|
||||||
|
.resize((self.screen_width, self.screen_height))
|
||||||
|
.filter(ImageFilter.GaussianBlur(3))
|
||||||
|
)
|
||||||
|
Image.composite(av2, av1, mask).save("result.png")
|
||||||
|
result = pygame.image.load("result.png")
|
||||||
|
self.surf.blit(result, (0, 0))
|
||||||
|
|
|
||||||
BIN
dirt.jpg
Normal file
BIN
dirt.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 MiB |
BIN
grass.webp
Normal file
BIN
grass.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 370 KiB |
BIN
map.png
BIN
map.png
Binary file not shown.
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 13 KiB |
BIN
result.png
Normal file
BIN
result.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
BIN
rock.jpg
Normal file
BIN
rock.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 433 KiB |
Loading…
Add table
Add a link
Reference in a new issue