Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: x,y map help

  1. #1
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default x,y map help

    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 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($imtrue); // turn it on, just for fun

    // Colors
    $grass ImageColorAllocate($im67,154,67);
    $enemy ImageColorAllocate($im255,0,0);
    $bgcolor ImageColorAllocate($im39,104,39);

    // exmaple color, not relevant to program.
    $white ImageColorAllocate($im25500);

    ImageFill($im00$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($im1+$i*$size21+$b*$size2$size+$i*$size2$size+$b*$size2$enemy);

            }
            else
            {
                
    // Nobody lives there, draw some grass.
                
    ImageFilledRectangle($im1+$i*$size21+$b*$size2$size+$i*$size2$size+$b*$size2$grass);
            }
        }
    }  
    ?>
    can anyone help, i been working to get this for a while now.
    thanks

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Is that the entire script? You never actually display the image-- this is done (usually) through imagepng() or related functions.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    hey daniel thanx for the reply.

    i had a try at adding:
    PHP Code:
    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:


    the red dot should indicate that an enemy resides there
    Last edited by liamallan; 08-15-2010 at 09:59 AM.

  4. #4
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    also, i removed the header to see if there were any errors, and it displayed:
    Code:
    �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?
    Last edited by liamallan; 08-15-2010 at 01:30 PM.

  5. #5
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    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?

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    it only displays a plain green background. no grid and no colored rectangles

    just this:


    its supposed to look like the image in post above

  8. #8
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    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 Code:
    <?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
    Last edited by liamallan; 08-16-2010 at 09:31 PM.

  9. #9
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    anyone?

  10. #10
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    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

    this is my code now:
    PHP Code:
    <?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>";
            
            
    }
    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •