Random boxes and circles using array(s)

Here is my incomplete attempt to demonstrate how to implement coins. For what you want to do, you should consider learning OOP as it will allow you to track related variables together under an abstraction called an object. The main concept you want to use from oop in this case would be encapsulation. Before you try to figure out ArrayList, you need to isolate and understand the Coin class. I have added an abstract class as it can be used to implement your bomb objects in your game as well, as I demonstrated below. Notice the bomb class is almost fully implemented, but some work still needs to be done there.

Finally, notice that the coins show in the wrong place. It is an easy fix and it will be your task to complete it.

I strongly recommend you to visit this guide about objects as it will resolve most of your immediate questions about the code below.

Kf

final int NCOINS=5;
final color GOLD_COLOR=color(220,220,15);
final int FLOOR_HEIGHT=285;
final int CEIL_HEIGHT=88;


int x;
int y=226;
boolean play = true;


ArrayList<Coin> allCoins;

void setup() {
  size(800, 400);
  background(0);
  
  allCoins = new ArrayList<Coin>();
  
  for(int i=0;i<NCOINS;i++){
    Coin aCoin=new Coin(random(width),CEIL_HEIGHT+40,GOLD_COLOR);  //40 to account for stone and coin dimensions
    allCoins.add(aCoin);    
  }
}

void draw() {
  cave();
  stone();
  clumsy();
  bomb();
  coins();
}

void cave() {
  noStroke();
  fill(160);
  rect(0, 88, width, 200);
}

void stone() {
  color c[] = new color[3];
  int n=0;
  c[0]=color(255, 0, 0); // first color
  c[1]=color(0, 255, 0); // second
  c[2]=color(0, 0, 255); // third
  for (int i=0; i<width; i+=30) {
    strokeWeight(2);
    stroke(0);
    fill(c[n]);
    rect(i, FLOOR_HEIGHT, 30, 30);
    rect(i, CEIL_HEIGHT, 30, 30);
    n++;
    if (n>2) n=0;
  }
}

void clumsy() {
  strokeWeight(1);
  fill(0);
  ellipse(x+30, y-5, 80, 45);
  fill(#F3F156);
  rect(x, y, 58, 58);
  fill(#90F614);
  rect(x+5, y+10, 10, 10);
  rect(x+40, y+10, 10, 10);
  fill(#EF1A1A);
  rect(x+15, y+30, 25, 15);
  if (play) {
    x++;
  }
  if (x>width) {
    x=0;
  }
}

void keyPressed() {

  if (key == CODED) {
    if (keyCode == UP) {
      if (y == 226) {
        y-= 100;
      }
    }
  }
  if (keyCode == DOWN) {
    if (y == 126) {
      y+= 100;
    }
  }

  if (keyCode == ' ') {
    play = !play;
  }
}


void bomb() {
}

void coins() {
  
  for(Coin x: allCoins){
    x.draw();
  }
}


// https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
public abstract class MasterObject {

  protected color c;
  protected PVector pos;
  abstract void draw();
  abstract void update();
}


class Coin extends MasterObject {
  
  float radius;

  //Constructor
  Coin( float x, float y, color col) {
    c=col;
    pos =new PVector(x, y);
    radius=10;
  }

  //Mandatory implementation: draw
  void draw() {
    fill(c);
    ellipse(pos.x,pos.y, radius*2,radius*2);
  }

  //Mandatory implementation: update
  void update() {
  }
}


//NEXT class is incomplete and needs to be fully implemented
class Bomb extends MasterObject {
  
  float len;  //Length pf side of square

  //Constructor
  Bomb( float x, float y, color col) {
    c=col;
    pos =new PVector(x, y);
  }

  //Mandatory implementation: draw
  void draw() {
    
    //HERE draw a square using the rect() function. Read documentation for proper implementation
  }

  //Mandatory implementation: update
  void update() {
  }
}
3 Likes