Friday, 4 March 2011

_09_BetterGamePlay

int batX = 240;
int batY = 700;

int ballX = 240;
int ballY = 400;

int ballXspeed = 5;
int ballYspeed = -5;

//So far we have put all the sizes of
//the bat and the ball straight into the code
//where they are being used.  There are at least
//two problems with this.  First it means that
//we have to enter the same number in lots of
//places in the code so it gets really annoying to
//change the size of the bat and the ball.  Secondly
//having meaningless numbers all over the code makes
//it hard to read because there is no clue about what
//the number represents.
//
//In this version of the code, we are going
//to replace the 'hard coded' numbers with 'constants'
//which are values a bit like variable except that they
//never change during the program.  You can make a value
//constant by putting the word 'final' in front of its
//definition.  Now we can replace all the numbers in
//the code with the constant name which solves both
//problems: the code is much easier to read because
//it has meaningful words instead of raw numbers in it,
//and now there is only one place we have to change a
//value to make changes to the size of the bat, ball or
//screen.  Look through the code to find all the places
//where each constant is used, then try changing the
//values to see how easy it is to make things different
//sizes.
final int ballSize = 10;
final int screenWidth = 480;
final int screenHeight = 800;

final int batSize = 50;
final int batHeight = 25;

//boolean gamePlaying
//
//This is a new variable.  Its 'type' is boolean
//which means it can have the value 'true' or the
//value 'false'.  It is used in the draw() function
//to decide when to show the game over screen.
//A variable which can switch from true to false
//is often called a 'flag' in programming speak.
boolean gamePlaying = true;

//===================================
void setup() {
  size( screenWidth, screenHeight );
  background( 0 );
  noCursor();
}

//
//The draw() function is nice and simple
//====================================
void draw() {
  background( 0 );
 
  if( gamePlaying ) {
    moveTheBat();
    moveTheBall();
  } else {
    gameOver();
  }
}



//====================================
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.