School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,100 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,029 people online right now. Registration is fast and FREE... Join Now!




PHP Game map

 

PHP Game map

Justice92

13 Jun, 2009 - 12:47 AM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 20



Thanked: 2 times
My Contributions
Hey,

Ive been programming php for about a year now and made one game, (texted base one)
I would like to expand on this and make one with a 2D map mixed in with a texted based one, Where you create/upgrade buildings. ( a bit like travian )

I just cant get my head around how i would create the map, I had a look at the GD library and that just confused me more.
Can anyone shed some light on this?

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: PHP Game Map

13 Jun, 2009 - 07:06 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well if you look at the travian game board you will notice it is setup like a series of tiles. Some of the tiles are the same picture. Maybe has a rock and a tree for instance.

This "board" consisting of tiles seems very similar to a 2D array of images doesn't it? So if you think about creating your game board as a 2 dimensional array and each slot of that array consists of a link to a particular picture, you can loop through the array to build a map by putting the tiles together.

So there are three parts to this problem...

1) An array to hold each tile
2) Picture tiles that we will link to in the array
3) Building the array by looping through the array and drawing all the tiles onto an image for display.

Here is how your array can look like given that you have created three tiles (one with a tree and rock "rock.gif", one with a town "town.gif", one with open grass "grass.gif")

CODE


$map = array(
            array("grass.gif", "grass.gif", "rock.gif"),
            array("rock.gif", "town.gif", "grass.gif"),
            array("grass.gif, "grass.gif", "grass.gif")
            );


Now this map above is a 9 tile (3x3) map with the town in the middle and surrounded by grass and rocks. Now normally you would build this using a loop and some code rather than hand code every tile. However, I suggest you start with a small 3x3 map like this until you get the code worked out. Then you can build onto it.

After this you create the three graphic files in your favorite graphic editor and store them in a folder to then reference in the map.

Now you may build a map that is 100 x 100 but show only 9 squares at any one time for the user. So you are going to have ranges that the user can see. So if the user is in the upper left corner of the map, you would show x 0, 1, 2 and y 0, 1, 2. $map[0][0], $map[0][1], $map[0][2] all the way through $map[2][2].

Look at the pattern we have here, a nested for loop will give the tiles you need to show and the last bit is to create an image and draw them using the GD library of PHP. You can see a simple example of drawing at the URL below...

A Simple Example of Drawing in PHP with GD

Clicking "Next" in the upper right corner will take you to pages that show interactive examples and more. So explore it a bit and play with the drawing functionality there.

Hope this helps get you started with some ideas. smile.gif

"At DIC we be PHP image drawing code ninjas...we draw with PHP and with pee in the snow" decap.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:16PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month