CMSC 122 | |
Hidden Turkey Game
This project will give you practice with event driven programming, functions, conditional statements, dynamic updates to webpage elements (modifying attributes and "innerHTML"), and CSS.
The project is probably easiest to understand by running this demonstration page. Your project should appear and function exactly like the demonstration page.
The game goes through a sequence of days. On each day the user (who is a turkey) will hide in one of four possible hiding places (which are pumpkins), and then one of the hiding places (selected randomly) will explode! The goal is to survive as many days in a row as possible without hiding in the location that explodes. It's tremendously exciting!
To begin the project, please download this file. It contains the three images that you will need for this project, which are also shown below. Note that two of the images are "animated gifs", which work perfectly on webpages and save us from having to write difficult code to do animations!
pumpkin.gif | hide.gif | explode.gif |
You should begin by creating a table with four rows. You don't need to do this part with Javascript, just create an HTML table on the page. The top row is where it says "Day X", the second row is where the images appear, the third row contains the numbers one through four, and the bottom row contains a message such as "Where will you hide?" Remember that you must use an identifier for any element that will change while the game is running.
For each of the images in the table, you should use "onclick" to run a function when the user clicks the image. Below we will describe a function called "hideMe", which should be called by onclick, passing in the picture number as an argument. For example, "hideMe(3)" should be called if the user has clicked the third picture.
We suggest using two global variables:
We suggest writing the functions below, although you are free to write the project any way you choose as long as the finished product looks and behaves exactly like our demonstration page.
The parameter (location) represents the position where the user has clicked -- in other words, this is where they want to hide, either 1, 2, 3, or 4. The function should put the "hide.gif" picture in the place where the user has clicked (replacing the plain pumpkin picture). Next, the function should replace the text at the bottom of the screen with "GOOD LUCK!". Finally, make a call to the function below (explode) using the setTimeout technique and using 1800 milliseconds for the delay.
Use the following statement to select a random location for the explosion:
explosionLocation = Math.floor(Math.random() * 4) + 1;
Now put the image called "explode.gif" in the place where the explosion has occurred, and replace all three of the other images with "pumpkin.gif". Replace the text at the bottom of the screen with either "You live to see another day" or "You have died", depending on whether or not the user has hidden in the same place where the explosion occurred. Finally, make a call to the function below (conclusion) using the setTimeout technique and using 1700 milliseconds for the delay.
If the user has survived, increment your dayCounter and make a call to the "reset" function, described below.
On the other hand, if the user has died then you should:
This function should replace all of the images with the original "pumpkin.gif" picture, update the day counter at the top so that it reflects the correct day, and change the text at the bottom so that it says, "Where will you hide?".
You should create a zip file that includes your HTML file, your CSS file, and all three images. You will not receive credit for submitting a file that is not a real "zip" file. In particular, "rar" files or "7z" files will not be graded. Submit the zip file to the submit server, as usual. You may submit as many times as you like -- we will only grade the last submission. Late submissions received up to 24 hours beyond the deadline will be accepted, but will be penalized 20 points. No submissions will be accepted more than 24 hours past the deadline.