I am new to java and I am making a snake game. I worked on it step by step and I reached adding the play again button and start button. I started with the play again and I keep getting errors on line:183 in the gameover method . I wish if someone can explain to me why I keep getting them and what is a possible way to solve it. I have some comments so it can help understand why I have those lines. Thank you in advance!
edit 1: line 183 is if(event.getSource()==replayButton){ the error says ';' expected on the first paranthesis (
edit 2: This is the smallest amount of code possible. I am trying to add a play again button in the gameover method. (I have the high score printed and game over printed, I am trying to add a button and if it is pressed, I wan the game to reset but still have the same score of last times)
GamePanel class
import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.util.Random; public class GamePanel extends JPanel implements ActionListener { static final int SCREEN_WIDTH = 380; //intially 600 static final int SCREEN_HEIGHT = 380; static final int UNIT_SIZE = 20; //how big the items will be on the screen including the grid lines for a better understanding(intially 25) static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT)/UNIT_SIZE; static final int DELAY = 150; //the higher the number the slower the game will be and vise versa final int x[] = new int[GAME_UNITS]; final int y[] = new int [GAME_UNITS]; int bodyParts = 3; int applesEaten; int highScore = 0; int applex; int appley; char direction = 'R'; boolean running = false; Timer timer; Random random; private JButton replayButton; public GamePanel(){ random = new Random(); this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT)); this.setBackground(Color.white); this.setFocusable(true); this.addKeyListener(new MyKeyAdapter()); 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(running){ /* //grid lines to help visualize for(int i=0; i<SCREEN_HEIGHT/UNIT_SIZE; i++){ g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT); g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE); } */ g.setColor(Color.red); g.fillOval(applex, appley, UNIT_SIZE, UNIT_SIZE); for(int i =0; i<bodyParts; i++){ if(i == 0){ g.setColor(Color.green); g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE); } else{ g.setColor(new Color(45, 180, 0)); g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))); g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE); } } g.setColor(Color.red); g.setFont(new Font("Int Free", Font.BOLD, 25) ); FontMetrics metrics = getFontMetrics(g.getFont()); g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics.stringWidth("Score: " + applesEaten))/2, g.getFont().getSize()); } else{ gameOver(g); } } public void gameOver(Graphics g){ //display the score g.setColor(Color.red); g.setFont(new Font("Int Free", Font.BOLD, 25) ); FontMetrics metrics1 = getFontMetrics(g.getFont()); g.drawString("Score: " + applesEaten, (SCREEN_WIDTH - metrics1.stringWidth("Score: " + applesEaten))/2, g.getFont().getSize()); //display the game over text g.setColor(Color.red); g.setFont(new Font("Int Free", Font.BOLD, 60) ); FontMetrics metrics2 = getFontMetrics(g.getFont()); g.drawString("Game Over", (SCREEN_WIDTH - metrics2.stringWidth("Game Over"))/2, SCREEN_HEIGHT/2); //display the highscore message if(applesEaten > highScore){ highScore = applesEaten; } g.setColor(Color.red); g.setFont(new Font("Int Free", Font.BOLD, 25) ); FontMetrics metrics3 = getFontMetrics(g.getFont()); g.drawString("High Score: " + highScore, (SCREEN_HEIGHT - metrics3.stringWidth("High Score: " + highScore))/2, SCREEN_HEIGHT-10); replayButton = new JButton(); replayButton.setText("Play Again"); replayButton.setSize(100, 50); replayButton.setLocation((SCREEN_HEIGHT - metrics3.stringWidth("Play Again" + highScore))/2, SCREEN_HEIGHT-20); replayButton.addActionListener(this); this.add(replayButton); @Override public void actionPerformed( ActionEvent event ){ if(event.getSource()==replayButton){ startGame(); } } } @Override public void actionPerformed( ActionEvent e){ if(running){ move(); checkApple(); snakeCollision(); } repaint(); }
https://stackoverflow.com/questions/66961345/snake-game-errors-play-again-button-error April 06, 2021 at 08:34AM
没有评论:
发表评论