Email conversation
From | Yusuf Vindhani |
To | Me |
Subject | Java Script |
Date | 9 March 2004 12:01 |
The Author,
I was just wondering whther you knew how to create a puzzle game using java
script. The game has to have one black square and be able to randomise the
the pictures at the click of a button and also solve the puzzle at the click
of a button.
If so can u please give me some sort of code on how to do this.
Regards
From | Me |
To | Yusuf Vindhani |
Subject | Re: Java Script |
Date | 9 March 2004 12:47 |
Yusuf,
I have not written a script to do this, but it is a fairly easy thing to do.
You take a picture, copy it into 'n' DIV elements, then clip each one of
them so that only a set square of each will be shown. When you position
them correctly, it makes a picture.
You would need to keep a couple of arrays of positions to work out what
square was beside the blank square, and allow them to move. The usual
approach is to detect the click event on the div and make it swap places
with the blank square if it is beside it.
Once all elements are in the right places, they have won. To solve the
puzzle, remember every move that has happened (assuming you start with the
complete puzzle then randomly move squares to prepare it), and play them in
reverse order using timers.
If you want to write your own script, you may find that using my 'generic
DHTML do everything library' is helpful:
http://www.howtocreate.co.uk/jslibs/
If you want a complete script, there is one on [dynamicdrive]
but sadly it is poorly written, and will not work properly cross browser.
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/