• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AlysiumX

Trying to program a game.

8 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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:
[CODE]if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.[/CODE]
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='DevLiquidKnight' timestamp='1341273152' post='4955110']
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:
[CODE]if(date.get(Calendar.MILLISECOND) < oldSecond + 1000) //if a second has passed.[/CODE]
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.
[/quote]

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

Share this post


Link to post
Share on other sites
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Update();
}
Draw();
}
}
[/CODE]
This isn't an accurate representation of frames per second. The code should be changed to the following:
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Draw();
}
Update();
}
}
[/CODE]
-2

Share this post


Link to post
Share on other sites
[quote name='DevLiquidKnight' timestamp='1341274373' post='4955114']
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Update();
}
Draw();
}
}
[/CODE]
This isn't an accurate representation of frames per second. The code should be changed to the following:
[CODE]
private void GameLoop(){
while(running){
if (within frames per second){
Draw();
}
Update();
}
}
[/CODE]
[/quote]

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?

[url="http://imageshack.us/photo/my-images/802/limitdrawexample.png/"]http://imageshack.us...rawexample.png/[/url]

Which would make the draw part of the program drop frames? Edited by AlysiumX
0

Share this post


Link to post
Share on other sites
[quote name='AlysiumX' timestamp='1341277235' post='4955125']
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.
[/quote]
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 [url="http://www.koonsolo.com/news/dewitters-gameloop/"]link[/url] on different FPS handling methods. Edited by DevLiquidKnight
0

Share this post


Link to post
Share on other sites
The way in which commercial game engines work is that the logic and rendering are separate, and the logic—[i]not the rendering[/i]—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 [url="http://lspiroengine.com/?p=378"]here[/url].

Your original post is disheartening because you asked how an [i]engine[/i] 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 [url="http://www.gamedev.net/blog/355/entry-2250186-designing-a-robust-input-handling-system-for-games/"]this[/url] to understand how a robust input system works on the engine side.
And [url="http://www.gamedev.net/topic/615796-input-manager-mapping-functionality/"]this[/url] to understand how to map functionality to in-game actions.


L. Spiro Edited by L. Spiro
0

Share this post


Link to post
Share on other sites
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 :
[url="http://www3.ntu.edu.sg/home/ehchua/programming/java/J8d_Game_Framework.html"]http://www3.ntu.edu...._Framework.html[/url]
and
[url="http://www.youtube.com/thejavahub"]http://www.youtube.com/thejavahub[/url]

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0