I currently have this
Mainframe
final int GAMESTART = 1;
final int GAMEPLAYING = 2;
final int GAME_END = 3;
int gamestatus = GAMESTART;
final int SCREEN_HEIGHT = 800;
final int SCREEN_WIDTH = 1200;
void settings() {
size ( SCREEN_WIDTH, SCREEN_HEIGHT);
}
void setup() {
ellipseMode(CORNER);
}
void draw() {
gamestatus();
}
void gamestatus() {
if (gamestatus == GAMESTART) {
gamestartmenu();
} else if (gamestatus == GAMEPLAYING) {
gamemainscreen();
} else if (gamestatus == GAME_END) {
//game_endscreen();
}
}
void mousePressed() {
to_maingame();
add_decmines();
add_dectreasures();
}
void keyPressed() {
movementdiver();
}
No big changes here except for the size removal from setup and adding it into settings.
Start menu
final int YLOC_BUTTON = 200;
final int BUTTONSIZE = 50;
final float XLOC_BUTTON1 = SCREEN_WIDTH / 1.75;
final float XLOC_BUTTON2 = SCREEN_WIDTH / 1.5;
final float XLOC_BUTTON3 = SCREEN_WIDTH / 2.55;
final float XLOC_BUTTON4 = SCREEN_WIDTH / 3.4;
final int XRECTSIZE= 150;
final int YRECTSIZE = 100;
int amountmines = 1;
int amount_treasures = 1;
PImage photo;
void gamestartmenu() {
//photo = loadImage("Background.jpg");
//photo.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
//image(photo, 0, 0);
drawsquares();
drawbuttons();
drawtexts();
}
void drawsquares() {
int ysquare = height/6;
float xsquare = width / 1.8;
fill(#1144ff);
drawsquare(width / 2.3, height - 50, XRECTSIZE, YRECTSIZE);
drawsquare(xsquare, ysquare, XRECTSIZE + 50, YRECTSIZE - 50);
drawsquare(xsquare / 2, ysquare, XRECTSIZE + 50, YRECTSIZE - 50);
}
void drawsquare(float xloc, int yloc, int xsize, int ysize) {
rect(xloc, yloc, xsize, ysize);
}
void drawtexts() {
int textheight = height/5;
final float YTEXTLOC = height / 2.9;
final float XTEXTLOC = SCREEN_WIDTH / 1.71;
fill(0);
textSize(18);
drawtext("Start game", width / 2.17, height - 20);
drawtext("Diving for treasure", width/2.35, height/10);
drawtext("Mines = " + amountmines, width / 3.2, textheight);
drawtext("Treasures = " + amount_treasures, SCREEN_WIDTH / 1.7, textheight);
fill(0);
textSize(30);
drawtext("-", XTEXTLOC, YTEXTLOC);
drawtext("+", XTEXTLOC / 1.45, YTEXTLOC);
drawtext("+", XTEXTLOC * 1.16, YTEXTLOC);
drawtext("-", XTEXTLOC / 1.9, YTEXTLOC);
}
void drawtext(String text, float xtext, float ytext) {
text(text, xtext, ytext);
}
void drawbuttons() {
fill(0);
drawbutton(XLOC_BUTTON1, YLOC_BUTTON, BUTTONSIZE);
drawbutton(XLOC_BUTTON2, YLOC_BUTTON, BUTTONSIZE);
drawbutton(XLOC_BUTTON3, YLOC_BUTTON, BUTTONSIZE);
drawbutton(XLOC_BUTTON4, YLOC_BUTTON, BUTTONSIZE);
}
void drawbutton(float xloc, int yloc, int buttonsize) {
rect(xloc, yloc, buttonsize, buttonsize);
}
void add_decmines() {
if (over(XLOC_BUTTON4, YLOC_BUTTON, BUTTONSIZE, BUTTONSIZE)) {
amountmines = amountmines - 1;
}
if (over(XLOC_BUTTON3, YLOC_BUTTON, BUTTONSIZE, BUTTONSIZE)) {
amountmines = amountmines + 1;
}
if (amountmines < 0) {
amountmines = 0;
}
if (amountmines > 10) {
amountmines = 10;
}
}
void add_dectreasures() {
if (over(XLOC_BUTTON1, YLOC_BUTTON, BUTTONSIZE, BUTTONSIZE)) {
amount_treasures = amount_treasures - 1;
}
if (over(XLOC_BUTTON2, YLOC_BUTTON, BUTTONSIZE, BUTTONSIZE)) {
amount_treasures = amount_treasures + 1;
}
if (amount_treasures < 0) {
amount_treasures = 0;
}
if (amount_treasures > 10) {
amount_treasures = 10;
}
}
void to_maingame() {
if (over(width/2.3, height-50, XRECTSIZE, YRECTSIZE) && gamestatus == GAMESTART) {
gamestatus = GAMEPLAYING;
drawmines();
drawtreasures();
}
}
boolean over(float x, int y, int w, int h) {
return (mouseX > x & mouseX < x + w & mouseY > y & mouseY < y + h);
}
In this tab, I added the boolean and used it for my mouseover if’s.
Also called the drawmines and drawtreasures in this tab as told.
No big changes overall too.
Main game
int xdiverloc = 0;
int ydiverloc= 0;
int score = 0;
int life = 3;
final int DIVERSIZE = 18;
final int COLS = 60;
final int ROWS = 40;
final int SCALE = 18;
boolean[][] minesArr = new boolean[COLS][ROWS];
boolean[][] treasuresArr = new boolean[COLS][ROWS];
void gamemainscreen() {
background(255);
drawgrid();
drawdiver();
drawscore();
border();
checkmineloc();
}
void setmines() {
int i = (int)random(COLS);
int j = (int)random(ROWS);
minesArr[i][j] = true;
}
void set_treasures() {
int i = (int)random(COLS);
int j = (int)random(ROWS);
treasuresArr[i][j] = true;
}
void checkmineloc() {
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
if ( minesArr[i][j] ) {
if (xdiverloc == i && ydiverloc == j) {
life = life - 1;
}
}
}
}
}
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 ( minesArr[i][j] ) {
circle(x, y, SCALE);
}
fill(0, 0, 255);
if ( treasuresArr[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 + DIVERSIZE;
}
}
if (key == CODED) {
if (keyCode == RIGHT) {
xdiverloc = xdiverloc + DIVERSIZE;
}
}
if (key == CODED) {
if (keyCode == LEFT) {
xdiverloc = xdiverloc - DIVERSIZE;
}
}
if (key == CODED) {
if (keyCode == UP) {
ydiverloc = ydiverloc - DIVERSIZE;
}
}
if (key == ' ') {
}
}
void border() {
if (xdiverloc < width - width) {
xdiverloc = width - width;
}
if (ydiverloc < height - height) {
ydiverloc = height - height;
}
if (xdiverloc > width - (SCREEN_WIDTH - 18*59) ) {
xdiverloc = width - (SCREEN_WIDTH - 18*59);
}
if (ydiverloc > height - (SCREEN_HEIGHT - 18*39)) {
ydiverloc = height - (SCREEN_HEIGHT - 18*39);
}
}
void drawscore() {
textSize(18);
fill(0);
text("Score = " + score, 20, height / 1.05);
text("Life = " + life, 120, height / 1.05);
}
void drawmines() {
for (int i = 0; i < amountmines; i++) {
setmines();
}
}
void drawtreasures() {
for (int i = 0; i < amount_treasures; i++) {
set_treasures();
}
}
The game looks like this when running:
When the diver is stepping on the red circle then he loses 1 life.
I tried to make it with this
void checkmineloc() {
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
if ( minesArr[i][j] ) {
if (xdiverloc == i && ydiverloc == j) {
life = life - 1;
}
}
}
}
}
however this does not work because i & j goes up to 60 and 40 respectively and does not mean the location.
What should be used here?