Advertisement

2D Random Map Generation

Started by March 10, 2018 08:38 PM
8 comments, last by Alberth 6 years, 9 months ago

I want to make a random map generator in python 3, but I have no idea how to do it. Could someone recommend me a good starting point?

What I want to make is a flat 2D overworld-map with roads, rivers, settlements, farmlands, forests, and plains.

I don't want to model specific buildings. This is just for a game I'm making where I'd like the player to be able to travel from point to point.

I'm assuming you didn't google "How to generate a random 2D map" in google, there are a ton of resources:

https://gamedev.stackexchange.com/questions/79049/generating-tile-map

https://stackoverflow.com/questions/11968167/random-2d-tile-map-generating-algorithm

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

And part 2 adds rivers:

https://gillesleblanc.wordpress.com/2013/01/09/creating-a-random-2d-game-world-map-part-2-adding-rivers-and-lakes/

Adapting any code in another language should be trivial at best, it's all rather simplistic for 2D.  There are edge cases but that should get you started.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Advertisement

I DID google a bunch of similar terms, but either came up with non-programming examples, or examples for 3D.

Thank you, but I should've been more specific. I really need a guide, not just bits and pieces about generating random maps. Once I've got a good foundation I feel like I can start exploring different ways to do the same thing. Otherwise, I'll be out of my depth. I'm still a very new programmer.

Maybe you need a guide about your project rather than about random map generation in general:

  • A game engine and a set of rules that make as few assumptions as possible about maps
  • Adequate map data structures (I'll assume a tile-based map)
  • With data structures enabling them, map operations beyond simple access to tiles: changing individual tiles, cropping a map, pasting tiles from a map into another map, finding tiles with certain attributes, distance between two sets of tile locations, loading a map from a file...
  • With a reliable and general toolbox, progressively more sophisticated experiments in random generation conservatively replacing fixed maps.
    For example if you want a city, a mountain and a swamp in random places you can load them from their respective small map files and paste each in a random location within predefined regions of a large map that has been initialized to grass or some other default terrain.
  • Only if it seems really useful, features that serve specifically random map generation, such as the metadata to treat map tiles as a system of Wang tiles or corner tiles in order to manage small structures automatically when individual tiles are picked at random.

Omae Wa Mou Shindeiru

Have you considered other languages besides just python? C# has a lot of good examples of tile maps. My preference is for list over array though for easier saving/storage.

On 3/10/2018 at 10:38 PM, RidiculousName said:

Could someone recommend me a good starting point?

Start with a premade engine: Pygame, Renpy or Panda3D is what I recommend. Or you should start by learning how to use OpenGL with python.

Advertisement
17 hours ago, LorenzoGatti said:

Maybe you need a guide about your project rather than about random map generation in general

I could definitely use a guide about making a general game project. I'll try to follow your bullet-points. I'm trying to make everything as simple as possible while still providing a framework I can build off of.

3 hours ago, Shawn Naef said:

Have you considered other languages besides just python? C# has a lot of good examples of tile maps. My preference is for list over array though for easier saving/storage.

I only know Python and C++. I'd consider doing the project in C++, but I don't want to learn a new language, as I'm pretty busy ATM. I originally chose Python because it's easier to develop with, and I'm more familiar with it.

1 hour ago, Scouting Ninja said:

Start with a premade engine: Pygame, Renpy or Panda3D is what I recommend. Or you should start by learning how to use OpenGL with python.

Assuming I couldn't find a guide, I planned to use either Pygame or Kivy for graphics. I'm worried though, since Pygame is a bit too in-depth for what I need, and Kivy is hard to find good information for. I'll look into the other engines you mentioned. I don't really know anything about them.

I guess I am confused about what an engine is. I thought both Pygame and Kivy were just graphical libraries.

1 hour ago, RidiculousName said:

I guess I am confused about what an engine is. I thought both Pygame and Kivy were just graphical libraries.

An engine is basically a set of tools (libraries, files, etc) that are just a collection of reusable code.  Sometimes well thought out and organized in a coherent manner, sometimes just ripped from past projects and slammed into the current one.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

On 3/10/2018 at 9:38 PM, RidiculousName said:

What I want to make is a flat 2D overworld-map with roads, rivers, settlements, farmlands, forests, and plains.

The very first thing that you need is the ability to have a map (any map) in your game. You can generate a nice map in some way, but if you cannot store it in the game, you can't display it.

So, first work on displaying a map in your game. If you use tiles, just use a random value for each tile. It will look horrible, but it should display the map.

 

Next, you likely want to save a generated map and load it back to play further, so loading a map from file could be a next step. If you make a map-file outside the program and load it, you then already have a map, so you can work on the game itself if you want (rather than working on making new maps).

If you add saving a map from the game, and you add a way to change tiles in the game as well, you have a map editor!

 

To start working on generating a map, a simple approach is to start with all land, and add random rivers in them, add settlements, add roads, and so on. You may want to scale down the number of features here, making the problem smaller so it's easier to make progress.

This topic is closed to new replies.

Advertisement