Search code examples
javaprocessing

Need a little help getting my title screen to appear


When I run this code i get a blank screen with no action. I took gameState and drawScreen from another assignment that I completed and the game works great with the title page appearing before the game. For some reason I can't get it to work with this game. I'm focusing on help specifically in my drawScreen function and seeing if there's something that I'm missing and if there's something that I need to takeout. Again I'm just trying to get the title screen to appear so that I won't have a blank screen when launching program.

      int numBubbles = 40; // initial bubble count
      final int BROKEN = -99; // code for "broken", may have other states later
      final int MAXDIAMETER = 120; // maximum size of expanding bubble
      PVector[] ellipses = { new PVector(150, 150), new PVector(300, 150), new PVector(450, 150)};
      int diam = 100;
      int mouseClicks = 15;
      int gameState;
      public final int INTRO = 1;
      public final int PLAY = 2;
      public final int PAUSE = 3;
      public final int GAMEOVERWIN = 4;
      public final int GAMEOVERLOSE = 5;
      PFont font;
      
      
      ArrayList pieces; // all the playing pieces
      
      // CHANGE: variable to keep score
      int score = 0;
      
      void setup() {
          pieces = new ArrayList(numBubbles);
          background(0);
          size(640, 640);
          font = createFont("Cambria", 32); 
          frameRate(24);
          noStroke();
          smooth();
          for(int i = 0; i < numBubbles; i++) {
              pieces.add(new Bubble(random(width), random(height), 30, i, pieces));
          }
      
      }
      
      void mousePressed() {
        
          
          
          // on click, create a new burst bubble at the mouse location and add it to the field
          Bubble b = new Bubble(mouseX,mouseY,2,numBubbles,pieces);
          // CHANGE: decrementing the score once so that score for new bubbles is not counted
          --score;
          b.burst();
          pieces.add(b);
          numBubbles++;
          if (mouseButton == LEFT) { mouseClicks--; } else { mouseClicks = 15; }
          
      }
      
      void draw() {
          
        switch(gameState) 
        {
          case INTRO:
            drawScreen("Welcome!", "Press s to start");
            break;
          case PAUSE:
            drawScreen("PAUSED", "Press p to resume");
            break;
          case GAMEOVERWIN:
            drawScreen("GAME OVER, YOU WON!", "Press s to try again");
            break;
          case GAMEOVERLOSE:
            drawScreen("GAME OVER, YOU LOST", "Press s to try again");
            break;
          case PLAY:
            background(0);
          
          if(mouseClicks >= 0 && score !=40)
              gameState = GAMEOVERLOSE;
          else
            gameState = GAMEOVERWIN;
          for(int i = 0; i < numBubbles; i++) {
              Bubble b = (Bubble)pieces.get(i); // get the current piece
              if(b.diameter < 1) { // if too small, remove
                  pieces.remove(i);
                  numBubbles--;
                  i--;
              }
              else {
                  // check collisions, update state, and draw this piece
                  if(b.broken == BROKEN) { // only bother to check collisions with broken bubbles
                      b.collide();
                  }
              }
              b.update();
              b.display();
          }
          
          // CHANGE: printing the score to screen
          textSize(32);
          fill(0, 102, 153);
          text(score , 10, 30);
          text(mouseClicks+"Clicks remaing", 0,600,width,height);
          break;
       }
      }
      
      void drawScreen(String title, String instructions) 
      {
        background(0);
        
        // draw title
        fill(255,100,0);
        textSize(60);
        textAlign(CENTER, BOTTOM);
        text(title, width/2, height/2);
        
        // draw instructions
        fill(255,255,255);
        textSize(32);
        textAlign(CENTER, TOP);
        text(instructions, width/2, height/2);
      }

Solution

  • It doesn't look like you ever set the initial value of gameState. It will default to 0, which is not one of the cases in your switch statement.

    Initialize the variable with 1 (int gameState = 1;), OR change your INTRO case to be zero and you should see the intro screen pop up.