Today I added the finishing touches onto my map generation software.
Here’s a list of the changes I made:
- Added a luck value to determine special biomes
- ice age
- desert
- alien planet
- Made the map randomly choose a season
- Added a size option at runtime
- Made the whole program loop indefinitely for continual map generation
The program now accomplishes what I had in mind at the start of the project.
Here is an example of a few outputs of differing sizes:
And here’s the final code (2 parts):
from GridMapGen import Grid
import Perlin
from PIL import Image, ImageDraw
import os
from GridMapGen import HeatMapGenerator
import random
def copy_image(img_1, img_2, width, height):
for x in range(width):
for y in range(height):
got_pixel = img_1.getpixel((x, y))
img_2.putpixel((x, y), (got_pixel[0], got_pixel[1], got_pixel[2]))
def main(image_index):
luck = random.randint(0, 1000)
imgx = int(input("Enter width"))
imgy = int(input("Enter height"))
path = os.path.dirname(os.path.abspath(__file__))
HeatMapGenerator.create_map(imgx, imgy)
Perlin.generate(imgx, imgy)
perlin_map = Image.open(path+'/PerlinNoise.png', mode='r')
temp_heat_map = Image.open(path+'/HeatMap.png', mode='r')
heat_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))
copy_image(temp_heat_map, heat_map, imgx, imgy)
final_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))
humidity_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))
copy_image(perlin_map, humidity_map, imgx, imgy)
Perlin.generate(imgx, imgy)
perlin_map = Image.open(path+'/PerlinNoise.png', mode='r')
height_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))
copy_image(perlin_map, height_map, imgx, imgy)
grid_size = (1, 1)
season = random.randint(0, 3)
for x in range(int(imgx/grid_size[0])):
for y in range(int(imgy/grid_size[1])):
test_grid = Grid.Grid(grid_size[0], grid_size[1], x*grid_size[0], y*grid_size[1], season+1, luck)
test_grid.assign_temperature(heat_map)
test_grid.assign_altitude(height_map)
test_grid.assign_humidity(humidity_map)
test_grid.draw(final_map)
final_map.show()
final_map.save(path+'/MapOutput' + str(image_index) + '.png')
if __name__ == '__main__':
for i in range(4):
main(i)
from PIL import Image, ImageDraw
class Grid:
humidity = 0
altitude = 0
temperature = 0
grid_type = 0
x_position = 0
y_position = 0
width = 0
height = 0
season = 1
luck = 0
def assign_humidity(self, image):
humidity_counter = 0
for x in range(self.width):
for y in range(self.height):
scan_x = x + self.x_position
scan_y = y + self.y_position
pixel_value = image.getpixel((scan_x, scan_y))[0]
humidity_counter += pixel_value
humidity_counter /= (self.width * self.height)
self.humidity = int(humidity_counter)
def assign_altitude(self, image):
altitude_counter = 0
for x in range(self.width):
for y in range(self.height):
scan_x = x + self.x_position
scan_y = y + self.y_position
pixel_value = image.getpixel((scan_x, scan_y))[0]
altitude_counter += pixel_value
altitude_counter /= (self.width * self.height)
self.altitude = int(altitude_counter)
def assign_temperature(self, image):
temperature_counter = 0
for x in range(self.width):
for y in range(self.height):
scan_x = x + self.x_position
scan_y = y + self.y_position
pixel_value = image.getpixel((scan_x, scan_y))[0]
temperature_counter += pixel_value
temperature_counter /= (self.width * self.height)
self.temperature = int(temperature_counter)
def __init__(self, width, height, x_pos, y_pos, season, luck):
self.width = width
self.height = height
self.x_position = x_pos
self.y_position = y_pos
self.season = season
self.luck = luck
def draw(self, image):
_deep_ocean = (0, 0, 120)
_shallows = (54, 56, 209)
_ice = (186, 224, 246)
_ocean = (35, 35, 185)
_snow = (255, 255, 255)
_sand = (219, 205, 116)
_plains = (195, 229, 117)
_swamp = (91, 101, 2)
_grass = (29, 186, 81)
_desert = (255, 229, 129)
_rock = (159, 156, 142)
_cold_sand = (162, 201, 163)
_wet_sand = (118, 141, 83)
_tundra = (131, 134, 102)
_boreal = (25, 125, 75)
_depths = (0, 0, 50)
_dirty_water = (64, 110, 141)
_icy_depths = (191, 199, 236)
if 150 < self.luck < 250 and self.season == 3:
self.season = 1
elif 150 < self.luck < 250 and self.season == 4:
self.season = 2
if self.season == 1:
self.temperature += 10
elif self.season == 2:
self.temperature += 50
self.altitude += 5
self.humidity -= 10
_snow = (166, 161, 150)
elif self.season == 3:
self.temperature -= 5
_plains = (230, 171, 68)
_grass = (181, 213, 107)
elif self.season == 4:
self.temperature -= 35
self.humidity += 10
_grass = (241, 255, 236)
_boreal = (180, 232, 204)
_shallows = _ice
_desert = _snow
_plains = _grass
_dirty_water = (218, 225, 196)
if 100 < self.luck < 150:
self.temperature -= 40
self.humidity += 25
if self.luck < 100:
self.temperature += 75
self.humidity -= 40
self.altitude += 12
if 150 < self.luck < 250:
self.humidity += 50
self.temperature += 20
self.altitude -= 2
_grass = _boreal
_rock = (101, 76, 25)
if self.luck > 900:
_grass = (198, 190, 89)
_rock = (70, 69, 57)
_ocean = (70, 69, 92)
_shallows = (86, 85, 118)
_deep_ocean = (34, 33, 58)
_snow = (240, 211, 246)
if self.luck > 990:
_grass = (199, 177, 144)
_ocean = (168, 199, 94)
_deep_ocean = (116, 134, 75)
_shallows = (179, 233, 52)
_rock = (119, 122, 173)
_snow = (255, 255, 255)
_desert = (105, 191, 142)
_plains = (119, 152, 133)
_swamp = (211, 216, 68)
_boreal = (22, 233, 29)
if self.altitude <= 60 and self.temperature < 50:
self.grid_type = _icy_depths
if self.altitude <= 60:
self.grid_type = _depths
elif self.altitude <= 95 and self.temperature < 50:
self.grid_type = _snow
elif self.altitude < 119 and self.temperature < 50:
self.grid_type = _ice
elif self.altitude <= 95:
self.grid_type = _deep_ocean
elif self.altitude <= 110:
self.grid_type = _ocean
elif self.altitude < 119:
self.grid_type = _shallows
elif self.altitude == 120 or self.altitude == 119 and 150 > self.temperature > 100:
self.grid_type = _sand
elif self.altitude == 120 or self.altitude == 119 and self.temperature <= 100:
self.grid_type = _cold_sand
elif self.altitude == 120 or self.altitude == 119 and self.humidity > 100:
self.grid_type = _wet_sand
elif self.altitude == 120 or self.altitude == 119:
self.grid_type = _sand
elif 151 > self.altitude > 120 and self.temperature > 125 and self.humidity < 95:
self.grid_type = _desert
elif 130 > self.altitude > 120 and self.temperature > 100 and self.humidity > 160:
self.grid_type = _swamp
elif 141 > self.altitude > 120 and self.temperature > 100 and self.humidity > 160:
self.grid_type = _dirty_water
elif self.altitude > 175 and self.temperature < 175:
self.grid_type = _snow
elif 175 >= self.altitude >= 150:
self.grid_type = _rock
elif 140 > self.altitude > 130 and self.humidity < 130 and self.temperature > 100:
self.grid_type = _plains
elif 150 > self.altitude > 145 and self.temperature < 100:
self.grid_type = _tundra
elif 145 >= self.altitude >= 140 and self.temperature < 105:
self.grid_type = _boreal
elif self.altitude > 120:
self.grid_type = _grass
for x in range(self.width):
for y in range(self.height):
scan_x = x + self.x_position
scan_y = y + self.y_position
image.putpixel((scan_x, scan_y), self.grid_type)
def main():
grid = Grid(0, 0, 0, 0)
if __name__ == '__main__':
main()