b. Is there a way to prevent that? Random seems the easiest way to use in this situation.
Do i have to make another If statement that prevents this?
However this is not a big issue at the moment as I am capping it at 10 mines drawn.
My code is fairly long for this project as it has not been optimized and still uses a lot of hard values.
The game goes to the main game by button during the start menu.
Frame
final int GAMEBEGIN = 1;
final int GAMEPLAYING = 2;
final int GAME_END = 3;
int gamestatus = GAMEBEGIN;
void setup() {
size (1200, 800);
ellipseMode(CORNER);
drawmines();
set_treasures();
}
void draw() {
gamestatus();
}
void gamestatus() {
if (gamestatus == GAMEBEGIN) {
gamestart();
} else if (gamestatus == GAMEPLAYING) {
gameplaying();
} else if (gamestatus == GAME_END) {
//game_end();
}
}
void mousePressed() {
to_maingame();
add_decmines();
add_dectreasures();
}
void keyPressed() {
movementdiver();
}
Start menu
int mines = 1;
int treasures = 1;
final int yloc = 200;
final int buttonsize = 50;
void gamestart() {
background(255);
drawsquares();
drawtext();
drawbuttons();
}
void drawsquares() {
int xrectsize = 150;
int yrectsize = 100;
int ysquare = height/6;
float xsquare = width / 1.8;
fill(#1144ff);
drawsquare(width / 2.5, height - 50, xrectsize, yrectsize);
drawsquare(xsquare, ysquare, xrectsize + 50, yrectsize - 50);
drawsquare(xsquare / 2, ysquare, xrectsize + 50, yrectsize - 50);
}
void drawsquare(float xlocation, int location, int xsize, int ysize) {
rect(xlocation, location, xsize, ysize);
}
void drawtext() {
int textheight = height/5;
fill(0);
textSize(18);
drawtext("Start game", width / 2.35, height - 20);
drawtext("Diving for treasure", width/2.5, height/10);
drawtext("Mines = " + mines, width / 3.4, textheight);
drawtext("Treasures = " + treasures, width / 1.78, textheight);
}
void drawtext(String text, float xtext, float ytext) {
text(text, xtext, ytext);
}
void drawbuttons() {
fill(0);
drawbutton(690, yloc, buttonsize);
drawbutton(790, yloc, buttonsize);
drawbutton(360, yloc, buttonsize);
drawbutton(460, yloc, buttonsize);
}
void drawbutton(int xloc, int yloc, int buttonsize) {
rect(xloc, yloc, buttonsize, buttonsize);
}
void add_decmines() {
if (mouseX > 360 && mouseX < 360 + buttonsize && mouseY > yloc && mouseY < yloc + buttonsize) {
mines = mines - 1;
}
if (mouseX > 460 && mouseX < 460 + buttonsize && mouseY > yloc && mouseY < yloc + buttonsize) {
mines = mines + 1;
}
if (mines < 0) {
mines = 0;
}
if (mines > 10) {
mines = 10;
}
}
void add_dectreasures() {
if (mouseX > 690 && mouseX < 690 + buttonsize && mouseY > yloc && mouseY < yloc + buttonsize) {
treasures = treasures - 1;
}
if (mouseX > 790 && mouseX < 790 + buttonsize && mouseY > yloc && mouseY < yloc + buttonsize) {
treasures = treasures + 1;
}
if (treasures < 0) {
treasures = 0;
}
if (treasures > 10) {
treasures = 10;
}
}
void to_maingame() {
if (mouseX > width / 2.5 && mouseX < width / 1.8 + 150 && mouseY > height - 50 && mouseY < (height - 50) + 100 ) {
gamestatus = GAMEPLAYING;
}
}
Main game
int xdiverloc = 0;
int ydiverloc = 0;
int score = 0;
int life = 3;
final int diversize = 18;
int cols = 60;
int rows = 40;
int scale = 18;
boolean[][] mineArr = new boolean[cols][rows];
boolean[][] treasureArr = new boolean[cols][rows];
void gameplaying() {
background(255);
drawgrid();
drawdiver();
drawscore();
borders();
}
void setmines() {
int i = (int)random(cols);
int j = (int)random(rows);
mineArr[i][j] = true;
}
void set_treasures() {
int i = (int)random(cols);
int j = (int)random(rows);
treasureArr[i][j] = true;
}
void drawgrid() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int x = i * scale;
int y = j * scale;
fill(#add8e6);
noStroke();
rect(x, y, scale, scale);
fill(255, 0, 0);
if ( mineArr[i][j] ) {
circle(x, y, scale);
}
fill(0, 0, 255);
if ( treasureArr[i][j] ) {
circle (x, y, scale);
}
}
}
}
void drawdiver() {
fill(0);
ellipse(xdiverloc, ydiverloc, diversize, diversize);
}
void movementdiver() {
if (key == CODED) {
if (keyCode == DOWN) {
ydiverloc = ydiverloc + 18;
}
}
if (key == CODED) {
if (keyCode == RIGHT) {
xdiverloc = xdiverloc + 18;
}
}
if (key == CODED) {
if (keyCode == LEFT) {
xdiverloc = xdiverloc - 18;
}
}
if (key == CODED) {
if (keyCode == UP) {
ydiverloc = ydiverloc - 18;
}
}
if (key == ' ') {
}
}
void borders() {
if (xdiverloc < width - width) {
xdiverloc = width - width;
}
if (ydiverloc < height - height) {
ydiverloc = height - height;
}
if (xdiverloc > width - 138) {
xdiverloc = width - 138;
}
if (ydiverloc > height - 98) {
ydiverloc = height - 98;
}
}
void drawscore() {
fill(0);
text("Score = " + score, 20, height / 1.05);
text("Levens = " + life, 120, height / 1.05);
}
void drawmines() {
for (int i = 0; i < mines; i++) {
setmines();
}
}