Jump to content

Coordinates grid


Porl123

Recommended Posts

I'm trying to make a mini-game using php and mysql, which would be an 11 by 11 grid having the player bang in the center. One where you can click on the other surrounding squares to move your character's coordinates. I've seen this done on other websites but can't think for the life of me how I'd go about creating it. ??? I don't expect to be told personally how it's done because I know that's way too much work, but any of you guys know where I can read up on it? Links would be much appreciated :) thanks

Link to comment
Share on other sites

My initial question is this: is the movement of your character restricted within the 11x11 grid, or is this grid only the viewable area of a larger map in which your character need always remain at the center? The approach for the motion would be different in those two scenarios, but the grid idea would be the same...

 

Basically, think of the grid as a multi-dimensional array (very like a battleship board), where you have an x and y coordinates. Then, you can build a simple class to manage the grid itself and even a GridPoint class if you wanted, to help manage the contents of each point on the grid. Something like this should at least get you started:

 

<?php
class Grid {
  /**
   * @var array $coords;
   */
  protected $coords;

  /**
   * @param int $x width of the grid
   * @param int $y height of the grid
   */
  public function __construct($x, $y)
  {
    $this->coords = array();
    for ($i = 1; $i <= $x; $i++)
    {
      for ($j = 1; $j <= $y; $j++)
      {
        // Load the positions with an empty value to be filled.
        $this->coords[$i][$j] = NULL;
      }
    }
  }

  public function loadPoint($x, $y, $contents)
  {
    if (!isset($this->coords[$x][$y]))
    {
      throw new Exception('Invalid coordinates provided!');
    }

    $this->coords[$x][$y] = $contents;
  }

  public function getPoint($x, $y)
  {
    if (!isset($this->coords[$x][$y]))
    {
      throw new Exception('Invalid coordinates provided!');
    }

    return $this->coords[$x][$y];
  }
}
?>

 

Hope this is at least a good start for you. Good luck!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.