Labels

Thursday, 17 May 2012

Bouncing Ball


I have now made the code for the ball bouncing around the screen and bouncing off the edges. 
Now to add interactivity 




int size = 60;
float xpos, ypos;


float xspeed = 4;
float yspeed = 5;


int xdirection = 1;
int ydirection = 1;


void setup() {  
  rect(width/2, height, 4, height);
  moveball();
  soccergoal();
  mouseball();
  soccerball();
  size(750, 500);
  noStroke();
  frameRate(35);
  smooth();
  xpos = width/2;   //starting positions
  ypos = height/2;
}




void soccergoal() {
  fill(255);
  rect(710, height-340, 5, 200);
}
void mouseball() {
  fill(255);
  smooth();
  ellipse(mouseX, mouseY, 5, 5);
}
void soccerball() {
  fill(255);
  smooth();
  ellipse(xpos+size/2, ypos+size/2, size, size);

}
void moveball() {
  xpos = xpos + (xspeed * xdirection);
  ypos = ypos + (yspeed * ydirection);


  if ( xpos > width-size || xpos < 0) {
    xdirection *= -1;
  }
  if (ypos > height-size || ypos < 0) {
    ydirection *= -1;
  }
}




void draw() {
  println(ypos);
  background(0);
  //goalnet();
  //goalscore();
  moveball();
  soccergoal();
  soccerball();
  mouseball();
  smooth();

No comments:

Post a Comment