We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The purpose of this project is to create a clone of the game found here. By clicking a group of two or more bubbles they should pop. I haven't started on making bubbles drop down when some are removed but my problem is the check() method in the bubble never completely runs through and results in the sketch crashing. If you see a problem or know of a better way to do this I would appreciate it.
//Variables for drawing bubbles
int bubbleDiameter = 25;
int bubbleOffset = bubbleDiameter/2 + 10;
//Colors set to names for readability
color blue = color(0, 204, 251);
color red = color(254, 42, 2);
color green = color(47, 252, 25);
color yellow = color(254, 236, 76);
color pink = color(250, 1, 138);
//Arrays conataining properties of each bubble
int bubbleColor[][] = new int[20][25]; //Contains an int corresponding to the bubble's color
boolean bubbleHover[][] = new boolean[20][25]; //When mouse is hovering above bubble this is true
Bubble bubble[][] = new Bubble[20][25];
void setup() {
size(520, 700); //Create window
colorGen();
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
bubble[x][y] = new Bubble(x, y);
}
}
}
void draw() {
background(192, 192, 255);
scoreBar();
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
bubble[x][y].drawBubble();
}
}
checkHover();
}
class Bubble {
boolean living;
int xPos;
int yPos;
int col;
//Bubble class
//Constructor for bubble
public Bubble(int x, int y) {
this.living = true; //State of bubble
this.xPos = x; //Gives the x position of the bubble in the array
this.yPos= y; //Gives the y position of the bubble in the array
this.col = bubbleColor[x][y]; //Sets color value to that in the array generated by colorGen()
}
//
void killBubble() {
this.living = false;
this.col = 5;
}
void check() {
boolean hasNeighbors = false;
print("Right");
try {
if(this.col == bubble[this.xPos+1][this.yPos].col) {
hasNeighbors = true;
bubble[this.xPos+1][this.yPos].check();
}
}
catch(Exception border) {}
print("Left");
try {
if(this.col == bubble[this.xPos-1][this.yPos].col) {
hasNeighbors = true;
bubble[this.xPos-1][this.yPos].check();
}
}
catch(Exception border) {}
print("Top");
try {
if(this.col == bubble[this.xPos][this.yPos+1].col) {
hasNeighbors = true;
bubble[this.xPos][this.yPos+1].check();
}
}
catch(Exception border) {}
print("Bottom");
try {
if(this.col == bubble[this.xPos][this.yPos-1].col) {
hasNeighbors = true;
bubble[this.xPos][this.yPos-1].check();
}
}
catch(Exception border) {}
print("If");
if(hasNeighbors) {
this.killBubble();
}
print("Check complete");
}
void drawBubble() {
if(this.living) {
//Colors bubble based on color value from array
if(this.col == 0) {
fill(red);
}
else if(this.col == 1) {
fill(blue);
}
else if(this.col == 2) {
fill(green);
}
else if(this.col == 3) {
fill(yellow);
}
else if(this.col == 4) {
fill(pink);
}
//Draws bubble in grid
ellipse(bubbleOffset + xPos*bubbleDiameter, bubbleOffset + yPos*bubbleDiameter, bubbleDiameter, bubbleDiameter);
}
}
}
//Runs when mouse is clicked
void mouseClicked() {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
if(bubbleHover[x][y] == true) { //Checks which bubble the mouse is over when clicked
try {
bubble[x][y].check();
}
catch(Exception e) {}
}
}
}
}
//Checks if mouseX and mouseY fall within a certain bubble's area
void checkHover() {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
if(mouseX > bubbleOffset + x*bubbleDiameter - bubbleDiameter/2 && mouseX < bubbleOffset + x*bubbleDiameter + bubbleDiameter/2
&& mouseY > bubbleOffset + y*bubbleDiameter - bubbleDiameter/2 && mouseY < bubbleOffset + y*bubbleDiameter + bubbleDiameter/2) {
bubbleHover[x][y] = true; //Sets hover to true if locations intersect
}
else {
bubbleHover[x][y] = false; //Sets hover to false if there is no intersection
}
}
}
}
//Generates random colors and assigns them to positions in an array
void colorGen() {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 25; y++) {
bubbleColor[x][y] = floor(random(5));
}
}
}
//Creates score box
void scoreBar() {
textSize(32);
fill(pink);
text("Score:", 10, 675);
text("Time:", width/2, 675);
}
Answers
http://forum.processing.org/two/discussion/comment/46456/#Comment_46456