Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
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();
				}
			}
		}
	}
Posted
Updated 24-Sep-13 19:34pm
v4
Comments
Shubhashish_Mandal 24-Sep-13 7:57am    
you have to open the game frame when "start" button clicked, so you have to do this in "start" button action instead of the main().
Member 10276989 24-Sep-13 8:46am    
Sir i have added the same code in actionPerformed() method of 'play' button, but problem i am facing is snake is not moving ad also gameframe is hangig too

1 solution

Java
public static void main(String[] args) {
    new PlayGame().setVisible(true); // probably opens a playGame-JFrame
 
    JFrame frame = new JFrame("SnakeGame"); // creates 2. JFrame
    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(); // might start some more
 
         }


Please see comments.
Please try to understand Java Basics first.
It's worth nothing if you do not know what you're doing.

Java Tutorials[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900