Sie sind auf Seite 1von 4

Creating a random 2d game world map

gillesleblanc.wordpress.com /2012/10/16/creating-a-random-2d-game-world-map/

Gilles Leblanc 17/10/2012

I wanted to create a program that generates a random 2d world map. Like one you would find in old rpg video
games. This got me to investigate the wonderful world of procedural content generation.

The word random is used here to mean that the map will be created using a certain amount of randomness so as to
get a different map every time we generate one.

Using totally random data would just create a bunch of noise. I generated a map using totally random data to show
you what I mean.

I looked into how to create random 2d worlds. My research on Google led me to pseudo-random number generation
and Perlin noise. I quickly realized this wasnt the path I wanted to investigate.

1/4
I wanted to share with you the solution I adopted. Its easy to code and powerful. I ended up generating a
heightmap. These are primarily used to create 3d worlds, but when you think about it, its even easier to use them
for a 2d world by applying some sort of filter on the height values of the map to convert them to 2d tiles. Here is
some code that uses an already generated heightmap, of exactly the same kind you would use for 3d graphics, and
turns it in a 2d world map.

2/4
1 require'../map'
2 require'../tile'
3 require'./height_map'
4
5 WorldMap <
6 class Map
7 initialize(width, height,
8 def height_map)
9 (width,
10 superheight)
11
12
13 (0...width).each do |x|
14 (0...height).each do |y|
15
16 edge_of_map?(x,
17 if y)
18 terrain
19 = :water
20 height_map[x + y * width]
21 elsif == 0
22 terrain
23 = :water
24 height_map[x + y * width]
25
elsif >= 70
26
27 terrain
28 = :mountain
29 height_map[x + y * width]
30 elsif >= 1
&& height_map[x + y * width]
<= 2
terrain
= :sand
height_map[x + y * width]
elsif >= 30
&& height_map[x + y * width]
<= 60
terrain
= :forest
else
terrain
= :grass
end

[x + y * width] = (terrain, x,
@tilesTile. newy)
end
end
end
end

This will allow us to generate something like this:

3/4
Bear in mind that these are preliminary results. I still have much work to do on the height map generator to get the
kind of results I am looking for.

Generating a heightmap is actually pretty simple. You can look up my two previous posts on the subject for more
information.

Generating heightmaps using particle deposition


Using Gaussian blurring on heightmaps

UPDATE: I published a part 2 to this article: Generating Lake and Rivers.


UPDATE: I published a part 3 to this article: Cities, caves and snow.

4/4

Das könnte Ihnen auch gefallen