Angry birds
Angry birds

Reputation: 85

How do I add a restart Mouse Input on the "Game Over" screen in Java

I was following a tutorial on how to create a snake game in Java, I followed it and started to add some features in it like a menu system, Game Over screen. Now on this Game Over screen, I wanted to add a restart button, I used "MouseListener" for this. I also added different states for the menu, game, GameOver.

this is my code for MouseListener:

public class MouseInput implements MouseListener {

    ...

    // Game Over Screen
    if (GamePanel.state == GamePanel.STATE.GAMEOVER) {
        if (mx >= 250 && mx <= 340) {
            if (my >= 350 && my <= 400) {
                // to quit the game
                System.exit(1);

            }
        }
        if (mx >= 250 && mx <= 340) {
            if (my >= 150 && my <= 200) {
                // to restart the game
                // dont know what to put here 
             }   
        }
      ...

I don't know if this is relevant but this is my game panel class where the game making is (warning: a block of code):

public class GamePanel extends JPanel implements ActionListener {

static final int SCREEN_WIDTH =  600;
static final int SCREEN_HEIGHT = 600;
static final int OBJECT_SIZE = 25;
static final int GAME_UNITS = (SCREEN_WIDTH*SCREEN_HEIGHT)/OBJECT_SIZE;
static final int DELAY = 75;
final int x[] = new int[GAME_UNITS];
final int y[] = new int[GAME_UNITS];
int size = 5;
int applesEaten;
int appleX;
int appleY;
char direction = 'D';
boolean running = false;
Timer timer;
Random random;

private Menu menu;
public static enum STATE{
    MENU,
    GAME,
    GAMEOVER
};
public static STATE state = STATE.MENU;

GamePanel() {
        random = new Random();
        this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
        this.setBackground(Color.BLACK);
        this.setFocusable(true);
        this.addKeyListener(new keyAdapter());
        this.addMouseListener(new MouseInput()); 
        menu = new Menu(); 
        startGame();
    }

public void startGame(){
    newApple();
    running = true;
    timer = new Timer(DELAY,this);
    timer.start();
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    draw(g);
}
public void draw(Graphics g) {
    if (state == STATE.GAME) {//******
        if (running) {              
            g.setColor(Color.GREEN);
            g.fillOval(appleX, appleY, OBJECT_SIZE, OBJECT_SIZE);

            for (int i = 0; i < size; i++) {
                if (i == 0) {
                    g.setColor(Color.blue);
                    g.fillOval(x[i], y[i], OBJECT_SIZE, OBJECT_SIZE);
                } else {
                    g.setColor(Color.CYAN);
                    g.fillOval(x[i], y[i], OBJECT_SIZE, OBJECT_SIZE);
                }
            }
            g.setColor(Color.GREEN);
            g.setFont(new Font("Ink Free", Font.BOLD, 35));
            FontMetrics metrics = getFontMetrics(g.getFont());
            g.drawString("Score: "+applesEaten, (SCREEN_WIDTH - metrics.stringWidth("Score: "+applesEaten))/2, g.getFont().getSize());
        }
        else {
            gameOver(g);
        }
    } else if(state == STATE.MENU){ //*****
            menu.render(g);
    }
}
public void newApple(){
    appleX = random.nextInt((int)(SCREEN_WIDTH/OBJECT_SIZE))*OBJECT_SIZE;
    appleY = random.nextInt((int)(SCREEN_HEIGHT/OBJECT_SIZE))*OBJECT_SIZE;

}
public void movement() {
    if (state == STATE.GAME) {
        for (int i = size; i > 0; i--) {
            x[i] = x[i - 1];
            y[i] = y[i - 1];
        }
        switch (direction) {
            case 'U':
                y[0] = y[0] - OBJECT_SIZE;
                break;
            case 'D':
                y[0] = y[0] + OBJECT_SIZE;
                break;
            case 'L':
                x[0] = x[0] - OBJECT_SIZE;
                break;
            case 'R':
                x[0] = x[0] + OBJECT_SIZE;
                break;
        }
    }
}
public void checkApple() {
    if (state == STATE.GAME) { //*******
        if ((x[0] == appleX) && (y[0] == appleY)) {
            size++;
            applesEaten++;
            newApple();
        }

    }
}
public void checkCollisions() {
    if (state == STATE.GAME) { //*******
        //checks if head collides with body
        for (int i = size; i > 0; i--) {
            if ((x[0] == x[i]) && (y[0] == y[i])) {
                running = false;
            }
        }
        //check if head touches left border
        if (x[0] < 0) {
            running = false;
        }
        //check if head touches right border
        if (x[0] > SCREEN_WIDTH) {
            running = false;
        }
        //check if head touches top border
        if (y[0] < 0) {
            running = false;
        }
        //check if head touches bottom border
        if (y[0] > SCREEN_HEIGHT) {
            running = false;
        }

        if (!running) {
            timer.stop();
        }

    }
}
public void gameOver(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    if (!running) {
        state = STATE.GAMEOVER;
        Rectangle play = new Rectangle(250, 150, 100, 50);
        Rectangle quit = new Rectangle(250, 350, 100, 50);

        Font fnt = new Font("arial", Font.BOLD, 70);
        Font fnt1 = new Font("arial", Font.BOLD, 30);
        g.setFont(fnt);
        g.setColor(Color.RED);
        g.drawString("GAME OVER", 90, 100);

        g.setFont(fnt1);
        g.drawString("Play", play.x + 20, play.y + 30);
        g2d.draw(play);
        g.drawString("Quit", quit.x + 20, quit.y + 30);
        g2d.draw(quit);

        g.setColor(Color.GREEN);
        g.setFont(new Font("Ink Free", Font.BOLD, 35));
        FontMetrics metrics2 = getFontMetrics(g.getFont());
        g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics2.stringWidth("Score: " + applesEaten)) / 2, g.getFont().getSize());
    }

}
    public void actionPerformed(ActionEvent a) {

    if (running) {
        movement();
        checkApple();
        checkCollisions();
    }


    repaint();
}
public class keyAdapter extends KeyAdapter{

    public void keyPressed(KeyEvent e){
        if(state == STATE.GAME) { //******
            switch (e.getKeyCode()) {
                case KeyEvent.VK_LEFT:
                    if (direction != 'R') {
                        direction = 'L';
                    }
                    break;
                case KeyEvent.VK_RIGHT:
                    if (direction != 'L') {
                        direction = 'R';
                    }
                    break;
                case KeyEvent.VK_UP:
                    if (direction != 'D') {
                        direction = 'U';
                    }
                    break;
                case KeyEvent.VK_DOWN:
                    if (direction != 'U') {
                        direction = 'D';
                    }
                    break;
            }
        }
    }
}

I'm sorry for the long code :( Anything helps, Thanks!

Upvotes: 0

Views: 81

Answers (1)

Rocco
Rocco

Reputation: 1118

You must reset all the variables to the initial values for a new game, simplest way is to define a new method newGame(), the call this method in the MouseListener (I'm assuming that your MouseInput class is an inner class of GamePanel).

public void newGame() {
    applesEaten=0;
    size=INITIAL_SIZE;
    newApple();
    state=STATE.GAME;
    running=true;
    Arrays.fill(x, 0);
    Arrays.fill(y, 0);
    direction = 'D';
    timer.start();
}

Said that, you should rethink a bit the state changes, since you set GAMEOVER when the timer is stopped, instead of stopping the timer AND set GAMEOVER immediately, you should get rid of running variable, since state should be enough for all your needs.

Upvotes: 1

Related Questions