+ All Categories
Home > Education > Browser based games

Browser based games

Date post: 14-Aug-2015
Category:
Upload: matthew-leach
View: 43 times
Download: 0 times
Share this document with a friend
15
Browser-Based Games Dr. Matthew Leach
Transcript
Page 1: Browser based games

Browser-Based Games

Dr. Matthew Leach

Page 2: Browser based games

thoughtfulmonkey.com/westnotts

Page 3: Browser based games

Learning Outcomes

• Learn the main code needed to run a browser-based game.

• Gain experience in rapid prototyping a game to provide scalable challenge.

• BTEC: Unit 39: LO1.1 / 2.1 / 2.3

Page 4: Browser based games

JSFiddle

• Rapid prototyping platform

• HTML, CSS, JavaScript

• Look for // *** TODO

Page 5: Browser based games

HTML Canvas (briefly)

<canvas id="gamebox" width=500 height=400></canvas>

gamebox = document.getElementById("gamebox");

ctx = gamebox.getContext("2d");

ctx.beginPath();

ctx.arc(x, y, 3, 0, 2 * Math.PI, false);

ctx.fillStyle = '#00DD00';

ctx.fill();

Page 6: Browser based games

HTML Canvas (briefly)

<canvas id="gamebox" width=500 height=400></canvas>

gamebox = document.getElementById("gamebox");

ctx = gamebox.getContext("2d");

ctx.beginPath();

ctx.arc(x, y, 3, 0, 2 * Math.PI, false);

ctx.fillStyle = '#00DD00';

ctx.fill();

Get reference to do 2D drawing

Draw something

HTML element on the page

Page 7: Browser based games

Script Structure

Variables

Game Setup

levelSetup()

update()

keyHandler( e )

gameOver()

Page 8: Browser based games

Call the Setup Function

// *** TODO: Perform level setup

levelSetup();

Page 9: Browser based games

Starting a Main Game Loop

// *** TODO: Start game loopintervalID = setInterval(update, 1000 / 16);

Page 10: Browser based games

Starting a Main Game Loop

// *** TODO: Start game loopintervalID = setInterval(update, 1000 / 16);

Built-in JavaScript function

Function to act as game loop

Interval between game loops

Reference to the timer

Page 11: Browser based games

Stopping the Game Loop

// *** TODO: Stop the game loopclearInterval(intervalID);

Page 12: Browser based games

User Input

// *** TODO: Add keyboard listenerdocument.addEventListener(

"keydown", keyHandler, false);

Page 13: Browser based games

Test the Game

• What’s broken?

• Where’s the challenge?

Page 14: Browser based games

Design Improvements

• Work in pairs

• Design 2 improvements–One scalable challenge–One new feature

Page 15: Browser based games

Summary

• Key JavaScript:–setInterval–clearInterval–addEventListener

• Identify opportunities to add challenge


Recommended