Jump to content
xisto Community
ewcreators

Creating Something With Co-ordinates With Php. Something like you see in those web-based php games, like a map or som

Recommended Posts

Well, i was just wondering if something simple and basic could be made (like a small map where you could move around on, like you click up, and your position moves up and reports it to the database..etc) with co-ordinates in php . Taking the x-axis and the y-axis into consideration. Things like maps which online games have such as web-based space games or legacy-game.net.Thanks for any feedback,~Aldo

Share this post


Link to post
Share on other sites

Well this is possible but i would suggest using AJAX, the interaction between javascript and PHP (google the term AJAX) As this would make the system run smoother. With AJAX it would work as you said, the user clicks a position and something moves there or it is reported to the DB, without AJAX the user would click, then the page would have to reload or go to another page to display the movement or save it to the DB.The second problem you will have is detecting the co-ords (now referred to as CDs) one solution would be to use HTML image maps. Basically use an image as the background or clickable area then define regions within it using HTML. The downside is these regions would essentially make the clickable area into a grid so the user would be able to click on these little squares but it would be crude and would not use CDs but it could be used to move a player one or more squares in any direction. The other option if you need to use CDs is to use Java as i dont think javascript has the capability to track the mouse or mouse clicks. but i kno nothing of Java so i'll let someone else help there.

Share this post


Link to post
Share on other sites

Well this is possible but i would suggest using AJAX, the interaction between javascript and PHP (google the term AJAX) As this would make the system run smoother. With AJAX it would work as you said, the user clicks a position and something moves there or it is reported to the DB, without AJAX the user would click, then the page would have to reload or go to another page to display the movement or save it to the DB.
The second problem you will have is detecting the co-ords (now referred to as CDs) one solution would be to use HTML image maps. Basically use an image as the background or clickable area then define regions within it using HTML. The downside is these regions would essentially make the clickable area into a grid so the user would be able to click on these little squares but it would be crude and would not use CDs but it could be used to move a player one or more squares in any direction.

The other option if you need to use CDs is to use Java as i dont think javascript has the capability to track the mouse or mouse clicks. but i kno nothing of Java so i'll let someone else help there.

the site i play it on )which is a mega hit( , it refreshes..etc and uses co-ords.
*waits for alex to butt in :lol:

Share this post


Link to post
Share on other sites

Just happen to have been checking this out the other day...

http://www.javascriptsource.com/

Here is a javascript snippet/function that snags the mouse co-ordinates relative to the top/left of the display window. Sounds like it would work for what you are needing. Use Ajax or php to communicate the co-ordinates back to the server and you are all set.

Share this post


Link to post
Share on other sites

Well... I wasn't going to post here but since you were waiting for me to "butt in" I'll give you my two cents. There are a few ways you can do it. One would be just plain refreshing as you had mentioned. This should be easy enough. Just have an x position and a y position for every user in sql. Then what you do is you have a table called map with x(int), y(int), and image(string), where x and y are the x/y coord of that tile and image is the url to the image of that tile. You then decide how many tiles you want your viewable area to be (lets say 10). And then have sql select all tiles and users grater than or equal to your y -10 and x -10, but less than your y+10 and x+10. And then you can lay them out in a table. Now that way would work but if you really wanted to make it fancy you could do it with javascript and ajax like shaddow suggested. And yes javascript can get the mouse position. But be sure to use javascript, DO NOT use java. It is TERRIBLE. It is slow, buggy and just loves to run out of memory. Trust me it's not pretty.

Share this post


Link to post
Share on other sites

You nailed it :P <3 Thanks, I wanted to do it that way but never knew how. Everyone, thanks for your feedback :lol:
Although , don't "resolve" this thread yet, im going to try it out.
Ok, I took a portion of a cleared out map from a game i play (using the map to test only), so now im just trying to code it in a square sort of way, like a graph with a x-axis and a y-axis i already made the database and the table , filled it with the co-ords..etc.

Using a while loop doesn't look to promising, so im trying with a for loop.

Ok, i did it with a for loop :

<?php//connection detailsecho "<table cellpadding=0 cellspacing=0><tr>";$select=mysql_query("SELECT * FROM map WHERE x >=1 && y <= 6");while($map=mysql_fetch_array($select)) {$x=$map['x'];$y=$map['y'];if ($x==5) {echo "<td><img src='$map[image]'></td></tr>";}else {echo "<td><img src='$map[image]'></td>";}}echo "</tr></table>";mysql_close($conn);?>

Edited by ewcreators (see edit history)

Share this post


Link to post
Share on other sites

Quick question, i have everything setup , i am now coding some advanced stuff for my map,so far for now the map displays its entire self, and i want to set the viewable grid/area of the map to like howyou said. So how do i execute my query? I mean what query? SHould i just add x>=..etc because i tried it and my map comes all messed up then.

Edited by ewcreators (see edit history)

Share this post


Link to post
Share on other sites

Can you post the code and maybe a link to the page so we can see what you have done so far?

Share this post


Link to post
Share on other sites

Can you post the code and maybe a link to the page so we can see what you have done so far?

Sure. well, ive only allowed it to be seen by my website staff .
Details :

Database :
Table `map`
x(int),y(int),image(varchar),
Main Image :
http://forums.xisto.com/no_longer_exists/404.png
Sliced it into 225 small images.
The map is a 15x15 square grid.

But here's the code so far :
$select3=mysql_query("SELECT * FROM map_user WHERE username='$_GET[a]'");$us=mysql_fetch_array($select3);$xu=$us['x'];$yu=$us['y'];$select222=mysql_query("SELECT * FROM ships_users WHERE user='$_GET[a]' && status='roam'");$coun1t=mysql_num_rows($select222);if ($coun1t < 1) {echo "YOU CANNOT ENTER SPACE WITHOUT A SPACE-SUITED AIRCRAFT!";}else {echo "<table cellpadding=0 cellspacing=0><tr>";$select111=mysql_query("SELECT * FROM map WHERE x >=1 && y <=15");while($map=mysql_fetch_array($select111)) {$x=$map['x'];$y=$map['y'];//if ($xu==$x && $yu==$y) {//if ($x==15) {//echo "<td><img src='$map[image]' border=1></td></tr>";//}//else {//echo "<td><img src='$map[image]' border=1></td>";//}//}if ($x==15) {echo "<td><img src='$map[image]'></td></tr>";}else {echo "<td><img src='$map[image]'></td>";}}echo "</tr></table>";}

Share this post


Link to post
Share on other sites

Sure. well, ive only allowed it to be seen by my website staff .
Details :

Database :
Table `map`
x(int),y(int),image(varchar),
Main Image :
http://forums.xisto.com/no_longer_exists/404.png
Sliced it into 225 small images.
The map is a 15x15 square grid.



$select111=mysql_query("SELECT * FROM map WHERE x >=1 && y <=15");
A few problems. First of all I've never used && in sql. I'm not quite sure if that works or not. I'd try capital AND. Also where are your other two comparisons? Remember you want to select the tiles between something. This is just kind of an odd query.
I wouldn't suggest using separate numbers here unless you wanted the player to be off center. So here is how you would figure out your query:

$verticaltiles=10;$horizontaltiles=10;//SET PLAYER X AND Y TO THESE VARIABLES:$x=playerx$y=playery$select111=mysql_query("SELECT * FROM map WHERE x>=".($x-$horizontaltiles/2)." AND x<=".($x+$horizontaltiles/2)." AND y>=".($y-$verticaltiles/2)." AND y<=".($y+$verticaltiles/2));

Tell me if this works.

Share this post


Link to post
Share on other sites

Well, all of the code above is great. As for getting the map to have links is a big thing as well. You could always use CSS and position each element, but this would be very time consuming. You also could use image maps in HTML, but I'm not quite sure if that is the preferred method. There also are tables with image links and you could use borders as a way to show a grid for clicking. I would probably use image maps or tables for simplicity.

Share this post


Link to post
Share on other sites

i just used a while loop to display the images, cool cat. And i set the images to the backround of the table data cell. And during the while loop, if the current image x,y co-ords being generated == to the current player's co-ordinates, it places a small image to show where the player is.And alex , ill try out your code.

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

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.