Friday, 4 March 2011

_05_If_Statements

int batX = 240;
int batY = 700;


//Here we go again
void setup() {
  size( 480, 800 );
  background( 0 );
}

//This time the white rectangle is going to follow the mouse
//but only side-to-side so the code sets the value of 'batX' to
//equal the built in variable 'mouseX'.  Also, each time Processing draws a
//frame we check the current value of 'batX'.  If the bat is going over
//the side of the window, we can just change the value of 'batX' to
//put it back on the screen before we actually draw it.  This way it looks
//as if the bat is bumping up against the edge of the window.
void draw() {
  background( 0 );
  fill( 255 );
 
  batX = mouseX;
 
  //if the bat 'hits' the left edge move it back
  if( batX < 50 ) {
    batX = 50;
  }
 
  //if the bat 'hits' the right edge move it
  if( batX > 430 ) {
    batX = 430;
  }
 
  rect( batX - 50, batY, 100, 25 );
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.