Results 1 to 4 of 4
  1. #1
    ch0sen is offline Member
    Join Date
    Aug 2015
    Posts
    1
    Rep Power
    0

    Default How to add a menu to my first Java game?

    This is my first game that I have made, so the code is probably trash. A lot of the code is stuff thrown together from differnet tutorials and guides. I'm just wondering how I would go about adding a menu to this game? One with start, exit, level select etc.

    Here is the code:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.util.Random;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class Game extends JFrame {
    
    	private Image dbImage;
    	private Graphics dbg;
    
    	boolean mouseDragged;
    
    	int mx, my;
    	int rectX, rectY;
    	int x = 490;
    	int x1 = 490;
    	int x2 = 490;
    	int x3 = 490;
    	int x4 = 490;
    	int x5 = 490;
    	int x6 = 490;
    	int x7 = 490;
    	int hp = 75;
    	int sc = 0;
    	int cd;
    	int cd1;
    	int cd2;
    	int cd3;
    	int cd4;
    	int cd5;
    	int sco;
    
    	int xTime;
    
    	public static void main(String[] args) {
    		Game mouse = new Game();
    
    	}
    
    	public Game() {
    		setSize(500, 600);
    		setResizable(false);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		addMouseMotionListener(new AL());
    		setLocationRelativeTo(null);
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		paintComponent(dbg);
    		g.drawImage(dbImage, 0, 0, this);
    	}
    
    	public void paintComponent(Graphics g) {
    		xTime++;
    		cd--;
    		cd1--;
    		cd2--;
    		cd3--;
    		cd4--;
    		cd5--;
    		sco--;
    
    		// if (rectY < 50);
    		// rectY = 51;
    
    		if (xTime % 6 == 1) {
    			x++;
    		}
    		if (xTime % 6 == 1) {
    			x1++;
    		}
    		if (xTime % 6 == 1) {
    			x2++;
    		}
    		if (xTime % 6 == 1) {
    			x3++;
    		}
    		if (xTime % 6 == 1) {
    			x4++;
    		}
    		if (xTime % 6 == 1) {
    			x5++;
    		}
    		if (xTime % 6 == 1) {
    			x6++;
    		}
    		if (xTime % 6 == 1) {
    			x7++;
    		}
    
    		if (x > 500) {
    			x = (int)(Math.random() * -1000);
    		}
    		if (x1 > 500) {
    			x1 = (int)(Math.random() * -1000);
    		}
    		if (x2 > 500) {
    			x2 = (int)(Math.random() * -1000);
    		}
    		if (x3 > 500) {
    			x3 = (int)(Math.random() * -1000);
    		}
    		if (x4 > 500) {
    			x4 = (int)(Math.random() * -1000);
    		}
    		if (x5 > 500) {
    			x5 = (int)(Math.random() * -1000);
    		}
    		if (x6 > 500) {
    			x6 = (int)(Math.random() * -1000);
    		}
    		if (x7 > 500) {
    			x7 = (int)(Math.random() * -1000);
    		}
    
    		g.setColor(Color.BLACK);
    		Rectangle rTitle = new Rectangle(0, 0, 500, 100);
    		g.fillRect(rTitle.x, rTitle.y, rTitle.width, rTitle.height); // top rect
    
    		g.setColor(Color.RED);
    		Rectangle r11 = new Rectangle(x, 120, 40, 40);
    		g.fillRect(r11.x, r11.y, r11.width, r11.height);
    		g.setColor(Color.ORANGE);
    		Rectangle r12 = new Rectangle(x1, 170, 40, 40);
    		g.fillRect(r12.x, r12.y, r12.width, r12.height);
    		g.setColor(Color.RED);
    		Rectangle r13 = new Rectangle(x2, 220, 40, 40);
    		g.fillRect(r13.x, r13.y, r13.width, r13.height);
    		Rectangle r14 = new Rectangle(x3, 270, 40, 40);
    		g.fillRect(r14.x, r14.y, r14.width, r14.height); // moving rects
    		g.setColor(Color.ORANGE);
    		Rectangle r15 = new Rectangle(x4, 320, 40, 40); // mouse rect
    		g.fillRect(r15.x, r15.y, r15.width, r15.height);
    		Rectangle r16 = new Rectangle(x5, 370, 40, 40); // mouse rect
    		g.fillRect(r16.x, r16.y, r16.width, r16.height);
    		g.setColor(Color.RED);
    		Rectangle r17 = new Rectangle(x6, 420, 40, 40); // mouse rect
    		g.fillRect(r17.x, r17.y, r17.width, r17.height);
    		Rectangle r18 = new Rectangle(x7, 465, 40, 40); // mouse rect
    		g.fillRect(r18.x, r18.y, r18.width, r18.height);
    		
    		Rectangle r1 = new Rectangle(400, rectY, 25, 25);
    		g.setColor(Color.GREEN);
    		g.fillRect(r1.x, r1.y, r1.width, r1.height);
    
    
    		g.setColor(Color.GREEN);
    		Rectangle health = new Rectangle(385, 110, hp, 15);
    		g.fillRect(health.x, health.y, hp, health.height);
    
    		g.setColor(Color.GREEN);
    		Rectangle score = new Rectangle(20, 110, sc, 15);
    		g.fillRect(score.x, score.y, sc, score.height);
    
    		Rectangle s1 = new Rectangle(20, 110, 3, 15);
    		g.setColor(Color.BLACK);
    		g.fillRect(s1.x, s1.y, s1.width, s1.height);
    		Rectangle s2 = new Rectangle(35, 110, 3, 15);
    		g.setColor(Color.BLACK);
    		g.fillRect(s2.x, s2.y, s2.width, s2.height);
    		Rectangle s3 = new Rectangle(50, 110, 3, 15);
    		g.setColor(Color.BLACK);
    		g.fillRect(s3.x, s3.y, s3.width, s3.height);
    		Rectangle s4 = new Rectangle(65, 110, 3, 15);
    		g.setColor(Color.BLACK);
    		g.fillRect(s4.x, s4.y, s4.width, s4.height);
    		Rectangle s5 = new Rectangle(80, 110, 3, 15);
    		g.setColor(Color.BLACK);
    		g.fillRect(s5.x, s5.y, s5.width, s5.height);
    		Rectangle s6 = new Rectangle(95, 110, 3, 15);
    		g.setColor(Color.BLACK);
    		g.fillRect(s6.x, s6.y, s6.width, s6.height);
    
    		Rectangle h1 = new Rectangle(385, 125, 3, 4);
    		g.setColor(Color.BLACK);
    		g.fillRect(h1.x, h1.y, h1.width, h1.height);
    		Rectangle h2 = new Rectangle(400, 125, 3, 4);
    		g.setColor(Color.BLACK);
    		g.fillRect(h2.x, h2.y, h2.width, h2.height);
    		Rectangle h3 = new Rectangle(415, 125, 3, 4);
    		g.setColor(Color.BLACK);
    		g.fillRect(h3.x, h3.y, h3.width, h3.height);
    		Rectangle h4 = new Rectangle(430, 125, 3, 4);
    		g.setColor(Color.BLACK);
    		g.fillRect(h4.x, h4.y, h4.width, h4.height);
    		Rectangle h5 = new Rectangle(445, 125, 3, 4);
    		g.setColor(Color.BLACK);
    		g.fillRect(h5.x, h5.y, h5.width, h5.height);
    		Rectangle h6 = new Rectangle(460, 125, 3, 4);
    		g.setColor(Color.BLACK);
    		g.fillRect(h6.x, h6.y, h6.width, h6.height);
    
    		if (r1.intersects(r11) && cd < 1) {
    			hp -= 15;
    			cd = 400;
    		}
    		
    		if (r1.intersects(r12) && cd1 < 1) {
    			sc += 15;
    			cd1 = 400;
    
    		}
    		
    		if (r1.intersects(r13) && cd < 1) {
    			hp -= 15;
    			cd = 400;
    		}
    		
    		if (r1.intersects(r14) && cd < 1) {
    			hp -= 15;
    			cd = 400;
    		}
    		
    		if (r1.intersects(r15) && cd4 < 1) {
    			sc += 15;
    			cd4 = 400;
    		}
    		
    		if (r1.intersects(r16) && cd5 < 1) {
    			sc += 15;
    			cd5 = 400;
    		}
    		
    		if (r1.intersects(r17) && cd < 1) {
    			hp -= 15;
    			cd = 400;
    		}
    		
    		if (r1.intersects(r18) && cd < 1) {
    			hp -= 15;
    			cd = 400;
    		}
    		repaint();
    
    		if (hp < 61) {
    			g.setColor(Color.YELLOW);
    			g.fillRect(health.x, health.y, hp, health.height);
    		}
    		if (hp < 16) {
    			g.setColor(Color.RED);
    			g.fillRect(health.x, health.y, hp, health.height);
    		}
    
    	}
    
    	public class AL extends MouseAdapter {
    		public void mouseMoved(MouseEvent e) {
    			rectX = e.getX() - 12;
    			rectY = e.getY() - 12;
    
    			if (hp < 1) {
    				JOptionPane.showMessageDialog(null, "Game over");
    				System.exit(ABORT);
    
    			}
    
    			if (sc > 74) {
    				JOptionPane.showMessageDialog(null, "You win!");
    				System.exit(ABORT);
    			}
    		}
    	}
    
    }
    Last edited by JosAH; 08-17-2015 at 08:00 AM. Reason: changed the tags to [code] ... [/code] tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to add a menu to my first Java game?

    Please edit your post and wrap your code with highlight tags:
    [highlight=java]
    YOUR CODE GOES HERE
    [/highlight]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to add a menu to my first Java game?

    how I would go about adding a menu to this game?
    There are classes in the Swing packages for doing that. See the tutorial: How to Use Menus (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to add a menu to my first Java game?

    There is quite a bit of code in your program which could be simplified (or streamlined) by using either arrays or ArrayLists. You may want to read about them in the tutorials in my signature.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Java 2D game - creating menu
    By ultimate in forum New To Java
    Replies: 4
    Last Post: 11-18-2012, 01:29 PM
  2. Creating a menu in a basic 2d game
    By ResidentBiscuit in forum New To Java
    Replies: 5
    Last Post: 11-30-2011, 04:18 AM
  3. Menu Help in TicTacToe Game
    By Ryan10 in forum New To Java
    Replies: 59
    Last Post: 04-04-2011, 05:19 AM
  4. Need help with a menu button in a game
    By pjr5043 in forum Java Applets
    Replies: 1
    Last Post: 12-07-2008, 09:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •