I have downloaded a snake game project in java. Initially the project contains three java files i.e "
Engine.java
Snake.java
GameBoard.java
And Engine.java have the main() method, when i run this Engine.java class game starts running.
And to improve the user iteractivity i have created two JFrames :"PlayGame.java", Rules.java
Now this project having five java classes in this project-
Engine.java(containing main() method)
Snake.java
GameBoard.java
PlayGame.java(is a JFrame)
Rules.java(is a JFrame)
PlayGame.java have three buttons
Play - i want when play button getclicked snake game start/run.
Rules - when clicked Rules.java Jframe should be opened
Exit - exits the application
Now what i want is at first "PlayGame.java" JFrame should appear and throw this game should start i.e when i click play button game should start But problem i am facing is "playGame.java" JFrame and "game screen" appearing at the same time .thats what i don't want in my project
Here is the code that i have included in main method
public static void main(String[] args) {
new PlayGame().setVisible(true);
JFrame frame = new JFrame("SnakeGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
Canvas canvas = new Canvas();
canvas.setBackground(Color.black);
canvas.setPreferredSize(new Dimension(GameBoard.MAP_SIZE *GameBoard.TILE_SIZE,GameBoard.MAP_SIZE * GameBoard.TILE_SIZE));
frame.add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
new Engine(canvas).startGame();
}
Here is the startGame() method which is in Engine.java class
public void startGame() {
canvas.createBufferStrategy(2);
Graphics2D g = (Graphics2D)canvas.getBufferStrategy().getDrawGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
long start = 0L;
long sleepDuration = 0L;
while(true) {
start = System.currentTimeMillis();
update();
render(g);
canvas.getBufferStrategy().show();
g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
sleepDuration = (1500L / UPDATES_PER_SECOND) - (System.currentTimeMillis() - start);
if(sleepDuration > 0) {
try {
Thread.sleep(sleepDuration);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}