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: Starting a New Game After One Ends.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Starting a New Game After One Ends.

    Hello I am writing a program that shoots a bullet that hits a balloon and pops, you have 60 seconds to pop as many balloons as you can. I have everything done the only thing I can not figure out is how to start a new game from my JMenubar any help pointing me in the right direction would be much appreciated.




    Main method.
    import java.awt.*;
       import java.awt.event.*;
       import java.util.Random;
       import javax.swing.*;
     
       public class BalloonGame extends JFrame {
          private Game	board;
     
     
          public  BalloonGame(int width, int height) {
             board = new Game(width, height);
             add(board);
             board.setFocusable(true);
          }
     
          public static void main(String[] args) {
             JMenuBar menuBar = new JMenuBar();
             int width = 640, height = 360; 
             final BalloonGame frame = new BalloonGame(width, height);
             frame.setSize(width, height);
             frame.setLocationRelativeTo(null);
             frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
             frame.setVisible( true);
             frame.setResizable(false);
             frame.setTitle("Balloon Game");
             frame.setJMenuBar(menuBar);
    		//panel
             JPanel panel = new JPanel();
     
     
          // menu bar  
             JMenu fileMenu = new JMenu("File");
     
             JMenuItem openMenu = new JMenuItem("New Game");       
             JMenuItem closeMenu = new JMenuItem("Close");
             fileMenu.add(openMenu);
             fileMenu.add(closeMenu);
             menuBar.add(fileMenu);
     
    		 openMenu.addActionListener(
                   new ActionListener(){
     
                      public void actionPerformed(ActionEvent e){
                      //TODO add for new game
     
                      }
                   });
     
     
    		 closeMenu.addActionListener(
                   new ActionListener(){
     
                      public void actionPerformed(ActionEvent e){
     
                         System.exit(0);                
                      }
                   });
     
     
    		}
       }


    Game class.

       import java.awt.*;
       import java.awt.event.*;
       import java.util.Random;
       import javax.swing.*;
     
       public  class Game extends JPanel  {
     
          //instance variables
          private int	xCoordinate	= 0;
          private Point	shootingFrom;
          private boolean	shooting	= false;
          private Point	balloonLocation;
          private int gunSize = 25;
          private int padding = 35;
          private boolean isShattered;
          private Timer stopShatter;
          private int shatterDistance;
          private int gunTilt = 90;
          private int bulletTilt;
          private int	balloonRadius = 20;
          private int bulletRadius = 5;
          private static int ballonsHit;
          private static  int s = 60;
          private static int k = 0;
          private Timer	timer;
          private int x;
          private int y;
     
          public Game(int width, int height) {
     
             JOptionPane.showMessageDialog (null,"               Welcome to my Balloon Game!               " + "\n You have 60 seconds to shoot and break as many balloons as you can." + 
                "\n\nDirections to play this game." + "\nLeft arrow key moves the gun left." + "\nRight arrow key moves the gun right." + "\nUp arrow key shoots the gun.");
     
             final  JLabel time = new JLabel();
             this.add(time);
     
             ActionListener actionListener = 
                new ActionListener() {
                   public void actionPerformed(ActionEvent actionEvent) {
     
                      if(s > k) {
                         s--; 
                         time.setText("Time left: " + s + 
                            "                                                                                                                                                               Balloons hit: " + ballonsHit);
                      }
                      if(s == 0){
                       time.setText("Time is up!" + 
                            "                                                                                                                                                               Balloons hit: " + ballonsHit);
     
                      }
                   }
                } ;
     
             Timer countDownTimer = new Timer(1000, actionListener);
             countDownTimer.start();
     
             this.setSize(width, height);
             xCoordinate	= this.getWidth()/2-1;
             balloonLocation = setRandomBalloonLocation();
     
             timer = new Timer( 15, 
                   new ActionListener() {
     
                      public void actionPerformed(ActionEvent e) {
                         repaint();
                      }
                   } );
     
             //repaint board every 25 milliseconds
             timer.start();
     
             //initiate shatter timer. 
             stopShatter = new Timer(1500, new ShatterTerminator());
     
             addKeyListener( 
                   new KeyAdapter() {
     
                      public void keyPressed(KeyEvent e) {
                         switch ( e.getKeyCode() ) {
                            case KeyEvent.VK_UP:
                               shootBall();
                               bulletTilt = gunTilt;
                               break;
                            case KeyEvent.VK_LEFT:
                               turnLeft();
                               break;
                            case KeyEvent.VK_RIGHT:
                               turnRight();
                               break;
                         }
                      }
                   });
          }
     
          public Point setRandomBalloonLocation() {
     
           //use java.util.Random to generate random location
             Random rand = new Random();
             Point location = new Point(rand.nextInt(this.getX()+this.getWidth()-padding)+1, (rand.nextInt(this.getY()+this.getHeight())/2)+1);
             return location;
          }
     
          public void shootBall() {
             if (!shooting) {
                shooting = true;
                shootingFrom = new Point(getWidth() / 2 + x - bulletRadius, getHeight() - y);		
             }  
          }
     
          public void turnLeft() {
             xCoordinate -= 1;
             gunTilt += 5;
          }
     
          public void turnRight() {
             xCoordinate += 1;
             gunTilt -= 5;
          }
     
          protected void paintComponent(Graphics g) {
             super.paintComponent(g);
     
             int x = (int) (Math.cos(Math.toRadians(gunTilt)) * gunSize);
             int y = (int)(Math.sin(Math.toRadians(gunTilt)) * gunSize);
     
             //turnLeft() and turnRight will control it
             g.drawLine( getWidth() / 2 + x, getHeight() - y, getWidth() / 2, getHeight() );
     
             //Display mini-balloons if the balloon was hit, otherwise display balloon
             if(isShattered) {
                g.setColor(Color.red);
                g.drawOval(balloonLocation.x+shatterDistance, balloonLocation.y, balloonRadius/2, balloonRadius/2);
                g.drawOval(balloonLocation.x-shatterDistance, balloonLocation.y, balloonRadius/2, balloonRadius/2);
                g.drawOval(balloonLocation.x, balloonLocation.y+shatterDistance, balloonRadius/2, balloonRadius/2);
                g.drawOval(balloonLocation.x, balloonLocation.y-shatterDistance, balloonRadius/2, balloonRadius/2);
                shatterDistance+=2;
             }
             else {
                g.setColor(Color.red);
                g.drawOval( balloonLocation.x, balloonLocation.y, balloonRadius*2, balloonRadius*2);
             }
     
             //Draw bullet
             if (shooting) {
                g.setColor(Color.black); 
                shootingFrom.x += (int)(Math.cos(Math.toRadians(gunTilt)) * gunSize);
                g.fillOval( shootingFrom.x, shootingFrom.y, bulletRadius, bulletRadius);
     
                if((shootingFrom.x >= balloonLocation.x && shootingFrom.x <= (balloonLocation.x+(balloonRadius*3))) 
                   && (shootingFrom.y >= balloonLocation.y && shootingFrom.y <= (balloonLocation.y+(balloonRadius*3)))
                   ) {
     
                   ballonsHit++;
                   shatterDistance = 0;
                   isShattered = true;
                   stopShatter.start();
                   shooting = false;
                }
     
                //checks if the ball hits the edge of the screen
                if(shootingFrom.y <= 0 || shootingFrom.x <= 0 || shootingFrom.x >= this.getX()+this.getWidth()) { 
     
                   shooting = false;
                }
     
                shootingFrom.y -= (int)(Math.sin(Math.toRadians(gunTilt)) * gunSize);
             }
          }
     
          public  class ShatterTerminator implements ActionListener {
             public void actionPerformed(ActionEvent e) {
                isShattered = false;
                balloonLocation	= setRandomBalloonLocation();
                stopShatter.stop();
             }
          }
       }
    Last edited by Delgado21; September 18th, 2011 at 10:19 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Starting a New Game After One Ends.

    how to start a new game
    Reset all variables to their values when the game started.

Similar Threads

  1. Starting BlackBerry development
    By AttilioCarotenuto in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: July 29th, 2011, 12:11 PM
  2. Starting a Slideshow Program... Lost
    By DrumKitt87 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 1st, 2010, 07:31 AM
  3. starting to make a simple database
    By chronoz13 in forum JDBC & Databases
    Replies: 1
    Last Post: January 15th, 2010, 02:06 PM
  4. Help starting program, need done by Nov 4
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 4th, 2009, 07:22 PM
  5. Need help starting a program
    By Mickey2315 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 7th, 2009, 02:21 PM