Computer Science Canada random map generator |
Author: | Raknarg [ Wed Dec 18, 2013 2:13 pm ] | ||
Post subject: | random map generator | ||
I know it's not anything new, but I'm pretty proud of it as I've been trying to figure this out for a very long time. A while ago I looked up the square diamond algorithm for map generation, and it made sense but I could not put it in code. However, this morning I realized I could do something like it in the opposite direction. Basically with a square diamond algorithm you're supposed to start with a map, and fill in the gaps as you go. However what I did here was start with a 2x2 square and progressively make it bigger as I go, creating gaps and filling it in like the square diamond method is supposed to. Produces great results. Note I use pygame to display it, so if you don't have it it will not show anything, but the algorithm still works. It seems fairly fast as well, even though I think it's probably fairly inefficient.
|
Author: | Raknarg [ Wed Dec 18, 2013 2:35 pm ] | ||||
Post subject: | RE:random map generator | ||||
For some cool colours you can replace the colour def with this:
EDIT: or if you like fire
|
Author: | Zren [ Wed Dec 18, 2013 6:39 pm ] | ||||
Post subject: | Re: random map generator | ||||
Because everything's better with pictures. Pretty neat! ~
Never use [] / lists / objects / pointers in a functions definition (as a default) in python. The default object is static over all calls to the function. Use m=None and if m is None: m = [[]]. You got away with it this time as you were reassigning m at the start anyways. ~ Tip:
Also, you can floor() (x1, y1) and ceil() (x2, y2) when drawing the tiles with real numbers. That way you don't get the black borders (as they'll overlap instead of falling short). ~ I modded it to be iterative instead of recursive to watch it in progress. It get's fairly slow after round 7 deep, it pretty much dies by depth = 9. |
Author: | Raknarg [ Wed Dec 18, 2013 6:50 pm ] |
Post subject: | RE:random map generator |
Oh thanks. What would happen if I didn't reassign it? And thanks, I was wondering how to get rid of those lines. I ended up just using lines, that's a smarter idea. And yes due to the nature of this algorithm, it's not really feasible to go past depth of 7 or 8 because after that it wont even really fit on my screen, and it's super slow. If I started with a map and working inward instead of constantly expanding it it would likely be faster, because I would't be doing all this array shifting and resizing. I'm also trying to figure out a way to do this without it being a square. The only problem seems to be getting the program to know which pixels to get it's data from. I'll look into seeing if I can rewrite it so its faster and scaleable. I could just resize a square image but that just feels like cheating, and it wouldn't look as good. |
Author: | Zren [ Wed Dec 18, 2013 10:01 pm ] | ||||
Post subject: | Re: RE:random map generator | ||||
Raknarg @ Wed Dec 18, 2013 6:50 pm wrote: Oh thanks. What would happen if I didn't reassign it?
Fun times.
It's hell to debug if you don't know about it beforehand. |
Author: | Raknarg [ Wed Dec 18, 2013 10:06 pm ] |
Post subject: | RE:random map generator |
So why does it do that? |
Author: | Dreadnought [ Thu Dec 19, 2013 3:13 am ] | ||||||||
Post subject: | Re: random map generator | ||||||||
Raknarg wrote: So why does it do that? First, objects in python are either immutable such as numbers, strings and tuples (the last two might surprise you) or mutable such as lists and dictionaries. Immutable objects cannot be modified and mutable ones can, for example
So lists objects can be modified, so what? Shoudn't a new list be created every time we call our function? Well, functions are first class values in python (i.e. they are objects) and def is an executable statement that creates a function object and binds it to an identifier (the function name). Hence why we can't have functions and other values with the same identifier (name).
According to the documentation for function definitions "default parameters are evaluated when the function definition is executed" which makes sense since def has to create a function object with a default value for arguments that makes sense at the time the function was defined, for example
So hopefully, you can see why a default parameter has to be evaluated when the function definition is executed. Now what happens when we have a mutable value as our default? We create the mutable object once and every time we call the function the default value for the argument is the same object (which we can modify). Here's an example like Zren's which shows that the list is only created once.
Hopefully, this explains everything. It really is one of those things that is unexpected for almost everybody the first time they encounter it. |
Author: | Raknarg [ Thu Dec 19, 2013 10:24 am ] |
Post subject: | RE:random map generator |
Oh wow, that's interesting. I'll keep that in mind for the future. |