EasyManuals Logo

Arduino Pro Mini User Manual

Arduino Pro Mini
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #140 background imageLoading...
Page #140 background image
Eventually, we set the balls position to the new values in line 30.
Moving the paddle is similar, but it depends on the current state of the game
controller. If the player wants the paddle to move left, we subtract the paddles
current speed from the paddles x-coordinate. We also make sure that the
paddle doesnt leave the screen. Movement to the right works nearly the same.
We only have to add the paddles current speed.
A difficult problem in video games is collision detection. Youve probably
played a game or two and yelled No, that thing didnt hit me! or Im sure I
killed that alien first! In most cases inexact collision detection is the cause
of your frustration.
Even for our simple game, exact collision detections arent easy. The blocks
have rounded corners, so checking whether the ball overlaps one of the corners
or has actually touched the block isnt trivial. For a good game experience
this isnt necessary, so Ive simplified the collision detection.
BrowserGame/Arduinoid/js/arduinoid.js
function checkCollisions() {
Line 1
if (ballDropped()) {
-
Game.lives = Game.lives - 1;
-
if (Game.lives == 0) {
-
Game.state = GameStates.LOST;
5
} else {
-
Game.state = GameStates.PAUSED;
-
resetMovingObjects();
-
}
-
}
10
if (!checkBlockCollision()) {
-
Game.state = GameStates.WON;
-
}
-
}
-
15
function ballDropped() {
-
var ball_y = $("#ball").position().top;
-
var paddle_y = $("#paddle").position().top;
-
return ball_y + Game.ball.diameter > paddle_y + Game.paddle.height;
-
}
20
-
function inXRange(ball_left, block_left, block_width) {
-
return (ball_left + Game.ball.diameter >= block_left) &&
-
(ball_left <= block_left + block_width);
-
}
25
-
function inYRange(ball_top, block_top, block_height) {
-
return (ball_top + Game.ball.diameter >= block_top) &&
-
(ball_top <= block_top + block_height);
-
}
30
Chapter 7. Writing a Game for the Motion-Sensing Game Controller 122
report erratum discuss
www.it-ebooks.info

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino Pro Mini and is the answer not in the manual?

Arduino Pro Mini Specifications

General IconGeneral
BrandArduino
ModelPro Mini
CategoryMotherboard
LanguageEnglish

Related product manuals