海洋层无法正常工作且太迟缓

问题描述 投票:0回答:1

感谢您的帮助,但现在海洋无法正确生成。我尝试让海洋成为自己的层,但它覆盖了岛屿(摧毁了它们),我感谢你们帮助解决我遇到的颜色生物群系问题。它也很滞后,我不知道如何优化代码。

(新代码)

import pygame
import random
import noise

# Initialize Pygame
pygame.init()

# Screen settings
screen_width = 900
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Infinite Scrollable World")

# Color palette
colors = {
    "land": (134, 168, 83),   # Light green
    "water": (30, 144, 255),  # Deep blue
    "desert": (244, 164, 96), # Sandy color
    "mountains": (139, 137, 137),  # Gray
    "snow": (255, 250, 250),  # Snow white
    "forest": (34, 139, 34),  # Forest green
    "swamp": (105, 139, 34),  # Swampy green
    "beach": (238, 214, 175),  # Light beige
    "tundra": (192, 192, 192)  # Light gray
}

# Biome definitions
biomes = {
    "water": {"elevation": (0, 70), "temperature": (0.0, 1.0), "moisture": (0.0, 1.0), "color": colors["water"]},
    "beach": {"elevation": (71, 100), "temperature": (0.2, 0.8), "moisture": (0.2, 0.8), "color": colors["beach"]},
    "desert": {"temperature": (0.4, 1.0), "moisture": (0.0, 0.3), "elevation": (101, 200), "color": colors["desert"]},
    "grassland": {"temperature": (0.2, 0.8), "moisture": (0.2, 0.6), "elevation": (101, 200), "color": colors["land"]},
    "forest": {"temperature": (0.0, 0.7), "moisture": (0.5, 1.0), "elevation": (101, 200), "color": colors["forest"]},
    "mountains": {"elevation": (201, 255), "temperature": (0.0, 0.5), "moisture": (0.2, 0.8), "color": colors["mountains"]},
    "snow": {"temperature": (-1.0, 0.0), "moisture": (0.5, 1.0), "elevation": (256, 300), "color": colors["snow"]},
    "swamp": {"temperature": (0.0, 0.7), "moisture": (0.8, 1.0), "elevation": (101, 200), "color": colors["swamp"]},
    "tundra": {"temperature": (-1.0, 0.5), "moisture": (0.3, 0.6), "elevation": (201, 255), "color": colors["tundra"]}
}

# Infinite world generator (using modulo for continuous access)
def generate_world(x, y, size):
    world = []
    for y_ in range(y, y + size):
        row = []
        for x_ in range(x, x + size):
            # Combine multiple Perlin noise layers
            noise_ocean = noise.snoise2(x_ * 0.005, y_ * 0.005, octaves=4)  # Ocean noise
            noise_elevation = noise.snoise2(x_ * 0.005, y_ * 0.005, octaves=4)  # Adjusted scale
            noise_temperature = noise.snoise2(x_ * 0.01, y_ * 0.01, octaves=4)
            noise_moisture = noise.snoise2(x_ * 0.015, y_ * 0.015, octaves=4)  # Adjusted scale

            ocean = int((noise_ocean + 1) * 128)  # Map ocean to 0-255
            elevation = int((noise_elevation + 1) * 128)  # Map elevation to 0-255
            temperature = min(max(noise_temperature, -1.0), 1.0)  # Limit temperature to range (-1.0, 1.0)
            moisture = min(max(noise_moisture, -1.0), 1.0)  # Limit moisture to range (-1.0, 1.0)

            # Assign biome based on elevation, temperature, and moisture
            color = (0, 0, 0)  # Default color
            if ocean < 90:  # If below water threshold, assign ocean color
                color = colors["water"]
            else:  # Otherwise, assign biome color
                for biome_name, info in biomes.items():
                    if elevation >= info.get("elevation", (0, 0))[0] and elevation <= info.get("elevation", (0, 0))[1] \
                            and temperature >= info.get("temperature", (0, 0))[0] and temperature <= info.get("temperature", (0, 0))[1] \
                            and moisture >= info.get("moisture", (0, 0))[0] and moisture <= info.get("moisture", (0, 0))[1]:
                        color = info["color"]  # Assign color within the matching biome condition
                        break

            row.append((elevation, color))  # Referencing 'color' after assignment
        world.append(row)
    return world

# Function to generate a new random seed for world generation
def generate_new_seed():
    return random.randint(0, 1000000)

# Player movement variables
player_x = screen_width // 2
player_y = screen_height // 2
scroll_speed = 5

# World generation parameters
chunk_size = 100  # Adjustable chunk size preference
world_offset = {
    "x": player_x // chunk_size,  # Initial world offset adjusted based on player position
    "y": player_y // chunk_size
}

# Perlin noise parameters for island generation
scale = 0.005
octaves = 4

# Set initial random seed for world generation
random_seed = generate_new_seed()
random.seed(random_seed)

# Game loop
running = True
clock = pygame.time.Clock()

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_n:  # Press "N" key to generate a new world
                random_seed = generate_new_seed()
                random.seed(random_seed)
                print(f"Generating new world with seed: {random_seed}")

    # Player movement and world offset update
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        player_x -= scroll_speed
        world_offset["x"] += scroll_speed // chunk_size  # Update world offset for scrolling
    if keys[pygame.K_RIGHT]:
        player_x += scroll_speed
        world_offset["x"] -= scroll_speed // chunk_size
    if keys[pygame.K_UP]:
        player_y -= scroll_speed
        world_offset["y"] += scroll_speed // chunk_size
    if keys[pygame.K_DOWN]:
        player_y += scroll_speed
        world_offset["y"] -= scroll_speed // chunk_size

    # Generate world chunk around player
    chunk_x = (player_x - screen_width // 2 // chunk_size)
    chunk_y = (player_y - screen_height // 2 // chunk_size)
    world_chunk = generate_world(chunk_x, chunk_y, chunk_size)

    # Clear screen and draw world chunk
    screen.fill((0, 0, 0))  # Clear black background
    for y in range(len(world_chunk)):
        for x in range(len(world_chunk[0])):
            elevation, color = world_chunk[y][x]  # Unpack elevation and color tuple
            rect = pygame.Rect(x * 10, y * 10, 10, 10)
            pygame.draw.rect(screen, color, rect)  # Use color from tuple

    # Update display
    pygame.display.flip()
    pygame.display.update()
    clock.tick(60)

pygame.quit()

不同的生物群系具有不同的颜色和海拔。

python pygame perlin-noise
1个回答
1
投票

快速阅读您的代码会发现

generate_world
中出现一些奇怪的事情:

  • noise4
    未使用(我假设它进入
    elevation
    公式) - 这可能会对地图上实际显示的海拔产生很小的影响;
  • biome_name
    未使用(那个并不重要,也许你只是想要它,这样你就可以使用
    print
    进行调试 - 否则你可以使用
    for info in biomes.values():
    );
  • elevation
    是唯一的变量,因此屏幕上不应有超过 5 个生物群落(以五个海拔范围中第一个出现的生物群落为准)。

除此之外似乎还不错。我运行了你的代码(只需用

noise
替换
perlin_noise
模块,因为
noise
不会安装)并得到这个:

如果这不是您所期望的,也许您可以更新问题?

© www.soinside.com 2019 - 2024. All rights reserved.