Skip to content

Instantly share code, notes, and snippets.

@sabotai
Last active March 13, 2017 16:53
Show Gist options
  • Save sabotai/f6606fb1e9ce96cc4b529daa9fc48532 to your computer and use it in GitHub Desktop.
Save sabotai/f6606fb1e9ce96cc4b529daa9fc48532 to your computer and use it in GitHub Desktop.
int ballX, ballY;
int ballSize;
float xSpeed, ySpeed;
int rectX, rectY, rectW, rectH;
void setup() {
size(1280, 720);
ballX = 100;
ballY = 100;
ballSize = 50;
xSpeed = 5;
ySpeed = 5;
rectX = 300;
rectY = 250;
rectW = 200;
rectH = 100;
}
void draw() {
background(0);
ellipse(ballX, ballY, ballSize, ballSize);
rect(rectX, rectY, rectW, rectH);
ballX += xSpeed;
ballY += ySpeed;
if (ballY > height - ballSize/2) {
//reverse the speed... positive = down/right ... negative = up/left
ySpeed *= -1;
}
// if the ball crosses the right boundary... reverse the x direction
if (ballX > width - ballSize/2){
xSpeed *= -1;
}
//...reverse the y direction if the ball crosses the top boundary
if (ballY < 0 + ballSize/2){
ySpeed *= -1;
}
if (ballX < 0 + ballSize/2){
xSpeed *= -1;
}
if (//... the ball is to the left of the rect right side &&
//the ball is to the right of the rect left side &&
//the ball is below the rect top side &&
//the ball is above the rect bottom side...
ballY > rectY &&
ballY < rectY + rectH &&
ballX > rectX &&
ballX < rectX + rectW)
{
//make something happen...
background(200,50,50);
ballSize +=5;
}
if (//... the ball is to the left of the rect right side &&
//the ball is to the right of the rect left side &&
//the ball is below the rect top side &&
//the ball is above the rect bottom side...
mouseY > rectY &&
mouseY < rectY + rectH &&
mouseX > rectX &&
mouseX < rectX + rectW)
{
if (mousePressed){
ballSize -=1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment