function handleMessageState(message) {
$("#" + message).show();
if (Game.controller.buttonPressed) {
$("#" + message).hide();
initGame();
}
}
The
gameLoop
function is surprisingly simple, because it only checks the game’s
current state and then delegates its work accordingly. If the game is paused
currently, it checks whether the player has pressed the game controller’s
button. If yes, it changes the game’s state to
GameStates.RUNNING
.
If the game is running already,
gameLoop
moves all game objects, checks for
potential collisions, and updates the game’s statistics. If the game was won
or lost, it calls
handleMessageState
to display a corresponding message.
handleMessageState
displays a message by manipulating an HTML element’s
content. It also checks whether the game controller’s button was pressed. If
yes, it hides the message and initializes the game so the player can start a
new round. After a player has won or lost a game, he or she can start a new
game by pressing the button on the game controller.
Moving the objects on the screen is the most important part in many video
games. Thanks to jQuery, it’s not that difficult.
BrowserGame/Arduinoid/js/arduinoid.js
function moveBall() {
Line 1
var ball_pos = $("#ball").position();
-
var ball_x = ball_pos.left;
-
var ball_y = ball_pos.top;
-
var next_x_pos = ball_x + Game.ball.vx;
5
var next_y_pos = ball_y + Game.ball.vy;
-
-
if (next_x_pos <= 0) {
-
Game.ball.vx *= -1;
-
next_x_pos = 1;
10
} else if (next_x_pos >= Game.playfield.width - Game.ball.diameter) {
-
Game.ball.vx *= -1;
-
next_x_pos = Game.playfield.width - Game.ball.diameter - 1;
-
}
-
15
var paddle_y = $("#paddle").position().top;
-
if (next_y_pos <= 0) {
-
Game.ball.vy *= -1;
-
next_y_pos = 1;
-
} else if (next_y_pos + Game.ball.diameter >= paddle_y) {
20
var paddle_x = $("#paddle").position().left;
-
Chapter 7. Writing a Game for the Motion-Sensing Game Controller • 120
report erratum • discuss
www.it-ebooks.info