Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Game restart

  1. #1
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Game restart

    Hi,
    I'm creating a game. When you die, there is a button called 'Play Again'.
    But I have no idea on how to get the game to restart.
    Could anyone please help me?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,988
    Rep Power
    9

    Default Re: Game restart

    Have a method called restart() that sets the game up. Call it at the very beginning of the game. Call it when you click that button.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Joey0412 is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Game restart

    thanks alot :)
    but i have another problem.
    Is there any way i can make a drawn Rectangle go away?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Game restart

    Tell the painting method to not draw the rectangle and call repaint().
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    Quote Originally Posted by Joey0412 View Post
    Hi,
    I'm creating a game. When you die, there is a button called 'Play Again'.
    But I have no idea on how to get the game to restart.
    Could anyone please help me?
    You can also encompass your entire code in a do.. while loop.
    - Declare a int variable "again" on top of the loop (initial value = 0)..
    - at the last part of the code within the loop.. ask the user if they would like to play again.. 0= yes, 1=no. (again is equal to the answer).
    - then your loop condition is while again = 0.
    Hope this helps, I personally think this route is easier than using methods.
    - CHEERS!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Game restart

    Quote Originally Posted by Tha Boss View Post
    You can also encompass your entire code in a do.. while loop.
    - Declare a int variable "again" on top of the loop (initial value = 0)..
    - at the last part of the code within the loop.. ask the user if they would like to play again.. 0= yes, 1=no. (again is equal to the answer).
    - then your loop condition is while again = 0.
    Hope this helps, I personally think this route is easier than using methods.
    - CHEERS!
    Since the original poster mentions that his program has a 'Play Again' button, I'm assuming that the program is a GUI program. If so, then a do... while loop won't work.

  7. #7
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    Quote Originally Posted by Fubarable View Post
    Since the original poster mentions that his program has a 'Play Again' button, I'm assuming that the program is a GUI program. If so, then a do... while loop won't work.
    True... but even in that sense, do... while is still possible... I suppose he's not going to have a dialog box pop up with only a "Play Again" button in it??? what if the users answer is no?, it would probably be in the form.. "Would you like to play again?" in a YES_NO_OPTION dialog.. so even better using swing.. e.g below...

    Java Code:
    int run = 0;
    do {
    
         //Program coding here...
    
    run = JOptionPane.showConfirmDialog(null,
                        "Would you like to play again?", //Boolean question - good choice for play-again scenario.
                        "Again?", //Dialog box name.
                        JOptionPane.YES_NO_OPTION); //Give user option to go again or not.
            } while (run == 0); //Loop condition.
    "Don't forget or regret your past in this great future... (let it be part of who you will become)."
    - Bob Marley.
    Last edited by Tha Boss; 06-17-2012 at 03:40 AM.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Game restart

    Quote Originally Posted by Tha Boss View Post
    True... but even in that sense, do... while is still possible... I suppose he's not going to have a dialog box pop up with only a "Play Again" button in it??? what if the users answer is no?, it would probably be in the form.. "Would you like to play again?" in a YES_NO_OPTION dialog.. so even better using swing.. e.g below...

    Java Code:
    int run = 0;
    do {
    
         //Program coding here...
    
    run = JOptionPane.showConfirmDialog(null,
                        "Would you like to play again?", //Boolean question - good choice for play-again scenario.
                        "Again?", //Dialog box name.
                        JOptionPane.YES_NO_OPTION); //Give user option to go again or not.
            } while (run == 0); //Loop condition.

    There's the possibility of an infinite loop in there given the JOptionPane.YES_OPTION value is 0, and that value is returned form the showConfirmDialog when the user clicks yes
    Last edited by doWhile; 06-17-2012 at 04:01 AM. Reason: clarification

  9. #9
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    Quote Originally Posted by doWhile View Post
    There's the possibility of an infinite loop in there given the JOptionPane.YES_OPTION value is 0.
    The program is always updating "run" last thing in your program, though the initial value is 0, it dosen't matter (it can be any number) the dialog will pop up and overwrite the value of 0, as by default the option pane will return a 0 if the user clicks yes and 1 if the user clicks no and store it in "run" which is checked last thing in the do... while loop. This Will never cause infinite loop.. I use it in all my programs. Besides methods for people new to java??? I think this by far is the better way to go, even in advanced for that matter.

    Check your facts bro... I know 0 usually means off/no and 1 means on/yes... But it's not so in the dialog boxes..

    And oh-my-gosh! Your name is DoWhile? - Shame on you!

    "It never ceases to surprise me at the infinite capacity of the human mind to resist the introduction of useful knowledge."
    - Thomas Raynesford Lounsbury.
    Last edited by Tha Boss; 06-17-2012 at 04:30 AM. Reason: Clarification

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Game restart

    This Will never cause infinite loop.
    Stick the code you posted inside a main method and try for yourself.

  11. #11
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    I use it in all my programs.
    I'm talking from experience using this method, Did you try it?

  12. #12
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Game restart

    Quote Originally Posted by Tha Boss View Post
    I'm talking from experience using this method, Did you try it?

    Yes. Run this and click the yes button:
    Java Code:
    public class Test {
    
    	public static void main(String[] args){
    		int run = 0;
    		do {
    			run = JOptionPane.showConfirmDialog(null,
    					"Would you like to play again?", //Boolean question - good choice for play-again scenario.
    					"Again?", //Dialog box name.
    					JOptionPane.YES_NO_OPTION); //Give user option to go again or not.
    		} while (run == 0); //Loop condition.
    
    	}
    But better yet, the above example show mixing command line and Swing (and violates the EDT single threaded rule to boot), run an example using a GUI, which Fubarable already pointed out the original poster is using - presuming the user wants to start a new game, the game turns into actually trying to start a new game

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.lang.reflect.InvocationTargetException;
    
    public class Test {
    
    	public static void main(String[] args) throws InterruptedException, InvocationTargetException{
    		
    		SwingUtilities.invokeAndWait(new Runnable(){
    			public void run(){
    				JFrame frame = new JFrame();
    				
    				JButton button = new JButton("Infinite loop");
    				button.addActionListener(new ActionListener(){
    
    					public void actionPerformed(ActionEvent arg0) {
    						int run = 0;
    						do {
    							run = JOptionPane.showConfirmDialog(null,
    									"Would you like to play again?", //Boolean question - good choice for play-again scenario.
    									"Again?", //Dialog box name.
    									JOptionPane.YES_NO_OPTION); //Give user option to go again or not.
    						} while (run == 0); //Loop condition.
    					}
    					
    				});
    				frame.getContentPane().add(button);
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.pack();
    				frame.setVisible(true);
    				
    			}
    		});
    		
    
    	}
    }
    Rather than continue to hijack the original posters thread, I encourage you to start your own. I also encourage you to take advice and comments on your code for what it is - advice and comments.
    Last edited by doWhile; 06-17-2012 at 04:58 AM.

  13. #13
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    Ok dude... I tried it...
    IT WORKS PERFECTLY FINE!
    Isn't it supposed to loop when you click yes???
    I suspect you did something incorrectly.. firstly you didn't import swing... and you missed a container to close the class. Run exactly what you see below:

    Java Code:
    import javax.swing.*;
    public class Test
    {
    
        public static void main (String[] args)
        {
            int run = 0;
            do
            {
                String name = JOptionPane.showInputDialog ("What is your name?");
                JOptionPane.showMessageDialog (null,
                        "Hello " + name);
                run = JOptionPane.showConfirmDialog (null,
                        "Would you like to play again?",
                        "Again?",
                        JOptionPane.YES_NO_OPTION);
            }
            while (run == 0);
            JOptionPane.showMessageDialog (null,
                    "If you see this message, you clicked no and the loop is broken.\n"
                    + "Click OK to end the program.");
        }
    }
    I'm not going to continue arguing this since as you say I'm now a hijacker.

    "There's no use preaching into an ignorant mind."
    - Personal.

  14. #14
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Game restart

    Isn't it supposed to loop when you click yes???
    Yes, it is. But first, doing so in the manner suggested in the code posted violates the single threaded Swing rule by running Swing methods on another thread. Second, place it in the context of a User interface, the context that we are guessing (key word guess) the original poster has (as my second example illustrates), and you tie up the EDT - hence, a loop that doesn't start a new game when the user wants to. I admire your desire to help. I don't admire your refusal of help when someone else tries. I recommend reading a bit about Swing, its threading rules, and what one can and cannot do in that context
    Last edited by doWhile; 06-17-2012 at 05:37 AM.

  15. #15
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    Well.. what can I say, this works for me every time. I for one one (and I'm sure any new java user will see it this way) don't care about some dumb "single threaded swing rule." (With all due respect) The code works perfectly fine, end of story. As far as I'm concerned the route you and the other guy is suggesting is very complex, which person new to java wants to use methods??? (KISS) I didn't suggest anybody wrong, I just suggested a different and much simpler route.

    Regardless of rules, ask yourself these questions on both our methods of solving:

    - Does the code work?
    - Is it the easiest route to take for someone new?
    - Does it sufficiently & effectively fulfill it's purpose?


    We all know the answer is yes to all for mine, once it's implemented correctly.

    "If you take your time to hear me... maybe you can learn to cheer me."
    - Tupac Shakur.
    Last edited by Tha Boss; 06-17-2012 at 05:58 AM. Reason: Clarification

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Game restart

    Quote Originally Posted by Tha Boss View Post
    ... I for one one (and I'm sure any new java user will see it this way) don't care about some dumb "single threaded swing rule." (With all due respect) ....
    Well then, I guess we know how much weight to give to your Swing coding advice now, don't we.
    doWhile likes this.

  17. #17
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Game restart

    My friends, I don't care how you twist it and turn it. Because as far as I can see you guys just don't want to admit that I had the better answer. All you did was try to poke holes in my barrel when my code works perfectly fine. Blatant reality: you're all Just being counterproductive. Judge it the way you guys want, I don't care.
    "Misunderstandings leads to misjudgments."
    - Personal.

  18. #18
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Game restart

    don't care about some dumb "single threaded swing rule." (With all due respect) The code works perfectly fine, end of story. As far as I'm concerned the route you and the other guy is suggesting is very complex, which person new to java wants to use methods???
    Quote Originally Posted by Tha Boss View Post
    My friends, I don't care how you twist it and turn it. Because as far as I can see you guys just don't want to admit that I had the better answer. All you did was try to poke holes in my barrel when my code works perfectly fine. Blatant reality: you're all Just being counterproductive. Judge it the way you guys want, I don't care.
    Thanks...I needed a good laugh today.

Similar Threads

  1. Making a game restart? :/
    By AndroidAppNewbie in forum New To Java
    Replies: 2
    Last Post: 02-19-2011, 10:11 AM
  2. Restart gif animation possible?
    By Addez in forum New To Java
    Replies: 12
    Last Post: 01-25-2011, 09:29 PM
  3. Replies: 5
    Last Post: 09-26-2010, 03:44 PM
  4. Restart main
    By Syahrul in forum New To Java
    Replies: 3
    Last Post: 11-22-2009, 08:34 AM
  5. Application restart in tomcat.....
    By jithan in forum New To Java
    Replies: 1
    Last Post: 09-10-2008, 03:31 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
  •