Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

My screen in choosing mess up

$
0
0

I dunno why when the user finish the game and will be at the score screen then suddenly when going back to choose menu screen it mess up and somehow it will skip the game it is Logic problem?

import processing.sound.*;

//==Game Background==//
bck[] bck = new bck[500];

//==Timer==//
StopWatchTimer sw;

int screen = 0;

int speed = 5;

int minute;
int second;

//==Storing==//
int storeminute;
int storesecond;

//==1 Player key function==//
boolean p1_up=false;
boolean p1_down=false;
boolean p1_left=false;
boolean p1_right=false;

//==boundaries==//
boolean bound=false;

boolean gomenu=false;

PImage bckmenu;
PImage imgbtnsplay;
PImage imgbtnssetting;
PImage imgtitle;
PImage imgbtnsplayer1;
PImage imgbtnsrestart;

//==Main Menu==//
SoundFile mainmenusound;

//==Game==//
//==Warning Sound==//
SoundFile alert;

//==MENU-BUTTONS-PLAY==//
int imgbtnplayx = 550;
int imgbtnplayy = 400;
int imgbtnplayw = 200;
int imgbtnplayh = 200;

//==MENU-BUTTONS-SETTING==//
int imgbtnsettingx = 1180;
int imgbtnsettingy = 20;
int imgbtnsettingw = 80;
int imgbtnsettingh = 80;

//==MENU-BUTTON-PLAYER1==//
int imgbtnplayer1x = 100;
int imgbtnplayer1y = 150;
int imgbtnplayer1w = 300;
int imgbtnplayer1h = 100;

//==SCORE-BUTTON-RESTART==//
int imgbtnrestartx = 90;
int imgbtnrestarty = 600;
int imgbtnrestartw = 50;
int imgbtnrestarth = 50;

//==Direction==//
int directionX=1,directionY=0;

//==Player1 Space Ship==//
int P1_directionx1 = 640;
int P1_directiony1 = 650;
int P1_directionx2 = 650;
int P1_directiony2 = 650;
int P1_directionx3 = 660;
int P1_directiony3 = 680;
int P1_directionx4 = 630;
int P1_directiony4 = 680;

//==Border==//
int borderStroke=0;

//==Hp Bar==//
int hp=100;

//==Timer==//
int hours;
int minutes;
int seconds;

void setup(){
 size(1280,720);
 bckmenu = loadImage("bck_menu.jpg");
 for (int i=0;i<bck.length;i++) {
     bck[i]=new bck(random(width),random(height));
  }
//==Background Music==//
   mainmenusound = new SoundFile(this,"mainmenu.wav");
   mainmenusound.loop();

//==Timer Function==//
sw = new StopWatchTimer();

}

void draw(){
  //==MainMenu Part==//
  if(screen == 0){
  menu_screen();
  }
  //==Setting==//
  else if(screen == 1){
  setting_screen();
  }
  //==Choose How Many Player==//
  else if(screen == 2){
  choose_player_screen();
  }
  //==GAME 1PLAYER==//
  else if(screen == 3){
  p1game_screen();
  }
  //==GAME 2PLAYER==//
  else if(screen == 4){
  p2game_screen();
  }
  //==Score==//
  else if(screen == 5){
  score_screen();
  }
}

//==Main Menu==//
void menu_screen(){

  //==background==//
  background(bckmenu);

  //==Play Button==//
  imgbtnsplay = loadImage("btnmenu.png");
  image(imgbtnsplay,imgbtnplayx,imgbtnplayy,imgbtnplayw,imgbtnplayh);
  buttonplay(imgbtnplayx,imgbtnplayy,imgbtnplayw,imgbtnplayh);

  //==Setting Button==//
  imgbtnssetting = loadImage("btnsetting.png");
  image(imgbtnssetting,imgbtnsettingx,imgbtnsettingy,imgbtnsettingw,imgbtnsettingh);
  buttonsetting(imgbtnsettingx,imgbtnsettingy,imgbtnsettingw,imgbtnsettingh);

 //==Title==//
 imgtitle= loadImage("title.png");
 image(imgtitle,430,0,450,350);
}

//==Setting==//
void setting_screen(){
  background(bckmenu);
}

//==Choose How Many Player==//
void choose_player_screen(){
  background(0);

  //==Player 1 Button==//
  imgbtnsplayer1 = loadImage("btnplayer1.png");
  image(imgbtnsplayer1,imgbtnplayer1x,imgbtnplayer1y,imgbtnplayer1w,imgbtnplayer1h);
  buttonplayer1(imgbtnplayer1x,imgbtnplayer1y,imgbtnplayer1w,imgbtnplayer1h);

  //==Title==//
  // Prompt the user to choose whether they want to play 1 player or 2 players
  fill(random(255),random(255),random(255));
  textSize(80);
  text("Choose Game Mode!",260,500);
}

//==GAME 1PLAYER==//
void p1game_screen(){
   //==Start Timer==//

  //==Background Color==//
  background(0);
  //==Circle(That we will make it as a star) Make it like a space feel==//
  for (int j=0;j<bck.length;j++) {
    bck[j].display();
    bck[j].travel();
  }

  //==Ship Shape==//
  stroke(204,102,0);
  fill(255);
  strokeWeight(8);
  quad(P1_directionx1,P1_directiony1,P1_directionx2,P1_directiony2,P1_directionx3,P1_directiony3,P1_directionx4,P1_directiony4);

  //==Direction==//
  /**
  The direction will be have a switch by using boolean (true or false)
  If the boolean is true then the direction on XY will be increase or decrease.(key when pressed)
  If the boolean is false then the direction will not be increase or decrease.(key when released)
  */
  if(p1_left){
        P1_directionx1=P1_directionx1+(directionX*speed);
        P1_directiony1=P1_directiony1+(directionY*speed);
        P1_directionx2=P1_directionx2+(directionX*speed);
        P1_directiony2=P1_directiony2+(directionY*speed);
        P1_directionx3=P1_directionx3+(directionX*speed);
        P1_directiony3=P1_directiony3+(directionY*speed);
        P1_directionx4=P1_directionx4+(directionX*speed);
        P1_directiony4=P1_directiony4+(directionY*speed);
  }
  else if(p1_right){
        P1_directionx1=P1_directionx1+(directionX*speed);
        P1_directiony1=P1_directiony1+(directionY*speed);
        P1_directionx2=P1_directionx2+(directionX*speed);
        P1_directiony2=P1_directiony2+(directionY*speed);
        P1_directionx3=P1_directionx3+(directionX*speed);
        P1_directiony3=P1_directiony3+(directionY*speed);
        P1_directionx4=P1_directionx4+(directionX*speed);
        P1_directiony4=P1_directiony4+(directionY*speed);
  }
  else if(p1_up){
        P1_directionx1=P1_directionx1+(directionX*speed);
        P1_directiony1=P1_directiony1+(directionY*speed);
        P1_directionx2=P1_directionx2+(directionX*speed);
        P1_directiony2=P1_directiony2+(directionY*speed);
        P1_directionx3=P1_directionx3+(directionX*speed);
        P1_directiony3=P1_directiony3+(directionY*speed);
        P1_directionx4=P1_directionx4+(directionX*speed);
        P1_directiony4=P1_directiony4+(directionY*speed);
  }
  else if(p1_down){
        P1_directionx1=P1_directionx1+(directionX*speed);
        P1_directiony1=P1_directiony1+(directionY*speed);
        P1_directionx2=P1_directionx2+(directionX*speed);
        P1_directiony2=P1_directiony2+(directionY*speed);
        P1_directionx3=P1_directionx3+(directionX*speed);
        P1_directiony3=P1_directiony3+(directionY*speed);
        P1_directionx4=P1_directionx4+(directionX*speed);
        P1_directiony4=P1_directiony4+(directionY*speed);
  }

  //==Check Bondaries==//
  if(P1_directionx4==0){
    gamep1toscore();
  }
  else if(P1_directionx3==width){
    gamep1toscore();
  }
  else if(P1_directiony1==0 & P1_directiony2==0){
    gamep1toscore();
  }
  else if(P1_directiony3==height & P1_directiony4==height){
    gamep1toscore();
  }

  //==Warning==//
  if(P1_directionx4<35){
    bound=true;
    boundaries();
  }
  else if(P1_directionx3>width-35){
   bound=true;
   boundaries();
  }
  else if(P1_directiony1<50 & P1_directiony2<35){
   bound=true;
   boundaries();
  }
  else if(P1_directiony3>height-35 & P1_directiony4>height-35){
   bound=true;
   boundaries();
  }
  else
   bound=false;

   //==Hp BAR==//
   HPBar();

   //==Timer==//
    minute = sw.minute();
    second = sw.second();
    timer();

}
//==GAME 2PLAYER==//
void p2game_screen(){
}

//==Score==//
void score_screen(){
  background(bckmenu);
  fill(255);
  textAlign(CENTER);
  textSize(70);
  text(nf(storeminute, 2)+":"+nf(storesecond, 2), width/2, height/2);

  //==Play Button==//
  imgbtnsrestart = loadImage("btnrestart.png");
  image(imgbtnsrestart,imgbtnrestartx,imgbtnrestarty,imgbtnrestartw,imgbtnrestarth);
  buttonrestart(imgbtnrestartx,imgbtnrestarty,imgbtnrestartw,imgbtnrestarth);
}

//==Main Menu ---> Setting==//
void menutosetting(){
screen = 1;
}

//==Setting ---> Main Menu==//
void settingtomenu(){
screen = 0;
}

//==Main Menu ---> ChooseScreen==//
void menutochoose(){
screen = 2;
sw.stop();
}

//==ChooseScreen ---> GameP1==//
void choosetogamep1(){
screen = 3;
sw.start();
}

//==ChooseScreen ---> GameP2==//
void choosetogamep2(){
screen = 4;
}

//==GameP1 ---> Score==//
void gamep1toscore(){
screen = 5;
//==Record the timer's time==//
storeminute=minute;
storesecond=second;
sw.stop();
}

//==GameP2 ---> Score==//
void gamep2toscore(){
screen = 5;
}

//==Score ---> ChooseScreen==//
void scoretochoose(){
screen = 3;
}

//==Score ---> Menu==//
void scoretomenu(){
screen = 0;
}

//==Boundaries==//
void boundaries(){

 if(bound == true){
  noFill();
  stroke(255,0,0);
  strokeWeight(30);
  int twoBS = borderStroke*2;
  rect(borderStroke, borderStroke, width-twoBS, height-twoBS);
  }
}

//==Hp Bar==//
void HPBar(){

  fill(255);
  stroke(255);
  strokeWeight(10);
  rect(50,50,200,20);

  fill(50,250,25);
  noStroke();
  rect(50,50,hp*2,20);

  fill(0);
  textSize(20);
  text("HP:"+hp,90,70);
  text("/100",160,70);
}

//==Timer==//
void timer(){
  fill(255);
  textAlign(CENTER);
  text(nf(minute, 2)+":"+nf(second, 2), 150, 175);
}

public void mousePressed(){
}

public void keyPressed(){
  if (key == CODED)
  {
    if (keyCode == LEFT)
    {
      //if (directionX>0) {
        p1_left=true;
        directionX=-1;
      directionY=0;
      //}
    }
    else if (keyCode == RIGHT)
    {
      //if (directionX<0) {
        p1_right=true;
        directionX=1;
      directionY=0;
      //}
    }
    else if (keyCode == UP)
    {
      //if (directionY<0) {
        p1_up=true;
        directionY=-1;
      directionX=0;
      //}
    }
    else if (keyCode == DOWN)
    {
      //if (directionY<0) {
        p1_down=true;
        directionY=1;
      directionX=0;
      //}
    }
  }
  if(key == CODED){
    if(keyCode == 'w'||keyCode == 'W'){
      gomenu=true;
    }
  }
}

public void keyReleased(){
  if (key == CODED)
  {
    if (keyCode == LEFT)
    {
      //if (directionX>0) {
        p1_left=false;
      //}
    }
    else if (keyCode == RIGHT)
    {
      //if (directionX<0) {
        p1_right=false;
      //}
    }
    else if (keyCode == UP)
    {
      //if (directionY<0) {
        p1_up=false;
      //}
    }
    else if (keyCode == DOWN)
    {
      //if (directionY<0) {
        p1_down=false;
      //}
    }
  }
}

void buttonplay(int x,int y,int w,int h){
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY<y+h){
      menutochoose();
    }
  }
}

void buttonsetting(int x,int y,int w,int h){
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY<y+h){
      menutosetting();
    }
  }
}

void buttonplayer1(int x,int y,int w,int h){
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY<y+h){
      choosetogamep1();
    }
  }
}

void buttonrestart(int x,int y,int w,int h){
  if(mousePressed){
    if(mouseX>x && mouseX <x+w && mouseY>y && mouseY<y+h){
      scoretomenu();
    }
  }
}

class bck {
  //member variables
  float cirX,cirY,cirSize,cirSpeed,starColor;

  //constructor
  bck(float starX, float starY) {
    this.cirX=starX;
    this.cirY=starY;
    cirSize = random(1,8);
    cirSpeed = random(1,2);
  }

  void display() {
    noStroke();

      fill(250,250,250); //white stars
    ellipse(cirX,cirY,cirSize,cirSize);
  }

  void travel() {
    cirY+=cirSpeed;
    if(cirY>height) {
      cirY=0;
    }
  }
}

class StopWatchTimer {
  int startTime = 0, stopTime = 0;
  boolean running = false;
  void start() {
    startTime = millis();
    running = true;
  }
  void stop() {
    stopTime = millis();
    running = false;
  }
  int getElapsedTime() {
    int elapsed;
    if (running) {
      elapsed = (millis() - startTime);
    }
    else {
      elapsed = (stopTime - startTime);
    }
    return elapsed;
  }
  int second() {
    return (getElapsedTime() / 1000) % 60;
  }
  int minute() {
    return (getElapsedTime() / (1000*60)) % 60;
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles