Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (799)
Games in Android Showcase (236)
games submitted by our members
Games in WIP (865)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to reset the game ?  (Read 2629 times)
0 Members and 1 Guest are viewing this topic.
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Posted 2014-07-18 05:34:55 »

I'm using Graphics2D with java and I want to reset the game if the alien ship hit my ship.
I tried some different things but didn't worked.

Here's the JFrame class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Window extends JFrame {

    public Window() {
        add(new Screen());
        setTitle("Square Invaders!");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(900, 560);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);  
    }

    public static void main(String[] args) {
        new Window();
    }
}



Here's the part of the code where I check if the game ended which resides in the
Sceen.java class (extends JPanel):


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paintComponent(g);
        if (ingame) {
            if (spaceShip.isVisible()) {
                drawSpaceShip(g);
            }
            drawAlienShip(g);

            g2d.setColor(Color.white);
            g2d.drawString("Squares left: " + aliens.size(), 5, 15);
        } else {
            String msg = "Game Over";
            Font small = new Font("Viner Hand ITC", Font.BOLD, 36);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (S_WIDTH - metr.stringWidth(msg)) / 2,
                    S_HEIGHT / 2 - metr.getHeight());
        }
        g.dispose();
    }



I tried to do this(by adding the : new Window()):


1  
2  
3  
4  
5  
6  
            g.drawString(msg, (S_WIDTH - metr.stringWidth(msg)) / 2,
                    S_HEIGHT / 2 - metr.getHeight());
            new Window();
        }
        g.dispose();
    }



the result is a window after window is being created !
Offline GNecro1
« Reply #1 - Posted 2014-07-19 08:57:21 »

well you need to reset everything score player .............
just when you do like
1  
player = new Player(....);

just repeat that! And reset score,lives ....... You don't need to create a new window!
I hope i helped!

Java freak! Cheesy
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #2 - Posted 2014-07-19 13:35:29 »

So do I have to reset the panel ? which is inside the Frame.
or just rest all the other entities that reside inside the panel ? is that what you mean ?

and is it possible to just call the constructor of the Panel which will reset everything by writing : new Panel() ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline hwinwuzhere
« Reply #3 - Posted 2014-07-19 13:45:28 »

1  
player = new Player(....);


You just need to reset all game objects (instantiate them again), as GNecro said. You could make an initialization method (which sets up the game objects) and run that method every time you need to reset the game.

There are two kinds of people in this world: Those who can extrapolate from incomplete data,
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #4 - Posted 2014-07-19 13:49:47 »

Done! issue Solved!
thanks GNecro1
Offline Phased
« Reply #5 - Posted 2014-07-19 13:50:12 »

If you want to reset a game, you don't want to "close the game" and restart it within the application.

To achieve the reset, just reset any variables back to the default.

I am going to assume you have an array of aliens, where you first put the aliens in the array, you could put it into a new method

1  
2  
3  
4  
5  
6  
7  
8  
9  
   public void createLevel(){
      aliens = new Alien[levelSize];
      for(int i = 0; i < levelSize; i++){
         alien[i] = new Alien();
      }
     
      //recreate or have a method inside player to change it back to the default values
      //and anything else you need to create for your game level
   }


something like that, and you can create new levels of any size pretty fast.

Edit: Ninjad, I hate formatting inside the post, so I always open up eclipse to format Sad just as i finished the code you posted it, but it was to much effort to delete!
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #6 - Posted 2014-07-19 13:54:47 »

Awesome Phased ! now I also learned how to make levels ! I had no idea I could do it in that simple way

Edit: LOL, no please don't delete it, I learned something new from that code you posted Phased.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (183 views)
2019-09-04 15:33:17

hadezbladez (4859 views)
2018-11-16 13:46:03

hadezbladez (1783 views)
2018-11-16 13:41:33

hadezbladez (5131 views)
2018-11-16 13:35:35

hadezbladez (1013 views)
2018-11-16 13:32:03

EgonOlsen (4356 views)
2018-06-10 19:43:48

EgonOlsen (5213 views)
2018-06-10 19:43:44

EgonOlsen (2940 views)
2018-06-10 19:43:20

DesertCoockie (3865 views)
2018-05-13 18:23:11

nelsongames (4290 views)
2018-04-24 18:15:36
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!