Labels

Thursday, 17 May 2012

Key Functions

I have now added key function to move the ball around. 
The key pad up,down,left and right move the ball the way of the key being pressed. 
If you click now it sends the ball back into the bouncing wall code.



int size = 60;
float xpos, ypos;
float speed = 4;
float xspeed = 0;
float yspeed = 0;


int xdirection = 1;
int ydirection = 1;
int twosize = 30;
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(690, height-340, 5, 200);
}
void mouseball() {
  fill(255);
  smooth();
  ellipse(mouseX, mouseY, 5, 5);
}
void soccerball() {
  fill(255);
  smooth();
  ellipse(xpos+twosize, ypos+twosize, 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 goalscore() {
  if (twosize == width - 40) {
    twosize = width/2;
  }
}








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


  if (keyPressed) {
    if (key == CODED) {
      if (keyCode == LEFT) {
        xpos -=speed;
      }
      if (keyCode == RIGHT) {
        xpos += speed;
      }
      if (keyCode == UP) {
        ypos -=speed;
      }
      if (keyCode == DOWN) {
        ypos +=speed;
      }
    }
  }


  if (mousePressed == true) {
    xspeed = 4;
  }
  if (mousePressed == true) {
    yspeed = 5;
  }
}

No comments:

Post a Comment