129
@randyhoyt Randy Hoyt Coding Your First HTML5 Game

Html5game presentation

Embed Size (px)

Citation preview

Page 1: Html5game presentation

@randyhoyt

Randy Hoyt

Coding Your First HTML5 Game

Page 2: Html5game presentation

@randyhoyt

randyhoyt.com/launchgame

Page 3: Html5game presentation

@randyhoyt

• Code

• HTML5

• Games

Overview

Page 4: Html5game presentation

@randyhoyt

• Games

• HTML5

• Code

Overview

Page 5: Html5game presentation

Games

Page 6: Html5game presentation

@randyhoyt

• Games are fun

Games

I love games!

Page 7: Html5game presentation

A Theory of Fun (2004), by Ralph Koster

Page 8: Html5game presentation

@randyhoyt

• Games are fun

• Games are everywhere

Games

I love games!

Page 9: Html5game presentation

Homo Ludens (1938), by Johan Huizinga

Page 10: Html5game presentation

@randyhoyt

• Games are fun

• Games are everywhere

• Games exercise the brain

Games

I love games!

Page 11: Html5game presentation

Play engages the prefrontal cortex,

responsible for your highest-level

cognitive functions – including self-

knowledge, memory, mental imagery,

and incentive and reward processing.

Brain Workout, Life Optimizer, http://trhou.se/WHkaR7

Page 12: Html5game presentation

Brain Workout, Life Optimizer, http://trhou.se/WHkaR7

Page 13: Html5game presentation
Page 14: Html5game presentation

@randyhoyt

• Games are fun

• Games are everywhere

• Games exercise the brain

• Games are practice for the real world

Games

I love games!

Page 15: Html5game presentation

7 TED Talks on Gaming, http://trhou.se/gamesTED

Page 16: Html5game presentation

@randyhoyt

Games And Me

Page 17: Html5game presentation

HTML5

Page 18: Html5game presentation

Buzzword Alert!

Page 19: Html5game presentation

@randyhoyt

• Canvas

HTML5 and Related Technologies

Page 20: Html5game presentation

@randyhoyt

• Canvas

• WebGL http://trhou.se/whyWebGL

HTML5 and Related Technologies

Page 21: Html5game presentation

@randyhoyt

• Canvas

• WebGL http://trhou.se/whyWebGL

• WebSocket http://trhou.se/introwebsockets

HTML5 and Related Technologies

Page 22: Html5game presentation

@randyhoyt

• Canvas

• WebGL http://trhou.se/whyWebGL

• WebSocket http://trhou.se/introwebsockets

• SVG

HTML5 and Related Technologies

Page 23: Html5game presentation

HTML5 Games

Page 24: Html5game presentation

It’s official: with HTML5 today the

browser has become a full-

fledged gaming platform.

HTML5 Gaming, http://html5rocks.com/gaming

Page 25: Html5game presentation
Page 26: Html5game presentation

Behind the Scenes, http://www.cuttherope.ie/dev/

Page 27: Html5game presentation

@randyhoyt

• Top 10 HTML5 games of 2012 http://trhou.se/gamesin2012

• Goko

• Game Closure Devkit

• Famo.us - http://tcrn.ch/ZfCuNh 

HTML5 Games

Page 28: Html5game presentation

Code

Page 29: Html5game presentation

@randyhoyt

Project Structure

CSS JavaScriptHTML

Page 30: Html5game presentation

@randyhoyt

Project Structure

CSS JavaScriptHTML

Page 31: Html5game presentation

@randyhoyt

Project Structure

CSS JavaScriptHTML

Page 32: Html5game presentation

@randyhoyt

Project Structure

CSS JavaScriptHTML

Page 33: Html5game presentation

HTML

Page 34: Html5game presentation

CSS

Page 35: Html5game presentation

Canvas Preview

Page 36: Html5game presentation

Canvas and Context

Page 37: Html5game presentation

Image in JavaScript

Page 38: Html5game presentation

requestAnimationFrame

Page 39: Html5game presentation

update function

Page 40: Html5game presentation

update function

Page 41: Html5game presentation

Game Logic: Init (Once), Update (Loop)

init update

Page 42: Html5game presentation

fillRect

Page 43: Html5game presentation

One wall in the browser

Page 44: Html5game presentation

fillRect

Page 45: Html5game presentation

Five walls in the browser

Page 46: Html5game presentation

drawImage

Page 47: Html5game presentation

Frog variables

Page 48: Html5game presentation

Frog in the browser

Page 49: Html5game presentation

Five walls in the browser

Page 50: Html5game presentation
Page 51: Html5game presentation

clearRect

Page 52: Html5game presentation

clearRect

Page 53: Html5game presentation
Page 54: Html5game presentation

Game Logic: Init (Once), Update (Loop)

init update

Page 55: Html5game presentation

Game Logic: Move

updateinit

Page 56: Html5game presentation

Game Logic: Update, Render, Loop

render

initupdate

Page 57: Html5game presentation

@randyhoyt

• Init

- Place Frog

- Place Obstacles

• Update

- Move

• Render

Game Logic

Page 58: Html5game presentation

@randyhoyt

• Init

- Bind Events

- Place Frog

- Place Obstacles

• Update

- Move

• Render

Game Logic: Bind Events

Page 59: Html5game presentation

@randyhoyt

• Init

- Bind Events

- Place Frog

- Place Obstacles

- Place Badges

• Update

...

Game Logic: Place Badges

Page 60: Html5game presentation

@randyhoyt

...

• Update

- Check for Movement

- Check for Obstacles

- Move

- Check for Badges

• Render

Game Logic: Update

Page 61: Html5game presentation

@randyhoyt

Game Logic: Check for Victory

...

• Update

...

...

• Render

• Check for Victory

Page 62: Html5game presentation

Game Preview

Page 63: Html5game presentation

@randyhoyt

• Impact

• LimeJS

• Crafty

• GameClosure

• CocoonJS

HTML5 Game Frameworks

... and many more!

Page 64: Html5game presentation

@randyhoyt

• Impact

• LimeJS

• Crafty

• GameClosure

• CocoonJS

HTML5 Game Frameworks

Game Engine Comparison, http://trhou.se/gamecompare

Page 65: Html5game presentation

@randyhoyt

• Free and Open Source

• Uses DOM or Canvas

• Built on Google Closure

• Requires Python

• Incompatible with CocoonJS

LimeJS

Page 66: Html5game presentation

> bin/lime.py create treehouse

Page 67: Html5game presentation

HTML

Page 68: Html5game presentation

Starting JavaScript File

Page 69: Html5game presentation

Create Director

Page 70: Html5game presentation

Director Settings

Page 71: Html5game presentation

Scene

Page 72: Html5game presentation

Blank Canvas

Page 73: Html5game presentation

Comments

Page 74: Html5game presentation

Game Background Sprite

Page 75: Html5game presentation

appendChild

Page 76: Html5game presentation

Create Analog Stick Sprite

Page 77: Html5game presentation

Preview Game

Page 78: Html5game presentation

Preview Game With Inspector

Page 79: Html5game presentation

Preview Game With Console

Page 80: Html5game presentation

Add Four Button Sprites

Page 81: Html5game presentation

Frog Constructor

Page 82: Html5game presentation

Frog Properties

Page 83: Html5game presentation

Require Frog

Page 84: Html5game presentation

> bin/lime.py update

Page 85: Html5game presentation

Constants

Page 86: Html5game presentation

appendChild Frog

Page 87: Html5game presentation

Game Logic: Init, Render

render

init

Page 88: Html5game presentation

Create Walls and Badges

Page 89: Html5game presentation

appendChild: Walls and Badges

Page 90: Html5game presentation

Game Preview

Page 91: Html5game presentation

Add Event for UP Button

Page 92: Html5game presentation

Frog: startMovement

Page 93: Html5game presentation

Game Logic: Schedule Manager

render

init update

Page 94: Html5game presentation

Schedule Manager

Page 95: Html5game presentation

Game Logic: Check Movement

update

render

?init

Page 96: Html5game presentation

Add Events to All Buttons

Page 97: Html5game presentation

checkMovement

Page 98: Html5game presentation

Game Logic: Check Movement

update

render

?init

Page 99: Html5game presentation
Page 100: Html5game presentation

Game Logic: Check Movement

update

render

?init

Page 101: Html5game presentation

Game Logic: Check for Obstacle

?

update

render

?init

Page 102: Html5game presentation

Determine all four corners

Page 103: Html5game presentation

Loop through all the walls

Page 104: Html5game presentation

Determine the wall’s corners

Page 105: Html5game presentation

If the frog’s right would be right of the wall’s left ...

Page 106: Html5game presentation

... and if the frog’s left would be left of the wall’s right ...

Page 107: Html5game presentation

... and if the frog’s top would be above the wall’s bottom ...

Page 108: Html5game presentation

... and if the frog’s bottom would be below the wall’s top ...

Page 109: Html5game presentation

... then the frog and the wall would overlap.

Page 110: Html5game presentation

Checking for a collision

Page 111: Html5game presentation

Early return on collision

Page 112: Html5game presentation

Game Logic: Check for Obstacle

?

update

render

?init

Page 113: Html5game presentation

Game Logic: Check for Obstacle

?

update

render

?init

Page 114: Html5game presentation
Page 115: Html5game presentation

Game Logic: Check for Obstacle

?

update

render

?init

Page 116: Html5game presentation

Game Logic: Check for Badge

?

update?

render

?init

Page 117: Html5game presentation

Keep track of badges earned

Page 118: Html5game presentation

Check for collision with badge

Page 119: Html5game presentation

Increment badges earned

Page 120: Html5game presentation
Page 121: Html5game presentation

Game Logic: Check for Badge

?

update?

render

?init

Page 122: Html5game presentation

Game Logic: Check for Badge

?

update?

render

?

+

init

Page 123: Html5game presentation

init

Game Logic: Check for Victory

?

update?

`

render

+

? ?

Page 124: Html5game presentation

Check for a victory

Page 125: Html5game presentation

Check for a victory

Page 126: Html5game presentation

init

Game Logic: Check for Victory

?

update?

`

render

+

? ?

Page 127: Html5game presentation

init

Game Logic: Reset on Victory

?

update?

`

render

+

??

Page 128: Html5game presentation

> bin/lime.py build treehouse -o treehouse/treehouse-min.js

Page 129: Html5game presentation

@randyhoyt

randyhoyt.com/launchgame