Lately I’ve had two main goals I’ve been working on for my Science Fiction Tower Power game.  Okay, the name is long, I know.  I’m just going to call it Tower Power from now on in passing.  Continuing on, I’ve had two main goals.  Number one, I’ve been working on creating a drop down menu for people to access when they want to set their difficultly level and problem type.  Secondly I want to create a pause function within the game, so someone can tap the space bar and walk away for a bit and come back without loosing where they are.  

As I’ve mentioned before I want to be able to provide statistics and print outs for my nieces and nephews so they can show the work they’ve done on the site.  In order to do so I need to make sure that they have time to pick out the type of problem they want to do, and the difficultly level.  Once I had almost finished my drop down menu I realized that even though someone could access the menu, the ships were still moving and didn’t stop moving.  Therefore a person would have to select their preferences quickly or else all would be lost.  

This made me realize the need for the second aspect, the need for a pause function within the code.  To start with I have to explain how our javascript creates moving objects on the screen.  You see, in order for objects to seem animated they need to recognize time within the code.  This means you have to import time, and you have keep track of it within a function.  To do this we created a function called tick().  

This function is recursive, which means it calls itself (albeit with a delay).  Thus tick() will continuously keep track of the time.  Tick() also runs a whole lot of other code.  In this code we have to run everything that has to be checked every second the game is running.  Some examples of that are: making the ships move across the screen, whether or not a problem has been solved, if that problem was solved correctly, whether or not the player has won the game.  

In order to pause the game, I had to learn all the places that tick() is called, and learn how to stop it or start it as needed.  I also had to learn how to identify that space bar had been pressed.  Coincidentally, this is the code needed to identify that space bar has been pressed.  

 

$(window).keypress(function(e) {
   if(e.keyCode == 32) {
      YOUR CODE GOES HERE 
   }
});

 


First off jQuery functions are identified by the $ in front of them.  Keypress indicates you’re looking for a key press type event.  Next you must to determine what kind of key you’ve pressed.  Each key on the keyboard is identified by a number, which is referred to as a keyCode here in the code.  To discern the keyCode you can use the jQuery function in their documentation which provides it for you.  The double == is javascript syntax verifying if it equals something.  You’ll sometimes see a triple === in javascript.  This is the javascript syntax verifying equality of both values and object types.  If you hadn’t notice the syntax is very different from python and it definitely took some getting used to.  

Also, to note, it’s 11 days til Christmas....more freaking out is had.

Up Next Time: What to do once you know you’ve pressed the space bar