0 Favourites

[Solved]Random map generation - Array - Tiles

  • Good day everyone,

    I have read some tutorials on the website and some forum post. Unfortunately, I am teaching myself on most of the stuff.

    I have an idea, but can't seem to find to proper to do it. I am still trying to wrap my head around the Array...

    I want to do a game that on start it would generate a first random sprite (grass tile or on of the many street tiles).

    Each of those would have a condition of what can be next to it.

    ie: To create one street section I have 2 tiles of 128px each. So each side of the street is a tile.

    I want to tell the array that is it has the right side (frame #) in XY it needs the left side next to it (frame #).

    What I want to know:

    1- How do I tell the array to choose a random sprite frame in XY

    2- How to had conditions to that, so the map actually looks like a city.

    3- I just need to be hint on what direction to take to make this happen.

    Here is an example of the game. I placed the sprites manually (use WASD to move, hold Shift to sprint):

    https://dl.dropbox.com/u/89646560/Walker/index.html

    Here are the sprites I want to use (the street is 12 wide):

    http://imgur.com/a/wMfyo

    Hopefully you guys can point me in the right direction. All help is greatly appreciated.

    Cheers !

    -Mike

  • if all your frames are in the same sprite then you dont need an array to CREATE the tiles you just can use the array to store what tiles are where.

    The best way to go about randomly making this like you want is to generate the streets on a grid first then generate anything else in the places streets arent.

    The first thing you need is a layout evenly sized for your tiles. If the map can be 20 tiles wide then the layout needs to be the width of the tile*20 wide. Same for height.

    Now instead of thinkin in terms of x and y location you can think in terms of location on a grid (layout.width/tile.width = number of columns in the grid. layout.height/tile.height = number of rows)

    To generate your roads you need to start at a location on your grid (probably a corner or edge) and then place a road tile. Now you know that if your road is going left/right then you need to put another road tile up or down to make the full road. Thats upto you how you want to do it but you need to do it that way every time a road tile is made goin left or right.

    Now you need to randomly decide a direction for the next part of the road. You can do that with choose() or random() or however you want to set it up. But randomly pick a direction (going any direction but the one you went last aka if the last road was placed right you dont wanna go left this time just up down or right again)

    When you get to another edge or corner you know your roads done. That will generate one random road. Do it again from a different starting location to make another road etc.

    Its not the best or fastest way to do it but its a good way to get you started with randomly generated content. Once its working you can tweak it to make it do things like generate intersections and instead of makin 1 road at a time you can make it generate a new road everytime it makes an intersection.

    The main things are

    • use a grid dont worry so much about the actual x,y values. You DO need em to place the tiles in the right part of the grid but dont try to use those to generate the locations
    • You can use an array to store the tile in the same location in the array as it is in the grid (the grid is really a 2d array) then use this array to tell exactly whats where or whats empty (for makin buildings and stuff between roads)
    • dont generate over what youve already generated (dont go left if you just went right)
    • if you need somethin made with each tile generated do it then. Dont try to go back and do it after its generated. Like the other part of the roads in your case. Itll save you headaches down the road.
  • -deleted due to lack of usefulness-

  • Aridale,

    I understand what you mean. My problem is I have a hard time putting my ideas in code because I am lacking the knowledge.

    I think I was looking for a bit of code to get me started or give me a place to begin building from.

    Thanks for the fast response.

  • Alright, I manage to get something together.

    Now my problem is in my code haha.

    Here is the .capx : https://dl.dropbox.com/u/89646560/Walker.capx

    It seems like my first tile is always selecting the 2nd frame of the sprite. It's like it is skipping para 3 in the code, and go directly to 4.

    I want to solve this issue before creating the rest of the code.

    Anyone have an idea to get me out of this ? lol

    Thanks a bunch in advance !

    -Michael

  • Are the 2 codes conflicting ?

  • I've always wanted to make a generator like this. So much fun.

    Anyway, to attempt this, you need to have a really solid knowledge of what an array is. This tutorial is excellent:

    scirra.com/tutorials/307/arrays-for-beginners

  • I've always wanted to make a generator like this. So much fun.

    Anyway, to attempt this, you need to have a really solid knowledge of what an array is. This tutorial is excellent:

    http://www.scirra.com/tutorials/307/arrays-for-beginners

    Thanks, it is the tutorial I use to create the code in the above message. Unfortunately, there is a small problem with the 0,0 of my array.

    I am unable to make it random. It's like if the line 4 in my code is overiding line 3.

    Any help ?

    Maybe if you could take a look at the code.

    Cheers

    -Michael

  • Anyone ?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Remember that events are executed from top to bottom.

    If you have two following events with close conditions, if they both execute, you'll always have the impression that only the last event executed.

    In that case, both event 3 and 4 fill the (0,0) cell of your array.

    First event 3 does, and right after, event 4 overrides the value.

    I'm not sure exactly what you're trying to achieve (as squiddster already mentioned) but atm, C2 executes the code the way the code is written, and it does what the current codes tells it to do.

    If you want to make the value random, either don't use event 4, or put a random expression in the event 4.

    Once again, not sure what exactly you're aiming for though.

  • What I am trying to do is generate a map that with randomize the streets. That is the first step. I will then add more conditions to make it more specific.

    I really want 0,0 to be random and then from left to right choose different frames. I would just love to have 0,0 random and build from there.

  • Hello guys,

    I have try different solutions and I think my code is right, but now I am stuck with 1,0 in my array that is staying on the same Sprite frame.

    Can someone look at the code in my .capx and tell me what I am doing wrong ? I have been working on this for days now and none of my tries work.

    It's the code in event sheet 2

    https://dl.dropbox.com/u/89646560/Walker2.capx

    I would really appreciate.

    Cheers !

    -Michael

  • What I am trying to do is generate a map that with randomize the streets.

    When we ask you what you're aiming for, this kind of answer is still vague.

    What do you mean by "randomize the streets" ? In your program what are the "streets" supposed to be ?

    Also your start of layout is quite messy.

    You're running a loop to check the former value in the array and set the current value according to that, why is that so ?

    For your first question you wanted to randomize the value at 0,0 coordinates in your array.

    Why not simply use the action "Set Map.at(0,0) to random(the_range_of_values)" ?

    As it is still unclear what you're wanting EXACTLY to do, it's hard to provide assistance.

    Moreover you say you will add other conditions later ? It almost sounds as you don't even know what you're trying to do yourself. Detail EVERYTHING at once, and we might from this point propose implementations or guidance.

  • Sorry if I am unclear on what I am trying to do.

    It is my first time, and though I was doing it right.

    Every time someone starts the game the map will be generated. I want it to be random and different each time.

    I divided my layout in tiles, which are represented by the array.

    I want 0,0 to be a random frame.

    Then the 1,0 will look up what is to its left and select a frame number. I am using the choose() for that.

    Then another line is telling to look at the array and to create a sprite at a determined position with the selected frame (array.CurValue)

    The frames are made that they will create patterns of streets.

    Saying that I will add more conditions was probably wrong. What I meant is: I want to have the first line in my array to work before trying to make the other rows match up. I want to get it right from the beginning to refrain from redoing tons of code.

    In the end I want the game to look a bit GTA style.

    I though that if I use "Set Map.at(0,0) to random(the_range_of_values)" the loop following it will replace the value that was randomized...

    Is there anything more you require for info ? I am not sure if I am missing something.

    Thanks for the help :)

  • First, your road sprite aren't really well designed. You shouldn't split the road in half. You should create a whole section of road and also create intersection sprites of the same size. Here in your cap you can't create a T intersection for instance. Then you start to see that you'd need many more sprites.

    Second, one of the way to generate a street would be to create the road network first. By road network I mean simple 0 or 1 in your array to figure out if it's a road cell or a non road cell.

    Then you just have to spawn the road sprite and check their surrounding to display the proper frame.

    Now the algo to create such a road could be :

    loop until you can't find any available cell

    • > cell at X,Y = 1 (1 = road, 0 = no road)
    • > choose a random direction between (1,0) (0,1) (-1,0) (0,-1)
    • > if the next cell is a valid one (not already a road cell and not out of grid) set X and Y to it.

    But then you will have a simple path without branching. For branching you'd have to go back in the road you've made and check for other adjacent available cells.

    Anyway, look at some maze algorigthm you might find some good tips.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
81 11,905
LaurenceBedford's avatar
LaurenceBedford
Unread hot topic
0 Favourites
[PLUGIN] Google Map
118 34,687
eryson27's avatar
eryson27
Unread hot topic
85 16,423
gaminslab's avatar
gaminslab