Can't seem a way to make game menu for simple gun game

boolean down=true;
boolean up=true;
boolean bullet=true;
float gunposition=350;//gun position
float bulletY=650;//bullet position
float bulletX=355;//bullet position
float alienX=350;//alien positon
float alienY=0;//alien position
float gravity=2;// gravity
PImage gun;
PImage bomb;
PImage spark;
boolean explosion=false;//no explosion yet


int count1=0;//score for terrorists (yourself)
int count2=0;//score for counter-terrorists(bot)
PFont font;


void setup() {
  size(700, 700);
  background(255);
  font=loadFont("MVBoli-22.vlw");
  bomb=loadImage("bomb.png");
   gun=loadImage("gun.png");
   spark=loadImage("fire.png");
   go=false;
}

void draw() {
  background(255);
  frameRate(60);
  fill(0, 0, 0);
  image(bomb,alienX,alienY,100,80);
  textFont(font);//font
  text("terrorists",50,50);
  text(count2,200,50);
  image(gun,gunposition,600,100,100);
  text("counter-terrorists",450,50);
  text(count1,670,50);
  bulletX=gunposition+70;
  alienY=alienY+gravity;
   rect(bulletX, bulletY, 10, 40);//bullet inside gun
  println(mouseX,mouseY);

 
 
   if (key==CODED==true) {
    if (keyCode==LEFT) {
      gunposition=gunposition-7;
    }
    if(gunposition>660){
      gunposition=660;}
     } 
  if (keyCode==RIGHT==true) {
    gunposition=gunposition+7;
  }
  if(gunposition<0){
  gunposition=0;
  }
  if(gunposition>600){
    gunposition=600;
}
  if (alienY<=650) {
    down=true;
  }
  if (alienY>650) {
    alienY=0;
    alienX= random(30, 670);//alien randomly spawn
    down=true;
    count1++;
  }
  if (keyPressed&&key==' '){
    up=true;
  }
  if (up==true) {//bullets going up
    bulletY=bulletY-10;
  }
  if (bulletY<10) {//reset
    bulletY=650;
    up=false;
   
  }
  if(bulletY<alienY+50&&bulletY>alienY-50&&bulletX<alienX+50&&bulletX>alienX-50){
    alienX=random(30,680);
    alienY=0;
    down=true;
    count2++;
}
if(explosion==true)//when hit explodes
{
  image(spark,gunposition,300,240,240);
  alienY=alienY-60;
}
if(explosion==true&&alienY<10){
  alienY=0;
  alienX=random(30,670);
  explosion=false;
  down=true;

}
}

For a menu please remember that you want to have two screens / major situations in draw() now: the game and the menu

You don’t want to mix them.

So start by saying boolean menu = true; before setup().

In draw check

if(menu) {
    // show menu
    .......
    ......
}
// -----------
else {
   // show game, guns.... what you have in draw() now
   ..... 
   ...
}//else

Nothing outside these lines is allowed in draw() (because it wouldn’t know to which state it belongs) except background().

The key checking: do it separately for both screens within the brackets {...}.

Remark

When you have more screens than 2, like with a game over screen, please make menu to an int named state which can be 0, 1, 2… so each state has a number.

Chrisir

2 Likes

As soon as user pressed a key in menu say menu = false;

Then you are in game mode

when you use keyPressed as a var, each key can be registered multiple times by processing. When you use a function keyPressed() and evaluate the key there, each key is registered only once. That would be better for the menu maybe.

1 Like