So I am making a moon lander game that procedurally generates a landscape. Originally I wanted to integrate landing platforms directly in the landscape but I dont really know how. Thats why I tried integrating platform.png images, that are placed randomly (im generating a random x value). Unfortunately im having trouble placing the platforms at the right y-value. This is my level class:

class Level:
    def __init__(self, octaves, freq, amp, seed):
        self.octaves = octaves
        self.freq = freq
        self.amp = amp
        self.seed = seed
        self.landscape = []
        self.noise = OpenSimplex(seed=self.seed)

    def generate_landscape(self, width, height):
        """
        Procedurally generates a landscape
        based on the perlin noise algorithm
        """
        for x in range(width):
            noise_value = 0
            for o in range(self.octaves):
                oct_scale = self.freq * (2 ** o)
                oct_amp = self.amp * (0.5 ** o)
                noise_value += self.noise.noise2(x * oct_scale, 0) * oct_amp

            self.landscape.append(height / 2 + int(noise_value))

I also created a platform class:

class Platform(pygame.sprite.Sprite):
    def __init__(self, image_path, scale):
        super().__init__()
        self.scale = scale
        self.image = pygame.image.load(image_path).convert_alpha()
        self.rect = self.image.get_rect()
        self.image = pygame.transform.scale(self.image, (int(self.image.get_width() * scale), int(self.image.get_height() * scale)))
        
    def generate_position(self, landscape, screen_width):
        """
        Generates a position for the platform based on the landscape heights
        """
        platform_width = self.rect.width
        x_coordinate = random.randint(0, screen_width - platform_width)
        self.rect.x = x_coordinate
        self.rect.y = landscape[x_coordinate] - self.rect.height

    def draw(self, screen):
        screen.blit(self.image, self.rect)

so that I can use an instance for every platform I want to draw, which doesnt seem like the best solution. Now are there any idea how I can achieve what I want or even an easy way to integrate platforms in the procedural generation? Thank you in advance

0

There are 0 best solutions below