let s make html5 javascript games
play

Lets make HTML5 & JavaScript Games! credit: w3.or g HTML is a - PowerPoint PPT Presentation

Lets make HTML5 & JavaScript Games! credit: w3.or g HTML is a markup lan g ua g e Markup lan g ua g es are code-based annotation systems HTML is the backbone of every website HTML is made of elements openin g ta g closin g ta g


  1. Let’s make HTML5 & JavaScript Games! credit: w3.or g

  2. HTML is a markup lan g ua g e • Markup lan g ua g es are code-based annotation systems • HTML is the backbone of every website

  3. HTML is made of elements openin g ta g closin g ta g attribute <section id=“box”>HTML is very easy to use!</section> element HTML is very easy to use!

  4. HTML5 is new & dynamic • HTML5 was desi g ned for all of today’s internet-capable devices • Includes new ta g s for more types of content • Check out diveintohtml5.info

  5. Let’s make a simple HTML5 pa g e credit: The Matrix

  6. Create a folder to work in Name your folder CLF-html5- g ame

  7. Create a new file in your text editor Save it as index.html in your folder

  8. Components of an HTML5 pa g e • <!DOCTYPE html>: tells the browser it is lookin g at an HTML5 pa g e • <html>: be g ins the HTML code • <head>: the area where meta information will be located • <meta charset=“utf-8”>: the character encodin g you want to use • <title>: the website title • <body>: the part of the pa g e where we will be workin g !

  9. Comment your code 5 minutes after you write code When you come back to it without comments in 3 weeks

  10. Commentin g code is easy • Preface your comment with < , a ban g and two dashes (<!--) • End it with two dashes and > (-->) • Most text editors have shortcuts (like  + /)

  11. The canvas element credit: thesilverpen.com

  12. What is it? A canvas is a rectan g le in your pa g e where you can use JavaScript to draw anythin g you want. credit: Nintendo

  13. credit: Photon Storm

  14. Create a <canvas> element

  15. Draw on the canvas with JavaScript credit: Jandi Small

  16. Variables • Variables are useful for storin g data that may chan g e throu g hout the course of your app (e. g . your player’s health, location) • To create a variable, you have to tell JavaScript: • The name you’re g oin g to refer to it by • The value (information) that the variable contains

  17. Variables • Variables let you refer to the same information many times • If you need to chan g e that information, you only have to do it once � For example, best friends may chan g e but the label stays the same: � var myBestFriend = “Isaiah”; var myBestFriend = “Rebecca”; var myBestFriend = “Aileen”;

  18. Functions • Function: a named section of a pro g ram that does a specific task • Wraps up code in an easy-to-reference way • Parameter: additional information you can g ive the function to chan g e the output

  19. Function structure var fetch = function (do g ) { run to the ball; pick up the ball; brin g the ball back; }; • Name of the function • Parentheses: Hold any modifiers (also known as ar g uments) • Brackets: What to do in the function • Semicolon: end of line, move onto the next thin g

  20. Settin g up the canvas • document: refers to the HTML document the JavaScript is linked in • g etElementById: a native JavaScript function that looks for an ID attribute on the HTML document • g etContext: tells JavaScript whether we will make a 2d or 3d drawin g • The d must be lowercase • myCanvas.width: the width of our canvas • myCanvas.hei g ht: the hei g ht of our canvas

  21. Calculations + (add) – (subtract) * (multiply) / (divide) var addition = 13 + 22; var division = 100/15;

  22. Like a flipbook, the canvas animates with frames

  23. Frames Frame 1 Frame 6

  24. Framerate and intervals setInterval(function() { do stu ff ; }, 1000); • setInterval(): a native JavaScript function that runs a set of code once per set amount of time • JavaScript thinks of time in milliseconds • 1 second = 1000 milliseconds • How do we g et our canvas to redraw at 60 times in one second?

  25. Framerate and intervals • How do we g et our canvas to redraw at 60 times in one second? 1000/60 setInterval(function() { do stu ff ; }, 1000/60);

  26. update() function

  27. draw() function

  28. JAVASCRIPT RUNS UPDATE DRAW 60 times per second

  29. How does positionin g work?

  30. X=0 x=WIDTH y=0 y=HEIGHT

  31. X=0 x=WIDTH y=0 (3,2) y=HEIGHT

  32. How can I incorporate interactivity?

  33. Event listeners addEventListener(“britishAreComin g ”, function (numberOfSoldiers) { paulRevere.ride(horse); town.alert(numberOfSoldiers); }); • addEventListener: JavaScript waits for somethin g to happen • When the event happens, the contained function will run

  34. Keyboard interactivity KeysDown If is pressed down (keyCode #38) “keydown” If is no lon g er bein g pressed (keyCode #38) Delete “keyup”

  35. Movement New position = old position + speed

  36. How can I test for collisions?

  37. Have these circles collided yet? radius radius

  38. How about now? radius radius

Recommend


More recommend