September 2012
Intermediate to advanced
552 pages
14h 48m
English
To finish the Blob Clicker game, the last piece needed is the game.ejs file, which contains the code for the game. Because the game in this case is extremely simple, rather than pull in the Quintus engine, the code just uses a few jQuery calls to update the page.
Add the code in Listing 20-6 to a new file in the views/ directory called game.ejs.
Listing 20-6: The view/games.ejs file
<!DOCTYPE html> <html> <head> <title>Blob Clicker</title> <script src="//code.jquery.com/jquery-1.7.2.min.js"></script> <link href='/style.css' rel='stylesheet' type='text/css' /> <meta name="viewport" content="width=device-width, ↵ user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"/> </head> <body> <div id="fb-root"></div> <div id="main-screen"> <h1><%= user.name %></h1> <div id='blob'>Click Me</div> <div id='clicks'><%= user.clicks %></div> <div id='show-top-ten'>See Top Ten</div> <div id='hide-top-ten'>Back to game</div> <ol id='top-ten'></ol> </div> <script> $(function() { var nextClick = <%= nextClick %>, clickTimer = null; function updateNextClick() { $("#blob").text(nextClick >= 0 ? Math.ceil(nextClick) + " seconds" : "Click Now"); } function setClickTimer() { clearInterval(clickTimer); clickTimer = setInterval(function() { nextClick--; updateNextClick(); },1000); } updateNextClick(); setClickTimer(); $("#blob").on("click",function() { $.post("/click",function(data) { if(data.clicked) { $("#clicks").text(data.user.clicks); nextClick = data.nextClick; updateNextClick(); ...Read now
Unlock full access