Log in

View Full Version : x,y map help



liamallan
08-14-2010, 08:47 PM
hey guys, im trying to make a game map, which will be like a x,y grid, a query will get the information stored in the 'x' and 'y' columns of the database table and project them onto the map.

im having a small problem though, the image created by php doesnt show(like the image url isnt right), i think it may be because of the header reference at start of code.

here is the code:

<?php
header("Content-type: image/png"); // set as png

mysql_connect("localhost", "uname", "pass") or die("Mysql Error");
mysql_select_db("dbname") or die("Database error.");

// Image Size
$height = 247;
$width = 247;

// Image Pointer, turn on antialiasing
$im = ImageCreate($width, $height); // this is the image
ImageAntiAlias($im, true); // turn it on, just for fun

// Colors
$grass = ImageColorAllocate($im, 67,154,67);
$enemy = ImageColorAllocate($im, 255,0,0);
$bgcolor = ImageColorAllocate($im, 39,104,39);

// exmaple color, not relevant to program.
$white = ImageColorAllocate($im, 255, 0, 0);

ImageFill($im, 0, 0, $bgcolor); // Fill the image with the background color

$size = 5; // size of a block, 5 x 5
$size2 = $size + 1; // don't change this, it is for spacing


$query = "SELECT * FROM `addhit`"; // get all user information
$result = mysql_query($query);
$userarray; // initialize variable

while($row = mysql_fetch_array($result))
{
$userarray[$row['x'] . ',' . $row['y']] = true; // load all the user info we need into an array
}

for($b=0; $b<=40; $b++) // the grid is 40 x 40, this will make the Y columns
{
for($i=0; $i<=40; $i++) // the grid is 40 x 40, this will make the X rows
{
if ($userarray[$i . ',' . $b] == true) // If the $userarray says that there is sombody in this location
{

ImageFilledRectangle($im, 1+$i*$size2, 1+$b*$size2, $size+$i*$size2, $size+$b*$size2, $enemy);

}
else
{
// Nobody lives there, draw some grass.
ImageFilledRectangle($im, 1+$i*$size2, 1+$b*$size2, $size+$i*$size2, $size+$b*$size2, $grass);
}
}
}
?>


can anyone help, i been working to get this for a while now.
thanks
:)

djr33
08-15-2010, 04:32 AM
Is that the entire script? You never actually display the image-- this is done (usually) through imagepng() or related functions.

liamallan
08-15-2010, 09:15 AM
hey daniel thanx for the reply.

i had a try at adding:

imagepng($im);
to the end of my code, and had a look at some exanples from php.net.

the output is exactly the same as, if u call an image on a page, but image not uploaded to server, it displays image missing logo/image.

this is an example of what it should look like:
http://simplysimonhosting.com/map.png

the red dot should indicate that an enemy resides there

liamallan
08-15-2010, 11:04 AM
also, i removed the header to see if there were any errors, and it displayed:

�PNG IHDR����*�PLTEC�C�'h'�E���&IDATh���1 �Om O��<�~��:IEND�B`�
what does that mean?

also, does $userarray have any point to it? does it do anything?

liamallan
08-15-2010, 03:18 PM
i got the green background to display! the problem was because the php was surrounded with html so that stopped it working properly.

however, the 'imagefillrectangle' doesnt seem to be working.

am i using '$userarray' correctly?

djr33
08-15-2010, 03:39 PM
A lot of questions... I'll try to answer them all.

1. imagepng() has a second optional parameter: save to file. Without it, it will be displayed directly. With that, it will be saved to the server. Which do you want? Does it work now?

2. Removing the headers causes the image to be displayed as text: you are trying to "read" a picture-- that's what it's supposed to look like. The code you see says, more or less, "I'm a PNG, be ready to display the following pixels....", but since it is served as HTML (default), not a PNG, it doesn't work. Those headers should be correct. There's no need to remove them.

3. There are several ways you could use $userarray, but it seems to work so that is fine. Here's one idea, though:
if ($userarray[$i . ',' . $b] == true)
That is one way to do it, but you could also simplify that to:
if (isset($userarray[$i . ',' . $b]))


I don't know why imagefillrectangle isn't working. The GD image function library is always a little hard to use. What is wrong, exactly?

At this point, what is working and what is not working? The only problem is imagefillrectangle?

liamallan
08-15-2010, 04:19 PM
it only displays a plain green background. no grid and no colored rectangles

just this:
http://shiftysplayground.co.cc/me.php

its supposed to look like the image in post above

liamallan
08-16-2010, 03:18 PM
hey guys, i have decided to dump the header imagecreate idea, and opted for a simpler table-type map which works.....to a certain extent.

it only pulls one row from the mysql table and displays on map, instead of displaying all rows.

this is the section of code that displays each row on the map:

<?php

function DisplayGrid($gridx,$gridy) {
global $x, $y;

$sfield="SELECT * from addhit";
$sfield2=mysql_query($sfield) or die(mysql_error());
$sfield3=mysql_fetch_array($sfield2);

if($gridx == $sfield3['x'] && $gridy == $sfield3['y']){
echo "<td bgcolor=\"#FF0000\" align=center valign=center data-tooltip=\"sticky1\"></td>";
print "<div id=\"mystickytooltip\" class=\"stickytooltip\">";
print "<div style=\"padding:5px\">";

print "<div id=\"sticky1\" class=\"atip\" style=\"width:200px\">";
print "".$sfield3['x'].", ".$sfield3['y']."<br />";
print "<img src=\"../hostile.JPG\" alt=\"\"/><br />";
print "<b>Lord Name:</b> ".$sfield3['lordname']."<br />";
print "<b>Alliance:</b> ".$sfield3['alliance']."";

print "</div>";
print "</div>";
print "<div class=\"stickystatus\"></div>";
print "</div>";
}else{
echo "<td background=\"map.png\" align=center valign=center></td>";
}

}
?>

here is a link to the problem page (http://shiftysplayground.co.cc/map.php)

liamallan
08-16-2010, 08:50 PM
anyone? :(

liamallan
08-17-2010, 12:25 AM
i have managed to get the different citys to show on the map but when the city info box(DD Sticky Tooltip script) pops up, all the citys hold the same information :confused:

this is my code now:

<?php

function DisplayGrid($gridx,$gridy) {
global $x, $y;

$sfield="SELECT * from addhit";
$sfield2=mysql_query($sfield) or die(mysql_error());
while($sfield3=mysql_fetch_array($sfield2)){

if ($gridx == $sfield3['x'] && $gridy == $sfield3['y']) {
echo "<td width=7 bgcolor=\"#FF0000\" align=center valign=center data-tooltip=\"sticky1\"><font size='1'></font></td>";
print "<div id=\"mystickytooltip\" class=\"stickytooltip\">";
print "<div style=\"padding:5px\">";

print "<div id=\"sticky1\" class=\"atip\" style=\"width:200px\">";
print "".$sfield3['x'].", ".$sfield3['y']."<br />";
print "<img src=\"../hostile.JPG\" alt=\"\"/><br />";
print "<b>Lord Name:</b> ".$sfield3['lordname']."<br />";
print "<b>Alliance:</b> ".$sfield3['alliance']."";

print "</div>";
print "</div>";
print "<div class=\"stickystatus\"></div>";
print "</div>";
}

}
echo "<td width=7 align=center valign=center><a href=\"index.php?xcord=$gridx&ycord=$gridy\"></a><font size=1></font></td>";


}
?>

sidanuk
07-20-2011, 06:43 PM
Did this user ever get this running ? , im desperatley trying to find some code for the same thing

Simple input applet with X.Y that then shows on a grid and stores it in the database , mabey 3 colours for various points.This is the closest i have found on the web .

Can anyone help

Thanks in advance