Sign in to follow this  

PHP Tile System Trouble.

Recommended Posts

Hello, I have been trying recently to put together a program in PHP that can essentially find a specified amount of coordinates around the players coordinate (drawn from the database). So for instance if you wanted to load a 5x5 grid of coordinates (eventually graphical tiles) you could use this program, although the database has not been connected to this program yet so the player coordinates are defined by the program for now. Here is the source:

$playerx = 20;//later these coordinates will be taken out of the DB.
$playery = 20;//^above^

$xstart = 4;//distance of the farthest x point from the player(center).
$ystart = 4;//distance of the farthest y point from the player(center).
$xlength = 9;//length of the x axis side.
$ylength = 9;//length of the y axis side.

$truex = $playerx - $xstart;//truex is the actual coordinate on the x axis farthest away from the player that will be shown as a tile.
$truey = $playery + $ystart;//truey is the actual coordinate on the y axis farthest away from the player that will be shown as a tile.
//truey is different from truex because we want to start with the uppermost tiles so we add to the players y coordinate,
//while as truex finds the left most coordinate and with both truex and truey we get the top left most corner of the grid.
$truelengthx = $truex + $xlength - 1;//truelengthx represents the other point on the x axis that is farthest away.
$truelengthy = $truey - $ylength + 1;//truelengthy represents the other point on the y axis that is farthest away.

while ($truey>=$truelengthy)//loop that goes until truey is less or equal to truelengthy, this loop is being used to lower the y coordinate number by one every time the loop goes through.
	while ($truex<=$truelengthx)//loop in the loop that goes until is more or equal to truelengthx, it outputs all of the changes in x at the same time.
	echo "[$truex,$truey]";//outputs the coordinates.
$truey--;//takes one from truey and restarts the loop.

With the current values in the variables you get this for output: [16,24][17,24][18,24][19,24][20,24][21,24][22,24][23,24][24,24] However, I think it's the y loop is not working for me. I am trying to get the code to keep looping so after the x loop is done it outputs the coordinates like above and then subtracts one from the y and starts the loop over until the y loop is done. So if done correctly it would look like this: [16,24][17,24][18,24][19,24][20,24][21,24][22,24][23,24][24,24] [16,23][17,23][18,23][19,23][20,23][21,23][22,23][23,23][24,23] [16,22][17,22][18,22][19,22][20,22][21,22][22,22][23,22][24,22] [16,21][17,21][18,21][19,21][20,21][21,21][22,21][23,21][24,21] [16,20][17,20][18,20][19,20][20,20][21,20][22,20][23,20][24,20] [16,19][17,19][18,19][19,19][20,19][21,19][22,19][23,19][24,19] [16,18][17,18][18,18][19,18][20,18][21,18][22,18][23,18][24,18] [16,17][17,17][18,17][19,17][20,17][21,17][22,17][23,17][24,17] [16,16][17,16][18,16][19,16][20,16][21,16][22,16][23,16][24,16] With 20,20 being the player coordinate that we started with and the rest of the points being generated by the loop where you can set the amount of coordinates around the player you want by changing the variables correctly. So why are the loops (or maybe something else) not working? Thanks for your help, ~SuperJman

Edited by SuperJman

Share this post

Link to post
Share on other sites
The pair of variables ($xstart, %ystart) provides the same information the ($xlength, $ylength) one does. In other words, they are redundant. I'll use $xstart and $ystart only:

$playerx = 20;
$playery = 20;

$xstart = 4;
$ystart = 4;

for($truey = $playery+$ystart; $truey&rt;=$playery-$ystart; $truey--) {
for($truex = $playerx-$xstart; $truex<=$playerx+$xstart; $truex++) {
echo "[$truex,$truey]";

Ask if you have any doubts.

P.S.: Btw, the problem in your code is the inner loop only works once. When $truex goes over 24 you don't reset it and it for all other $truey values the condition is evaluated as false. You should have the "$truex = $playerx - $xstart;" before the inner loop (making sure the variable always got to the initial value before entering the while).

P.P.S.: Code edited

[Edited by - pedrosorio on March 15, 2009 4:22:11 PM]

Share this post

Link to post
Share on other sites
I tried the same thing with "for" loops but that did not work either and yes I did know the loop only works once I was asking how to fix that. Your code does not seem to be doing the trick for me though but thank you for the condensation I had a feeling it could all be done a little easier.

Have you tried the code you made pedrosorio? just wondering if it was me or the code.

Thanks for the help,

P.S. on the line that says:

for($truex = $playerx-$xstart; $truex<=playerx+$xstart; $truex++) {

you missed the $ before playerx, it still does not work with that changed though.

Share this post

Link to post
Share on other sites
Sorry for the double post, can I do this?

I found out the problem, on this line:

for($truey = $playery+$ystart; $truey<=$playery-$ystart; $truey--) {

the $truey<=$playery should be $truey>=$playery.

It works fine now, thank you so much for the help,
~ Jakob

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this