int batX = 240;
int batY = 700;
int ballX = 240;
int ballY = 400;
int ballXspeed = 5;
int ballYspeed = -5;
//
final int ballSize = 10;
final int screenWidth = 480;
final int screenHeight = 800;
final int batSize = 50;
final int batHeight = 25;
//
boolean gamePlaying = true;
//
final int blocksPerRow = 10;
final int blockWidth = screenWidth / blocksPerRow;
final int blockHeight = 20;
//===================================
void setup() {
size( screenWidth, screenHeight );
background( 0 );
noCursor();
}
//
//The draw() function is nice and simple
//====================================
void draw() {
background( 0 );
if( gamePlaying ) {
drawBlocks();
moveTheBat();
moveTheBall();
} else {
gameOver();
}
}
//void drawBlocks()
//
//This function introduces a few new programming
//ideas. The first one is quite simple. You can
//have variables _inside_ a function; called 'local'
//variables, these values are 'trapped' inside the
//function and can't be used anywhere outside it, so
//the names 'counter' and 'rowPosition' only exist
//in the drawBlocks() function. You can store any
//number you like in them but when the function ends,
//they are forgotten.
//
//The second idea is the 'for' loop. In programming,
//you almost always have to do something over and over
//again. They way you can repeat a piece of code is to
//put it inside a loop.
//
//A loop is just a block of code plus a counter variable.
//The block of code runs over and over until the counter
//reaches a certain value.
//
//The 'for' loop has four separate parts:
//
// for( ONE; TWO; THREE ) {
// FOUR;
// }
//
//ONE: this is the bit that sets the _starting_ value for
//the counter.
//TWO: this is the bit that checks if the counter has reached
//the final value (it is a lot like the code in an 'if' test
//THREE: this bit makes the counter count up (or down).
//FOUR: this is the block of code which gets repeated however
//many times the loop goes round.
//==================================//
void drawBlocks() {
int counter;
int rowPosition = 50;
fill( 200 );
// The parts of a for loop:
// | ONE | TWO | THREE | //
for( counter = 0; counter < 4; counter = counter + 1 ) {
// FOUR
drawOneRow( rowPosition );
rowPosition = rowPosition + blockHeight;
}
}
//===================================//
void drawOneRow( int rowLevel ) {
int counter;
for( counter = 0; counter < blocksPerRow; counter = counter + 1 ) {
rect( blockWidth * counter + 1, rowLevel + 1, blockWidth - 2, blockHeight - 2 );
}
}
//====================================
void moveTheBat() {
fill( 255 );
batX = mouseX;
//if the bat 'hits' the left edge move it back
if( batX < batSize ) {
batX = batSize;
}
//if the bat 'hits' the right edge move it
if( batX > screenWidth - batSize ) {
batX = screenWidth - batSize;
}
rect( batX - batSize, batY, batSize * 2, batHeight );
}
//=======================================
void moveTheBall() {
ballX = ballX + ballXspeed;
ballY = ballY + ballYspeed;
if( ballX < ballSize ) {
ballX = ballSize;
ballXspeed = ballXspeed * -1;
}
if( ballX > screenWidth - ballSize ) {
ballX = screenWidth - ballSize;
ballXspeed = ballXspeed * -1;
}
if( ballY < ballSize ) {
ballY = ballSize;
ballYspeed = ballYspeed * -1;
}
if( ballHitBat() ) {
ballYspeed = ballYspeed * -1;
}
if( ballY > screenHeight ) {
gamePlaying = false;
}
rect( ballX - ballSize, ballY - ballSize, ballSize * 2, ballSize * 2 );
}
//======================================
void gameOver() {
fill( 255 );
text("Game Over", 200, 400);
}
//======================================
boolean ballHitBat() {
if( ballY + ballSize >= batY ) {
if( ballY + ballSize <= batY + ballSize * 2 ) {
if( ballX > batX - batSize ) {
if( ballX < batX + batSize ) {
//the ball has hit the bat so return the answer is 'true'
return true;
}
}
}
}
//if we get this far then the ball hasn't hit, return the answer 'false'
return false;
}
int batY = 700;
int ballX = 240;
int ballY = 400;
int ballXspeed = 5;
int ballYspeed = -5;
//
final int ballSize = 10;
final int screenWidth = 480;
final int screenHeight = 800;
final int batSize = 50;
final int batHeight = 25;
//
boolean gamePlaying = true;
//
final int blocksPerRow = 10;
final int blockWidth = screenWidth / blocksPerRow;
final int blockHeight = 20;
//===================================
void setup() {
size( screenWidth, screenHeight );
background( 0 );
noCursor();
}
//
//The draw() function is nice and simple
//====================================
void draw() {
background( 0 );
if( gamePlaying ) {
drawBlocks();
moveTheBat();
moveTheBall();
} else {
gameOver();
}
}
//void drawBlocks()
//
//This function introduces a few new programming
//ideas. The first one is quite simple. You can
//have variables _inside_ a function; called 'local'
//variables, these values are 'trapped' inside the
//function and can't be used anywhere outside it, so
//the names 'counter' and 'rowPosition' only exist
//in the drawBlocks() function. You can store any
//number you like in them but when the function ends,
//they are forgotten.
//
//The second idea is the 'for' loop. In programming,
//you almost always have to do something over and over
//again. They way you can repeat a piece of code is to
//put it inside a loop.
//
//A loop is just a block of code plus a counter variable.
//The block of code runs over and over until the counter
//reaches a certain value.
//
//The 'for' loop has four separate parts:
//
// for( ONE; TWO; THREE ) {
// FOUR;
// }
//
//ONE: this is the bit that sets the _starting_ value for
//the counter.
//TWO: this is the bit that checks if the counter has reached
//the final value (it is a lot like the code in an 'if' test
//THREE: this bit makes the counter count up (or down).
//FOUR: this is the block of code which gets repeated however
//many times the loop goes round.
//==================================//
void drawBlocks() {
int counter;
int rowPosition = 50;
fill( 200 );
// The parts of a for loop:
// | ONE | TWO | THREE | //
for( counter = 0; counter < 4; counter = counter + 1 ) {
// FOUR
drawOneRow( rowPosition );
rowPosition = rowPosition + blockHeight;
}
}
//===================================//
void drawOneRow( int rowLevel ) {
int counter;
for( counter = 0; counter < blocksPerRow; counter = counter + 1 ) {
rect( blockWidth * counter + 1, rowLevel + 1, blockWidth - 2, blockHeight - 2 );
}
}
//====================================
void moveTheBat() {
fill( 255 );
batX = mouseX;
//if the bat 'hits' the left edge move it back
if( batX < batSize ) {
batX = batSize;
}
//if the bat 'hits' the right edge move it
if( batX > screenWidth - batSize ) {
batX = screenWidth - batSize;
}
rect( batX - batSize, batY, batSize * 2, batHeight );
}
//=======================================
void moveTheBall() {
ballX = ballX + ballXspeed;
ballY = ballY + ballYspeed;
if( ballX < ballSize ) {
ballX = ballSize;
ballXspeed = ballXspeed * -1;
}
if( ballX > screenWidth - ballSize ) {
ballX = screenWidth - ballSize;
ballXspeed = ballXspeed * -1;
}
if( ballY < ballSize ) {
ballY = ballSize;
ballYspeed = ballYspeed * -1;
}
if( ballHitBat() ) {
ballYspeed = ballYspeed * -1;
}
if( ballY > screenHeight ) {
gamePlaying = false;
}
rect( ballX - ballSize, ballY - ballSize, ballSize * 2, ballSize * 2 );
}
//======================================
void gameOver() {
fill( 255 );
text("Game Over", 200, 400);
}
//======================================
boolean ballHitBat() {
if( ballY + ballSize >= batY ) {
if( ballY + ballSize <= batY + ballSize * 2 ) {
if( ballX > batX - batSize ) {
if( ballX < batX + batSize ) {
//the ball has hit the bat so return the answer is 'true'
return true;
}
}
}
}
//if we get this far then the ball hasn't hit, return the answer 'false'
return false;
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.