diff --git a/cave_survival/map.py b/cave_survival/map.py index 541cf81..cd6a705 100644 --- a/cave_survival/map.py +++ b/cave_survival/map.py @@ -1,6 +1,7 @@ -from noise import snoise2 import pydantic import pygame +from noise import snoise2 +from PIL import Image, ImageFilter from rich.console import Console console = Console() @@ -20,23 +21,27 @@ class Point(pydantic.BaseModel): class Map: def __init__(self): - self.resolution = 32 - self.scale = 0.1 # Determines the "smoothness" of the terrain + self.grass = pygame.image.load("grass.webp").convert_alpha() + 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.last_offset = self.offset # self.width = int(screen_width / self.resolution) # self.height = int(screen_height / self.resolution) self.screen_width = 800 self.screen_height = 600 - self.octaves = 6 # Number of layers of noise to combine - self.persistence = 0.5 # Amplitude of each octave - self.lacunarity = 2.0 # Frequency of each octave + self.octaves = 2 # Number of layers of noise to combine + self.persistence = 0.05 # Amplitude of each octave + self.lacunarity = 1.0 # Frequency of each octave self.thresh = 128 - self.surf = pygame.Surface( - (self.screen_width / self.resolution, self.screen_height / self.resolution) - ) 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() # def generate_noise_map(self): @@ -66,14 +71,16 @@ class Map: (0, 0), ) - def point_check_collision(self, x, y): - return self.get_noise(x / self.resolution, y / self.resolution) < self.thresh + def point_check_collision(self, x, y, thresh=None): + return self.get_noise(x / self.resolution, y / self.resolution) < ( + thresh or self.thresh + ) def pre_draw(self): console.log("drawing") - self.surf = pygame.Surface((self.screen_width, self.screen_height)) - for x in range(self.screen_width): - for y in range(self.screen_height): + self.refresh_surf() + for x in range(int(self.screen_width)): + for y in range(int(self.screen_height)): if not self.point_check_collision(x, y): pygame.draw.rect( self.surf, @@ -88,3 +95,22 @@ class Map: ), ) 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)) diff --git a/dirt.jpg b/dirt.jpg new file mode 100644 index 0000000..8fddbc5 Binary files /dev/null and b/dirt.jpg differ diff --git a/grass.webp b/grass.webp new file mode 100644 index 0000000..f908b95 Binary files /dev/null and b/grass.webp differ diff --git a/map.png b/map.png index 86e6fa2..aad662e 100644 Binary files a/map.png and b/map.png differ diff --git a/result.png b/result.png new file mode 100644 index 0000000..e0a29b5 Binary files /dev/null and b/result.png differ diff --git a/rock.jpg b/rock.jpg new file mode 100644 index 0000000..04f20b1 Binary files /dev/null and b/rock.jpg differ