So, initially for this week’s assignment, I wanted to replicate my childhood game, which was called the Brick Breaker, as I really loved this game and played it almost everyday since I was small with my mom’s old Nokia phone.
Initially, I sort of drew a sketch for myself to better follow along and code, as shown below. (Excuse the poor quality)
At first, I had no difficulty creating my user bar, the bouncing balls, and the blocks at the top. However, the main obstacle for me was being able to make the balls to be able to bounce off my user bar and also the rectangles (and causing them to disappear upon contact). I tried searching up tutorials, videos, websites, and even other online projects made before in order to help me understand collision detection and how to code it. However, in the end, I still wasn’t able to code the collision detection for my ellipses because I could not quite grasp and fully understand the mechanics behind coding for interactions between two different classes, which in this case would be the ellipses with the user bar and the rectangles. Furthermore, you will notice a lot of hardcoding because I wasn’t quite sure how to use arrays yet and I did not want to mess with something I wasn’t familiar with yet.
So, in the end, I decided to just add a bit more ellipses to make it look less boring as it did.
/***************MAIN SKETCH **************/ int x; int y; int blockx; int blocky; int rectWidth=50; int rectHeight=50; Bar userbar; RandomBlocks blocks; RandomBlocks blocks2; RandomBlocks blocks3; RandomBlocks blocks4; BouncingBall ball; BouncingBall ball2; BouncingBall ball3; BouncingBall ball4; BouncingBall ball5; void setup() { size(1280, 720); userbar= new Bar(); //calling for a constructor blocks= new RandomBlocks();//constructor always needs two paranthesis () ball= new BouncingBall(random(width), random(height), 50); ball2= new BouncingBall(random(width),random(height),50); ball3= new BouncingBall(random(width),random(height),50);//ALL OF THIS IS HARDCODING SORRY ball4= new BouncingBall(random(width),random(height),50); ball5= new BouncingBall(random(width),random(height),50); blocks2= new RandomBlocks(); blocks3= new RandomBlocks(); blocks4= new RandomBlocks(); } void draw() { background(255); userbar.drawbar(); //using dot syntax, it calls a function for the object to do sth built by constructor //Random blocks at the top blocks.drawblock(); blocks2.drawblock2(); blocks3.drawblock3(); blocks4.drawblock4(); //All the five balls ball.bounceBall(); ball2.bounceBall(); ball3.bounceBall(); ball4.bounceBall(); ball5.bounceBall(); } /***********Code for the controllable user blue bar***********/ class Bar { Bar() { //this is a constructor with paranthesis () // in a constructor, it doesn't necessarily have to contain anything inside it, it just represents an object } void drawbar(){ stroke(255, 0, 0); fill(0, 0, 255); rect(mouseX-250, height-50, 300, 20); } } /************Code for the five ellipses***************/ class BouncingBall { //float ballx=random(width); //float bally=random(height); float diameter; float xspeed; float yspeed; float x; float y; color c; BouncingBall(float tempX, float tempY, float tempDiameter) {//constructor x=tempX; y=tempY; diameter=tempDiameter; xspeed=10; yspeed=10; } void bounceBall() { stroke(0); fill(211, 123, 231); ellipse(x, y, 50,50); if (x>=width || x<=0) { xspeed*=-1; } if (y>=height || y<=0) { yspeed*=-1; } x=x+xspeed; y=y+yspeed; } } /************Code for the rows of shiny blocks at the top**********/ class RandomBlocks {//for a class, the first letter has to be capitalized RandomBlocks() {//a constructor } void drawblock() { for (x=0; x<width; x+=rectWidth+50) { stroke(0); fill(random(255), random(255), random(255)); rect(x, y, rectWidth, rectHeight); } } void drawblock2() { for (x=0; x<width; x+=rectWidth+50) { stroke(0); fill(random(255), random(255), random(255)); rect(x+50, y+60, rectWidth, rectHeight); } } void drawblock3() { for (x=0; x<width; x+=rectWidth+50) { stroke(0); fill(random(255), random(255), random(255)); rect(x+100, y+120, rectWidth, rectHeight); } } void drawblock4() { for (x=0; x<width; x+=rectWidth+50) { stroke(0); fill(random(255), random(255), random(255)); rect(x+50, y+180, rectWidth, rectHeight); } } }