Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Game Menu

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Game Menu

    Can someone help me make a game menu for our game where there is start, how to play and exit?

    This is the code
    //Car
     
    // Input
    int virtualMouseY;
     
    // Objects
    Car car1;
    b1 b1;
    Rock[] rocks;
    int score = 0;
     
    int BEGINSPEED = 5;
    float speed = 5;
     
    // Game States
    int state;
    int PLAYING = 0;
    int PAUSED = 1;
    int GAMEOVER = 2;
     
    // Images
    PImage bg;
     
    class Car {
      void cardisplay() {
        fill(183, 67, 123);     
        rect(30, virtualMouseY-17, 100, 35);
        rect(40, virtualMouseY-36, 60,20);
     
        fill(0);
        ellipse(40,virtualMouseY+40-17.5,15,15);
        ellipse(120,virtualMouseY+40-17.5,15,15);
      }
    }
     
    class Rock {
      int x;
      int y;
     
      int width = 50;
      int height = 40;
     
      boolean passedCar;
     
      Rock() {
        reset();
      }
     
      void reset() {
        x = int(random(1000, 1500));
        y = int(random(300, 580));
        passedCar = false;    
      }
     
      void update(float speed) {
        if(x < 30 && passedCar == false) {
          passedCar = true;
          score += 1;
          speed += 1;
        }
     
        if(x < 0) {
           reset(); 
        }
     
        x -= speed;
      }
     
      void draw() {
        fill(128);
        ellipse(x, y, width, height);
      }
    }
     
    void setup() {
      rocks = new Rock[4];
      for (int i = 0; i < rocks.length ; i++) {
        rocks[i] = new Rock();
      }
     
      size(1000, 600);
      bg = loadImage("road.jpg");
      car1= new Car();
      b1= new b1();
     
      smooth();
      loop();  
      noStroke();  
    }
     
    void mousePressed() {
      if (state==GAMEOVER) {
        state = PLAYING;
     
        score = 0;
        speed = BEGINSPEED;
        for (int i = 0; i < rocks.length ; i++) {
          rocks[i].reset();
        }
      }
      if (state == PLAYING) {
         state = PAUSED; 
      } else if(state == PAUSED) {
         state = PLAYING; 
      }
    }
     
    void update() {
     
     
      b1.update();
      for (int i = 0; i < rocks.length ; i++) {
        rocks[i].update(speed);
     
     
     //car collides with rocks
        int frontR = rocks[i].x - (rocks[i].width/2);
        int backR = rocks[i].x + (rocks[i].width/2); 
        int topR = rocks[i].y + (rocks[i].height/2);
        int bottomR = rocks[i].y - (rocks[i].height/2); 
     
        int topC = int(virtualMouseY+17.5);
        int bottomC = int(virtualMouseY-17.5);
        if(frontR <= 130 && backR >= 30) {
          if((topR > virtualMouseY && bottomR < virtualMouseY) || (topR > topC && bottomR < topC) || (topR > bottomC && bottomR < bottomC)) {
             state = GAMEOVER;
          }
        }
      }
    }
     
    void draw() {
      virtualMouseY = constrain(mouseY, 335, 525);  
      if(state == PLAYING) {
        update();
        background(bg);
     
     
        b1.draw();    
        for (int i = 0; i < rocks.length ; i++) {
          rocks[i].draw();
        }
        car1.cardisplay();
     
        fill(108);
        textSize(15);
        text("CLICK TO PAUSE", 10, 55);
        fill(255, 169, 88);
        textSize(35);
        text("Score:", 10, 35);
        text(score*100, 120, 35);
      } else if (state == GAMEOVER) {
        background(128);
        textSize(42);
        fill(245, 166, 166);
        text("GAME OVER", 400, 240);
        fill(255);
        text("SCORE:", 450, 290);
        text(score*100, 450, 340);
        fill(166, 214, 245);
        text("CLICK TO RETRY", 350, 390);
      } else if (state == PAUSED) {
        background(110);
        fill(255);
        textSize(42);
        fill(245, 166, 166);
        text("PAUSED", 450, 280);
        fill(166, 214, 245);
        text("CLICK TO RESUME", 350, 330);
      }
     
    }
     
    class b1 {
     
      float xx;
     
      void update() {
        xx -= speed;
        if(xx <= -20) {
           xx += 1000; 
        }
      }
     
      void draw() {
        fill(128);    
        beginShape();
        vertex(xx-1000, 216);
        vertex(xx-951, 216);
        vertex(xx-951, 247);
        vertex(xx-931, 228);
        vertex(xx-923, 247);
        vertex(xx-923, 150);
        vertex(xx-908, 135);
        vertex(xx-893, 150);
        vertex(xx-893, 179);
        vertex(xx-882, 167);
        vertex(xx-870, 179);
        vertex(xx-870, 195);
        vertex(xx-826, 195);
        vertex(xx-826, 224); 
        vertex(xx-807, 198);
        vertex(xx-788, 224);
        vertex(xx-768, 198);
        vertex(xx-749, 224);
        vertex(xx-749, 150);
        vertex(xx-734, 135);
        vertex(xx-719, 150);
        vertex(xx-719, 217);
        vertex(xx-671, 217);
        vertex(xx-671, 248);
        vertex(xx-657, 228);
        vertex(xx-642, 248);
        vertex(xx-642, 195);
        vertex(xx-599, 195);
        vertex(xx-599, 224);
        vertex(xx-579, 200);
        vertex(xx-560, 224);
        vertex(xx-560, 179);
        vertex(xx-548, 168);
        vertex(xx-536, 179);
        vertex(xx-536, 233);
        vertex(xx-493, 233);
        vertex(xx-493, 195);
        vertex(xx-449, 195);
        vertex(xx-449, 151);
        vertex(xx-435, 136);
        vertex(xx-420, 151);
        vertex(xx-420, 179);
        vertex(xx-409, 167);
        vertex(xx-397, 179);
        vertex(xx-384, 167);
        vertex(xx-373, 179);
        vertex(xx-373, 223);
        vertex(xx-353, 198);
        vertex(xx-328, 233);
        vertex(xx-292, 233);
        vertex(xx-292, 214);
        vertex(xx-249, 214);
        vertex(xx-249, 179);
        vertex(xx-236, 168);
        vertex(xx-224, 179);
        vertex(xx-224, 225);
        vertex(xx-204, 200);
        vertex(xx-186, 225);
        vertex(xx-186, 151);
        vertex(xx-171, 137);
        vertex(xx-156, 151);
        vertex(xx-156, 197);
        vertex(xx-114, 197);
        vertex(xx-114, 181);
        vertex(xx-101, 169);
        vertex(xx-89, 181);
        vertex(xx-89, 215);
        vertex(xx-48, 215);
        vertex(xx-23, 181);
        vertex(xx, 211);
        vertex(xx, 216);
        vertex(xx, 216); //1st
        vertex(xx, 216);
        vertex(xx+49, 216);
        vertex(xx+49, 247);
        vertex(xx+63, 228);
        vertex(xx+77, 247);
        vertex(xx+77, 150);
        vertex(xx+92, 135);
        vertex(xx+107, 150);
        vertex(xx+107, 179);
        vertex(xx+118, 167);
        vertex(xx+130, 179);
        vertex(xx+130, 195);
        vertex(xx+174, 195);
        vertex(xx+174, 224); 
        vertex(xx+193, 198);
        vertex(xx+212, 224);
        vertex(xx+232, 198);
        vertex(xx+251, 224);
        vertex(xx+251, 150);
        vertex(xx+266, 135);
        vertex(xx+281, 150);
        vertex(xx+281, 217);
        vertex(xx+329, 217);
        vertex(xx+329, 248);
        vertex(xx+343, 228);
        vertex(xx+358, 248);
        vertex(xx+358, 195);
        vertex(xx+401, 195);
        vertex(xx+401, 224);
        vertex(xx+421, 200);
        vertex(xx+440, 224);
        vertex(xx+440, 179);
        vertex(xx+452, 168);
        vertex(xx+464, 179);
        vertex(xx+464, 233);
        vertex(xx+507, 233);
        vertex(xx+507, 195);
        vertex(xx+551, 195);
        vertex(xx+551, 151);
        vertex(xx+565, 136);
        vertex(xx+580, 151);
        vertex(xx+580, 179);
        vertex(xx+591, 167);
        vertex(xx+603, 179);
        vertex(xx+616, 167);
        vertex(xx+627, 179);
        vertex(xx+627, 223);
        vertex(xx+647, 198);
        vertex(xx+672, 233);
        vertex(xx+708, 233);
        vertex(xx+708, 214);
        vertex(xx+751, 214);
        vertex(xx+751, 179);
        vertex(xx+764, 168);
        vertex(xx+776, 179);
        vertex(xx+776, 225);
        vertex(xx+796, 200);
        vertex(xx+814, 225);
        vertex(xx+814, 151);
        vertex(xx+829, 137);
        vertex(xx+844, 151);
        vertex(xx+844, 197);
        vertex(xx+886, 197);
        vertex(xx+886, 181);
        vertex(xx+899, 169);
        vertex(xx+911, 181);
        vertex(xx+911, 215);
        vertex(xx+952, 215);
        vertex(xx+977, 181);
        vertex(xx+1000, 211);
        vertex(xx+1000, 216); //1st
        vertex(xx+1049, 216);
        vertex(xx+1049, 247);
        vertex(xx+1049, 315);
        vertex(xx-1000, 315);
        endShape(CLOSE);
      }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Game Menu

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    You have a good start . . . try writing a method that presents a menu, gets the user's choice - a letter, number, whatever you decide - and returns the user's choice. The method I describe could be completely stand-alone at this point so that you can thoroughly test it.

Similar Threads

  1. Menu Problem
    By StuckInJava in forum Loops & Control Statements
    Replies: 4
    Last Post: March 23rd, 2012, 06:43 PM
  2. Is it possible to use card layout for menu and menu items?
    By jai2rul in forum AWT / Java Swing
    Replies: 0
    Last Post: April 11th, 2011, 08:19 AM
  3. Menu of Menus Help
    By Strivelli in forum Java Theory & Questions
    Replies: 10
    Last Post: June 5th, 2010, 02:40 PM
  4. popup menu
    By antonio69 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 04:24 AM
  5. JFrame Menu Screen(For my game)?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 2
    Last Post: March 8th, 2010, 04:07 AM

Tags for this Thread