Download presentation
Presentation is loading. Please wait.
Published byAndra Barker Modified over 8 years ago
1
Programming Games Reprise Credit Cards! Reprise Binary. Overall time limit, setTimeout Homework: [Show virtual something.] Make proposal as reply to my post due 10/7. Work on your project: due 10/18. Midterm 10/14.
2
Credit card Recall new balance is –Old balance + purchases – payment –THEN add in the interest balance = balance+purchases-payment Balance = balance + (balance*rate/12) Where rate is the ANNUAL rate, so it is necessary to divide by 12 for the monthly rate. Remember decimal point!!!!!!! Rates given as percentages must be divided by 100.
3
Binary decoding Write out the powers of 2 16 8 4 2 1 Write the number in binary underneath, making sure the rightmost number lines up under the 1 1 0 is 2 + 0 is 2 1 1 1 is 4+2 +1 is 7 1 0 0 0 is 8+0+0+0 is 8
4
Suggestions/warnings Pay attention Ask for help Come to office hours Programming (programming logic) is a distinct way of thinking.
5
Adding over-all time limit Add as global variables var gametime; var timelimit = 15000; //15 seconds Add when you want timing to start Issue when this should be. May require if statement gametime = setInterval("finish();",timelimit); Add finish function definition function finish() { display in some way that game is over stop / re-set game }
6
Alternate: setTimeout Sets up one timing event gametime = setTimeout("finish();",timelimit); Note: you can cancel the one event using clearTimeout(gametime);
7
Adding keeping time Time kept as part of score, but no time limit. –different type of pressure Use variables to keep starting time: var startDate = new Date(); var startTime = startDate.getTime(); //This is the milliseconds from a base time To determine time elapsed, nowTimeD = new Date(): nowTime = nowTimeD.getTime(); elapsed = (nowTime-startTime); //milliseconds elapsed = (nowTime-startTime)/1000; //seconds
8
Display You don't have to display everything… To display time, say there is an input element named time in a form named f and an internal variable, elapsed document.f.time.value = Number(elapsed);
9
Alternative Say you (your code) calls setInterval every mm milliseconds to call a function change In change, elapsed = elapsed + mm/1000; This rounds down to seconds. To round properly: elapsed = elapsed + (mm+500)/1000;
10
Saving original state/picture Suppose you want to display an image, say an animated gif, for S seconds and then revert back to previous image. Global variable: var origimg; In change() function or whatever function decides to display the temporary image origimg = document.pet.src; temp = setInterval("reset();",S*1000); Definition of reset function reset() { clearInterval(temp); //turn off timing document.pet.src = origimg; }
11
Ending / stopping the game My virtual dog function stopgame [just] stopped the calls to change. It may be appropriate to do more Global variable: var alive = true; In different functions: if (alive) { } else { alert("game is over. Reload to replay");
12
Stopping, cont. In function stopgame() { alive = false; … }
13
Pesky players…. What happens in my/your slide show if the player/viewer keeps clicking on the start button?
14
Slide show, virtual, etc. May be appropriate to stop player from calling [code that calls] setInterval repeated times. Global variable: var started = false; function startgame() { if (started) return false; started = true; tid = setInterval("change();",1000); }
15
Preventing re-starting function stopgame() { started = false; clearInterval(tid); //stops the calls to change }
16
Caution File names must be exactly the same On the [old] newmedia.purchase.edu server, running Linux, this means the same case –AND no spaces EXTRA CREDIT opportunity: determine situation on students.purchase.edu! Remember: Everything to be viewed on the Web must be within your web folder.
17
Homework (Catch up) (Post proposal and see my response) Work on JavaScript project Look at midterm guide and be prepared to ask questions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.