Game Development Reference
In-Depth Information
111 window = GameWindow . new
112 window . show
Run the program, zoom with up / down arrows and regenerate everything with spacebar.
$ ruby 02-warmup/perlin_noise_map.rb
Map generated with Perlin noise
This is a little longer than our previous examples, so we will analyze some parts to make it
clear.
81 def generate_map
82 noises = Perlin :: Noise . new( 2 )
83 contrast = Perlin :: Curve . contrast(
84 Perlin :: Curve :: CUBIC , 2 )
85 map = {}
86 MAP_WIDTH . times do | x |
87 map [ x ] = {}
88 MAP_HEIGHT . times do | y |
89 n = noises [ x * 0.1 , y * 0.1]
90 n = contrast . call(n)
91 map [ x ][ y ] = choose_tile(n)
92 end
93 end
94 map
95 end
generate_map is the heart of this program. It creates two dimensional
Perlin::Noise generator, then chooses a random tile for each location of the map,
according to noise value. To make the map a little sharper, cubic contrast is applied to noise
value before choosing the tile. Try commenting out contrast application - it will look like a
boring golf course, since noise values will keep buzzing around the middle.
Search WWH ::




Custom Search