[Solved]Random map generation - Array - Tiles

Get help using Construct 2

Post » Tue Jul 10, 2012 3:22 am

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 !
-MikeWicstar2012-07-14 18:48:01
B
12
S
1
Posts: 22
Reputation: 1,109

Post » Tue Jul 10, 2012 4:36 pm

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.
B
13
S
5
G
2
Posts: 306
Reputation: 3,262

Post » Tue Jul 10, 2012 6:59 pm

-deleted due to lack of usefulness-digitalsmeg2012-07-11 18:15:29
B
12
G
1
Posts: 19
Reputation: 1,066

Post » Wed Jul 11, 2012 12:35 am

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.
B
12
S
1
Posts: 22
Reputation: 1,109

Post » Thu Jul 12, 2012 3:33 am

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
B
12
S
1
Posts: 22
Reputation: 1,109

Post » Thu Jul 12, 2012 11:56 am

Are the 2 codes conflicting ?
B
12
S
1
Posts: 22
Reputation: 1,109

Post » Thu Jul 12, 2012 1:49 pm

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
B
90
S
30
G
24
Posts: 3,189
Reputation: 32,400

Post » Thu Jul 12, 2012 5:18 pm

[QUOTE=sqiddster] 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[/QUOTE]
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
B
12
S
1
Posts: 22
Reputation: 1,109

Post » Thu Jul 12, 2012 10:40 pm

Anyone ?
B
12
S
1
Posts: 22
Reputation: 1,109

Post » Thu Jul 12, 2012 10:47 pm

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.
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 7,000
Reputation: 57,795

Next

Return to How do I....?

Who is online

Users browsing this forum: jtcummins, mjj1990, Vodakman and 7 guests