After watching some of the Coding Train’s (Dan Shiffman) YouTube tutorials on OOP, I took my inspiration for the game from this purple rain. Except that instead of the falling droplets I decided to make falling balls of random colors, and the whole trick of the game is to catch all of them with a net.
I started by creating a class for the ball object, along with the functions to initialize the objects, make them fall, create a net, and check if the ball is caught by the net.
One of the things that I was not too sure about is whether I should have had my net as a separate class or a part of the Ball class. I chose to go with the latter, because it allowed me to have functions inside of one class that checked the position and the catch:
void makeNet() {
fill(0);
rectMode(CENTER);
rect(posX, posY, netWidth, radiusX);
}
void checkNet() {
if (locX + radiusX >= posX
&& locX + radiusX <= posX + netWidth
&& locY + radiusY == posY) {
locX = -200;
score++;
}
}
My second challenge was to connect the keyPressed() function to the class for the net to move. I was a bit confused about whether to make it a part of the class functions or let it be a function outside the class, so I started to experiment. Being a part of the Ball class, the function did not work properly when I pressed the necessary keys, so I let it be outside the class. However, that chunk of code still seems a bit inefficient to me so far:
void keyPressed() {
if (keyPressed) {
for(int i = 0; i < balls.length; i++){
if (key == 'a'){
balls[i].posX -= 100;
}
if (key == 'd'){
balls[i].posX += 100;
}
}
}
}
One of my main intentions for this game was to display the score, which is calculated based on the number of balls caught, on the console. However, I still could not figure out the best way to do it:
print(balls[i].score);
There is room for improvement in this code, so I hope to figure out by the end of the week!
// Declare the object class
class Ball {
float radiusX, radiusY, locX, locY;
color ballColor;
int score;
float posX, posY;
float netWidth = 250;
// Constructor
Ball () {
radiusX = 50;
radiusY = 50;
locX = random(10, width-10);
locY = random(height);
ballColor = color(random(255), random(255), random(255));
posX = width/2;
posY = height-10;
}
// Make the object
void makeBall() {
fill(ballColor);
stroke(1);
ellipse(locX, locY, radiusX, radiusY);
}
// Updating location of the object
void falling() {
locY += 1;
if (locY > height+radiusY/2) {
locY = -radiusY*2;
}
}
// Make the net
void makeNet() {
fill(0);
rectMode(CENTER);
rect(posX, posY, netWidth, radiusX);
}
// Check if the object is in the net
void checkNet() {
if (locX + radiusX >= posX
&& locX + radiusX <= posX + netWidth
&& locY + radiusY == posY) {
locX = -200;
score++;
}
}
}
// Initialize an array of objects
Ball[] balls;
void setup(){
size(1280, 720);
// Put objects in the array
balls = new Ball[10];
for(int i = 0; i < balls.length; i++){
balls[i] = new Ball();
}
}
// Display and run the game
void draw(){
background(255);
for(int i = 0; i < balls.length; i++){
balls[i].makeBall();
balls[i].falling();
balls[i].makeNet();
balls[i].checkNet();
print(balls[i].score);
}
}
// Move the net if the keys are pressed
void keyPressed() {
if (keyPressed) {
for(int i = 0; i < balls.length; i++){
if (key == 'a'){
balls[i].posX -= 100;
}
if (key == 'd'){
balls[i].posX += 100;
}
}
}
}
You can watch the demo here: