I am experimenting with Perlin Noise and random map generation. I have a 2D numpy ndarray full of 16-bit floats called map_list
that I call from the singleton ST
. It has 900 rows with 1600 elements each. I am iterating through it to display different colored pixels to represent terrain at different points in the map. Array values in different ranges produce pixels of different colors. The total possible range is [0, 1] because I am normalizing the values.
Is there a way to display the aforementioned pixels faster than I am capable of now?
"""
This file holds functions that modify pyGame surfaces.
"""
from __future__ import division
from singleton import ST
from pygame import gfxdraw
def display_map(surface):
"""
This takes in a pyGame surface, and draws colored tiles on it according to
the values in ST.map_list. The higher the value, the lighter the shade of
the tile.
:param surface: A pyGame surface.
"""
x_pos = 0
y_pos = 0
for y in range(len(ST.map_list)):
for x in range(len(ST.map_list[y])):
noise_value = ST.map_list[y][x]
shade = int(noise_value * 255)
color = __color_tiles(noise_value, shade)
gfxdraw.pixel(surface, x_pos, y_pos, color)
x_pos += ST.TILE_SIZE
x_pos = 0
y_pos += ST.TILE_SIZE
def __color_tiles(noise_value, shade):
"""
Treat this function as private. It should only be called by functions and
methods within this file. It returns a 3-element 1D tuple that represents
the rgb color values to display the tile as.
:param noise_value: The noise value at a specific point in ST.map_list.
:param shade: How dark or light the tile should be.
:return: tuple
"""
if noise_value < ST.WATER_LEVEL:
rgb = (shade, shade, 255)
elif noise_value > ST.MOUNTAIN_LEVEL:
rgb = (shade, shade, shade)
else:
rgb = (shade, 255, shade)
return rgb
What it generates