This is a simple jugging game I created for this week’s assignment. I wanted to replace the 2 circles with images of circus balls but I having problems. I also couldn’t get them to rotate.
I started with the Example code in Processing for BouncyBubbles and I built the game on top of that.
/* This stetch will produce a little juggling game */
//int numBalls = 2;
int numBalls = 3;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
int gameLength = 30; //how long the game will last in seconds
int timer; //time in seconds
int counter = 0;
int timedScore;
int savedTimeScore;
int ballsUpStartTime;
//int ballCounter = 0; //number of balls in the air
void setup() {
size(400, 700);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), height, random(20, 40), i, balls);
}
}
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display();
}
//set the countdown timer
timer = gameLength - int(millis()/1000);
text ("Time left: " + timer, 20, 10);
//set the ball counter = number of balls in the air
int ballCounter=0;
for (int i=0; i<numBalls; i++) {
if (balls[i].onTheFloor() == false) {
ballCounter++;
}
}
if (ballCounter == balls.length) {
//reset the time counter
println("All BALLS UP!");
//start timing
//timedScore += int(frameCount /60);
ballsUpStartTime = int(millis()/1000);
println("millis/1000 "+int(millis()/1000));
timedScore = int(millis() /1000) - ballsUpStartTime;
println("timedScore : "+timedScore);
}else { // if there's a ball on the ground, save the time
//check if the time is bigger that the other time
if (timedScore > savedTimeScore){
savedTimeScore = timedScore;
}
timedScore = 0;
}
text ("Balls in the Air: " + ballCounter, 10, 50);
if (timer == 0) {
noLoop();
background(0);
fill(150);
text("Your top score: "+ savedTimeScore, width/2, height/2);
}
}// end draw
void mousePressed() {
for (int i = 0; i < numBalls; i++) {
balls[i].collideWMouse(mouseX, mouseY);
}
}
class Ball {
float x, y;
float diameter = 50;
float vx = 0;
float vy = 0;
int id;
int ballRotation = 0;
int push = 2;//for the on-click function
Ball[] others;
PImage b;
boolean onTheFloor = true;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
id = idin;
others = oin;
b = loadImage("circusball.gif");
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void collideWMouse(int mousePosX, int mousePosY) {
if (mousePosX >x - diameter/2 && mousePosX < x + diameter/2 && mousePosY > y-diameter/2 && mousePosY < y + diameter/2) {
// println("You hit Ball #" + id);
// println("on the Floor: " +onTheFloor());
// println("Y: " +y);
vy = -(Math.abs((vy+1) + push));
}
}
void move() {
vy += gravity;
x += vx;
y += vy;
// this.rotate(ballRotation++);
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= 0;
fill(255, 0, 0);
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
fill(255, 204);
ellipse(x, y, diameter, diameter);
// image(b, x, y, diameter, diameter);
}
boolean onTheFloor() {
if (y + diameter/2 >= height) {
onTheFloor = true;
}
else {
onTheFloor = false;
}
return onTheFloor;
}
}//end Ball class