我尝试修复代码,但无法修复。颜色不显示。它以前有效,但当我尝试添加生物群落时,它删除了颜色,但不知道为什么。有谁知道如何修理它?它基本上是一个 2D 地图生成器,可以无限生成不同的生物群落和陆地类型,它应该显示海拔和事物。
`
import pygame
import random
import noise
# Initialize Pygame
pygame.init()
# Screen settings
screen_width = 900
screen_height = 600 # Adjusted screen height
screen = pygame.display.set_mode((screen_width, screen_height)) # Set screen size
pygame.display.set_caption("Infinite Scrollable World")
# Color palette
colors = {
"land": (60, 140, 40), # Lighter land color
"water": (30, 50, 170),
"desert": (230, 220, 170), # Desert color
"mountains": (128, 128, 128), # Mountains color
"snow": (255, 255, 255), # Snow color
"forest": (34, 139, 34), # Forest color
"swamp": (139, 69, 19), # Swamp color
"beach": (255, 255, 153), # Beach color
"tundra": (255, 255, 255) # Tundra color
}
# Biome definitions
biomes = {
"water": {"elevation": (0, 30), "color": colors["water"]},
"beach": {"elevation": (31, 60), "color": colors["beach"]},
"desert": {"temperature": (0.4, 1.0), "moisture": (0.0, 0.3), "elevation": (61, 150), "color": colors["desert"]},
"grassland": {"temperature": (0.2, 0.8), "moisture": (0.2, 0.6), "elevation": (61, 150), "color": colors["land"]},
"forest": {"temperature": (0.0, 0.7), "moisture": (0.5, 1.0), "elevation": (61, 150), "color": colors["forest"]},
"mountains": {"elevation": (151, 200), "color": colors["mountains"]},
"snow": {"temperature": (-1.0, 0.0), "moisture": (0.5, 1.0), "elevation": (201, 255), "color": colors["snow"]},
"swamp": {"temperature": (0.0, 0.7), "moisture": (0.8, 1.0), "elevation": (61, 150), "color": colors["swamp"]},
"tundra": {"temperature": (-1.0, 0.5), "moisture": (0.3, 0.6), "elevation": (151, 255), "color": colors["tundra"]}
}
# Print elevation ranges for each biome
for biome_name, info in biomes.items():
print(f"{biome_name}: {info['elevation']}")
# 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
noise1 = noise.snoise2(x_ * 0.05, y_ * 0.05, octaves=4) # Sand noise
noise2 = noise.snoise2(x_ * 0.02, y_ * 0.02, octaves=8) # Grass noise
noise3 = noise.snoise2(x_ * 0.01, y_ * 0.01, octaves=6) # Mountains noise
noise4 = noise.snoise2(x_ * 0.005, y_ * 0.005, octaves=2) # Snow noise
elevation = int((noise1 + noise2 + noise3) * 128) # Combine and map to 0-255
# Assign biome based on elevation
color = (0, 0, 0) # Default color
for biome_name, info in biomes.items():
if elevation >= info.get("elevation", (0, 0))[0] and elevation <= info.get("elevation", (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
# 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
# Game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 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()
不同的生物群系具有不同的颜色和海拔。
快速阅读您的代码会发现
generate_world
中出现一些奇怪的事情:
noise4
未使用(我假设它进入 elevation
公式) - 这可能会对地图上实际显示的海拔产生很小的影响;biome_name
未使用(那个并不重要,也许你只是想要它,这样你就可以使用 print
进行调试 - 否则你可以使用 for info in biomes.values():
);elevation
是唯一的变量,因此屏幕上不应有超过 5 个生物群落(以五个海拔范围中第一个出现的生物群落为准)。除此之外似乎还不错。我运行了你的代码(只需用
noise
替换 perlin_noise
模块,因为 noise
不会安装)并得到这个:
如果这不是您所期望的,也许您可以更新问题?