Creating a hidden object game in html5 (pseudo code)

Started by
0 comments, last by Richard Cesar 11 years, 9 months ago
Hello.

I'm trying to create a reasonable simple hidden object (where is Waldo/Wally type game) and having some trouble creating a plan of action to create this.

What I want to do is have a starmap and ask Users to find a specific Zodiac. When a user clicks a star it should shown to be clicked and once the user believes they have the right stars selected can submit it to check if they are right or not.

My plan is to have a blank starmap and set it as the background image. I know I can use jquery to bind mouse clicks over the image. My idea than was to have a list of the correct pixle positions and check if the users click was within x radius of the right spot to verify they clicked a star. My problem is how to show the user clicked something. Right now the only way I can think of doing that is have a different image map of each star selected (plan is just to colour them in) and the rest of the map set as transparent and just overlap the background image with a selected star image (so as they keep getting overlapped the stars should be shown to be coloured in).

In a nutshell my Pseudo code is
[source lang="plain"]
pick random zodiac.
Set blank star map as background image
do
{
if mouse click
{
send server the spot clicked
check if spot is within X of any stars (list of correct positions)
if user clicked a star highlight/deselect star
//insert idea here how to highlight star
}
if user submits
{
check if right starts selected
If true than game isDone
if not inform user their selection is wrong
}
} while (isDone == false)[/source]


So just wondering if this would be the best way to go, if there is a more efficient way?
Advertisement
Hello Wizuriel:

There are quite a few ways you could approch this problem, and HTML5 is unnecessary (and unless your planning on going the canvas route, irrelevent). so the direction you choose to take this in is really a matter of preference and target audience (browser support). I am going to make the assumption you are using jquery here, although any method of dom manipulation should port fine.

In the ancient days, ala the pyrimids, babalyon, the invention of the pythagoran theorem, and using tables to describe the layout of a page, the W3C oracles gifted us a tag for what they called the "image maps" (http://www.w3schools.com/tags/tag_map.asp). This is what all those "find-em" games were written in back in the 90's and early 2000's. This is probably going to yield the best browser support due to how long support for this has been around, but is riddled with its own pesks, and there are simply better ways to approch the problem provided you are not interested in support for browsers over 10 years old.

In more modern days, the approch you suggested is a little more valid, though not nearly as limited to an "image" (in fact, thats hardly your best solution). Using javascript, you can easily bind yourself to the click event of ANY element. The 1st argument to the click event contains a browser-dependent location (I believe pageX/pageY, but this is one of those things that is generally normalized by api's like jquery,mootools,or prototype which you should certainly use). You connect to this event, You can have a div with a background-image representing your star-map, or an img with a src pointing to one, or a blank div with stars as individual html sprites (positioned div's with a transparent background image that looks like a star), etc. If you have the stars as indiviual elements placed on the page, you can simply bind to THEIR click events, and you will instantly know what star they click, changing the src or background-image of the star as needed. If you are using a background image with no individual elements for stars, then you will want to take the mouseX/Y location, offset it by the parent element (the starmap)'s position, in order to get a normalized starmapX/starmapY value. You then can have a bucket with all of your stars x y and radius. Then simply calculate a hit test to see if your normalized vector would be within the circles. Regardless of which the directions you take here, you should now have the "star" that was clicked (or lack thereof).

When it comes to visually showing your selection, canvas not-withstanding, one solution would be to CREATE the stars THAT WERE SELECTED by simply adding an absolutely positioned div or image on valid click (just make sure the starmap itself is positioned, hence why using the PAGE may be a bad idea). This could be a background-transparent image that goes ON TOP of the star to show its selected, or any number of tags. Furthermore, bind to the created tags click events (on create) so that you can toggle on and off stars which were selected, which removes the element. You may want to consider using delegated events here, to cut the number of event handles down.

Constallations can then be arrays of stars, probably indexed multiple times with an index for each star it spans (allowing you to quickly derive which constallations are currently being clicked). You should also store your selected stars, along with a reference to whatever represents it, to aid in finding/clearing/deleting it later.

This topic is closed to new replies.

Advertisement