Jump to content

  • Log In with Google      Sign In   
  • Create Account


Trying to program a game.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 AlysiumX   Members   -  Reputation: 152

Like
0Likes
Like

Posted 02 July 2012 - 05:30 PM

So am at the early stages of developing a game in java. I wanted to attempt to build a game based off of knowledge of how an engine is suppose to work vs actually copying code from some one else. Now I just put an image to my canvas and I was attempting to move it "smoothly" using keybindings from it. However when I try to move the image, it doesn't seems to show movement for a few seconds after I hit the button. I am sure it has to do with my main game loop, can you please take a look and let me know if I am doing something wrong?

Other notes:
- I am setting booleans for movement on my keybindings(using pressed and released)
- I am updating the image position in my update() functions using above boolean.
- I have a JPanel added to the main JApplet which I am drawing to by overridding the paintComponent method on(which i hate btw, wish i could add it to the main game loop and always call it instead along with getting key inputs)



[source lang="java"]public void run() { int frameCount = 0; //Used for time Calendar date; date = Calendar.getInstance(); //Get the latest instance of date int oldSecond = date.get(Calendar.MILLISECOND); //Get the current second. int timeLeft = 0;//We delay this time after we've updated to the framerate target. while(running){ date = Calendar.getInstance(); //Update Second if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed. { if(frameCount != TARGET_FRAMERATE) //Limit target frame rate. { frameCount++; Update(); }else{ //Get the time left in milliseconds timeLeft = (oldSecond + 1000) - date.get(Calendar.MILLISECOND); try { fps = frameCount; //Get frames per second. Thread.sleep(timeLeft); //and sleep the game thread for that amount of time. frameCount = 0; //Reset frame count. oldSecond = date.get(Calendar.SECOND); //Reset old second. } catch (InterruptedException e) { e.printStackTrace(); } } } } }[/source]

Edited by AlysiumX, 02 July 2012 - 05:32 PM.


Sponsor:

#2 DevLiquidKnight   Members   -  Reputation: 834

Like
0Likes
Like

Posted 02 July 2012 - 05:52 PM

You could try putting the code that updates the keyboard polling, into a separate thread, and it would likely stop happening. I cant see to much wrong with this setup. I am curious about this part though:
if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.
Why do you wait a second before updating the game? If your trying to limit frame rates just use a sleep command when it goes over your desired frame rate.

Edited by DevLiquidKnight, 02 July 2012 - 05:54 PM.


#3 AlysiumX   Members   -  Reputation: 152

Like
0Likes
Like

Posted 02 July 2012 - 06:02 PM

Alright after further review I can see a few things I am doing wrong with the code, I was trying to work off the process that I sleep a thread to allow drawing time. However now I realize I am doubling up my efforts by manually capping the framerate and also sleeping which is stopping draw cycles. I am now getting closer to what I am looking for, here is the current code.

[source lang="java"]public void run() { int frameCount = 0; //Used for time Calendar date; date = Calendar.getInstance(); //Get the latest instance of date int oldSecond = date.get(Calendar.MILLISECOND); //Get the current second. int timeLeft = 0;//We delay this time after we've updated to the framerate target. while(running){ date = Calendar.getInstance(); //Update Second if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed. { if(frameCount != TARGET_FRAMERATE) //Limit target frame rate. { frameCount++; Update(); } }else{ //Get the time left in milliseconds //timeLeft = (oldSecond + 1000) - date.get(Calendar.MILLISECOND); fps = frameCount; //Get frames per second. frameCount = 0; //Reset frame count. oldSecond = date.get(Calendar.SECOND); //Reset old second. } //Draw the canvas gameCanvas.paintComponent(gameCanvas.getGraphics()); } }[/source]

The goal I am really trying to accomplish is to build a game loop that follow the convention I have seen with the explanation of how it works. So :

[source lang="java"]private void Init(){ //Initialize game objects here. running = true; GameLoop();}private void GameLoop(){ while(running){ if (within frames per second){ Update(); } Draw(); }}private void Update(){ GetInput(); //Update game objects here}private void Draw(){ //Draw game objects here}private void GetInput(){ //Get key input}[/source]

Two questions :
Can you get the key inputs in java the same way you getGraphics?

Why is a JPanel/Canvas perferred vs just doing getGraphics on the main JFrame/JApplet?

Thanks.

#4 AlysiumX   Members   -  Reputation: 152

Like
0Likes
Like

Posted 02 July 2012 - 06:06 PM

You could try putting the code that updates the keyboard polling, into a separate thread, and it would likely stop happening. I cant see to much wrong with this setup. I am curious about this part though:

if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.
Why do you wait a second before updating the game? If your trying to limit frame rates just use a sleep command when it goes over your desired frame rate.


Sorry that comment is kind of misleading, It should really read, //if a second has not passed.

#5 DevLiquidKnight   Members   -  Reputation: 834

Like
-2Likes
Like

Posted 02 July 2012 - 06:12 PM

private void GameLoop(){
  while(running){
    if (within frames per second){
	  Update();
    }
    Draw();
  }
}
This isn't an accurate representation of frames per second. The code should be changed to the following:
private void GameLoop(){
  while(running){
    if (within frames per second){
	  Draw();
    }
    Update();
  }
}


#6 AlysiumX   Members   -  Reputation: 152

Like
0Likes
Like

Posted 02 July 2012 - 07:00 PM

private void GameLoop(){
  while(running){
	if (within frames per second){
	  Update();
	}
	Draw();
  }
}
This isn't an accurate representation of frames per second. The code should be changed to the following:
private void GameLoop(){
  while(running){
	if (within frames per second){
	  Draw();
	}
	Update();
  }
}


ive been confused on this for quite some time, so you limit the draw rate by frame per second not the update?

I don't understand this concept.

Wouldn't this happen?

http://imageshack.us...rawexample.png/

Which would make the draw part of the program drop frames?

Edited by AlysiumX, 02 July 2012 - 07:56 PM.


#7 DevLiquidKnight   Members   -  Reputation: 834

Like
0Likes
Like

Posted 02 July 2012 - 08:59 PM

ive been confused on this for quite some time, so you limit the draw rate by frame per second not the update?
I don't understand this concept.

Frame rate is the frequency or rate at which unique consecutive images are drawn to the screen. So delaying the updating of say the keyboard polling, movement and the physics, does not correlate to frame rate. The human eye cannot even really detect changes in high frame rates.

Check out this link on different FPS handling methods.

Edited by DevLiquidKnight, 02 July 2012 - 09:00 PM.


#8 L. Spiro   Crossbones+   -  Reputation: 13437

Like
0Likes
Like

Posted 03 July 2012 - 05:52 AM

The way in which commercial game engines work is that the logic and rendering are separate, and the logic—not the rendering—is limited.

Render every cycle, update only once for a given fixed amount of time.
The implementation is a bit tricky to get right, but I have documented it here.

Your original post is disheartening because you asked how an engine should handle movements [of your character] etc., which means you have not yet really made a proper distinction between engine tasks and game tasks.
But that should come with age.

For now, read this to understand how a robust input system works on the engine side.
And this to understand how to map functionality to in-game actions.


L. Spiro

Edited by L. Spiro, 03 July 2012 - 05:52 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#9 AlysiumX   Members   -  Reputation: 152

Like
0Likes
Like

Posted 03 July 2012 - 07:42 AM

Thanks for the clarification and those links.

I think I may just need to go back to basics.

Any one know where I can find some really good java game programming tutorial?

I have found some tutorials like :
http://www3.ntu.edu...._Framework.html
and
http://www.youtube.com/thejavahub

But I am not sure of the quality of these tutorials. Any one have any links to tutorials that they would stand by as a "Good" game creation tutorial?

Edited by AlysiumX, 03 July 2012 - 07:43 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS