Saturday, November 8, 2014

Boucing Ball


I recently had an assignment where I had to create  a ball that is aware of the sides of the screen and bounces. In order to complete this assignment I used what I learned about int's and  if statements. In order to get he balls to move but not further than the width I used this code:

if(bballx > width){
    bballx=width;
    movex=-movex;
    
  }

This code allowed me to control how far past the width of the screen the ball went. I used this same code except I removed the width and placed it on  either height or 0 and I changed bball x and movex with bbally and movey depending on what part of the screen I was dealing with. The full code is down below.

//Original Position of my variables
 int bballx=400;
 int bbally=300;

//How much the balls should move
 int movex=6;
 int movey=10;


void setup(){
 size(760,760);
 background(#CD00FC);

 strokeWeight(4);
  
}

void draw(){
  background(#CD00FC);
  stroke(#00FCD0);
  strokeWeight(4);
  fill(#3E00FC);
  ellipse(bballx,bbally,100,100);
  bballx = bballx + movex;
  bbally = bbally + movey;
 
  // (Too far right)
  if(bballx > width){
    bballx=width;
    movex=-movex;
    
  }
// (Too far left)
  if(bballx < 0){
    bballx=0 ;
    movex = -movex;
 
}
  //(Too far down)
  if(bbally > height){
    bbally=height;
    movey = -movey;

  }
   //(Too far up)
  if(bbally < 0){
    bbally=0;
    movey = -movey;
  }   
}
 
“Make everything as simple as possible, but not simpler.”
– Albert Einstein








No comments:

Post a Comment