Write simple procedural game Minecraft-like world generation. It generates 2d terrain…
- User
Write simple procedural game Minecraft-like world generation. It generates 2d terrain image and saves it to PNG file Usage: gen.py
- Assistant
Write simple procedural game Minecraft-like world generation. It generates 2d terrain image and saves it to PNG file Usage: gen.py
To generate a 2D terrain image similar to Minecraft's world generation, we can use a combination of noise functions to create an interesting landscape. One popular choice for terrain generation is Perlin noise, which can be generated using libraries such as noise or numpy. Additionally, we'll use the Pillow library to create and save the image.
Below is a Python script that generates a simple 2D terrain image based on the input parameters you provided. The script utilizes the noise library for generating Perlin noise and Pillow for handling image creation and saving.
First, make sure you have the required libraries installed. You can install them using pip:
pip install noise Pillow
Now, here is the gen.py script:
import sys
import numpy as np
from noise import snoise2
from PIL import Image
def generate_terrain(width, height, seed, x_offset, y_offset):
terrain = np.zeros((height, width, 3), dtype=np.uint8)
for y in range(height):
for x in range(width):
# Generate noise value
n = snoise2((x + x_offset) / 100.0, (y + y_offset) / 100.0, octaves=6, persistence=0.5, lacunarity=2.0, repeatx=1024, repeaty=1024, base=seed)
# Map -1 to 1 range to 0 to 255
color_value = int((n + 1) / 2 * 255)
terrain[y, x] = [color_value, color_value, color_value] # Grayscale
# Optionally add some color based on elevation
if n > 0.1: # Sunlit terrain
terrain[y, x] = [34, 139, 34] # Green for land
elif n < -0.1: # Water
terrain[y, x] = [70, 130, 180] # Blue for water
return terrain
def save_image(terrain, output_file):
image = Image.fromarray(terrain, 'RGB')
image.save(output_file)
print(f"Image saved to {output_file}")
def main():
if len(sys.argv) != 6:
print("Usage: gen.py <image width> <image height> <world seed> <x start coordinate> <y start coordinate> <png output image>")
return
width = int(sys.argv[1])
height = int(sys.argv[2])
seed = int(sys.argv[3])
x_start = int(sys.argv[4])
y_start = int(sys.argv[5])
output_file = sys.argv[6]
terrain = generate_terrain(width, height, seed, x_start, y_start)
save_image(terrain, output_file)
if __name__ == "__main__":
main()
gen.py.gen.py is located.python gen.py 800 600 42 0 0 output.png
This will generate a terrain image of size 800x600 pixels using the seed 42, starting at coordinates (0, 0), and save the image as output.png.
<image width>: The width of the generated image.<image height>: The height of the generated image.<world seed>: A seed value that controls the randomness of the terrain generation.<x start coordinate>: The starting x-coordinate for the noise generation.<y start coordinate>: The starting y-coordinate for the noise generation.<png output image>: The name of the output PNG file where the generated image will be saved.This script provides a very basic implementation of Minecraft-like terrain generation using noise functions, and you can expand upon it by adding more features and complexity as needed!
Not yet included in a published release.