Trying to program a game.

Started by
7 comments, last by AlysiumX 11 years, 9 months ago
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]
Advertisement
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.
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.

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.

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();
}
}


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?

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.
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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?

This topic is closed to new replies.

Advertisement