Finally, we import all the JavaScript code we need. The jQuery
5
library is a
very popular tool for creating dynamic web applications. It makes it very easy
to manipulate HTML elements, and it’ll make our life much easier.
You already know the
serial_device.js
and
game_controller.js
files. The
arduinoid.js
file
is more interesting because it contains the actual game logic. It starts with
the definition of a few data structures holding the game’s most important
status information:
BrowserGame/Arduinoid/js/arduinoid.js
const MAX_LIVES = 5;
var GameStates = {
RUNNING: 'running',
PAUSED: 'paused',
LOST: 'lost',
WON: 'won'
}
var Game = {
lives: MAX_LIVES,
score: 0,
state: GameStates.PAUSED,
paddle: {
speed: 15,
width: $("#paddle").width(),
height: $("#paddle").height()
},
playfield: {
width: $("#playfield").width(),
height: $("#playfield").height(),
rows: 4,
columns: 10
},
ball: {
diameter: $("#ball").width(),
vx: 5 + Math.random() * 5,
vy: -10
},
controller: new GameController('/dev/tty.usbmodem24321')
}
5.
http://jquery.com/
Chapter 7. Writing a Game for the Motion-Sensing Game Controller • 116
report erratum • discuss
www.it-ebooks.info