Books are failing me!

Hey guys,

     So I have both Killer Game Programming in Java and Developing Games in Java. Im trying desperately to use them and work through them, and I am getting NOWHERE. Before I had these books, I used my own knowledge and help from you guys and managed to put together a decent game of pong. I thought I could dive into these books to get better and do things the "right" way, but all thats happening is confusion. KGPIJ is talking about a million different ways to achieving certain FPS, and then has a bunch of unfinished, broken methods. Im trying to get something out of these but Im getting discouraged. Is it recommended to just forget these "official" books and just use trial and error along with the few online tutorials that exist? I have some links that have been suggested such as Kevs Space Invader tutorial and a few others. In your experience, is it possible to get good at this by just writing code, asking for help, and fixing it until it works?? Right now Im attempting to find some "path" that I seem to be failing at discovering.

I personally don’t think there is a “right” way to learn. Everyone learns differently and you need to find a way that suits you. Some people can learn from just reading, others can’t understand the concept unless it is presented in code form. Yet others learn from watching others.

If you find reading the book confusing, I think you should start search for online tutorials/exercises that are right for you. Once you have some of the basics down, go back to the book and it will make more sense.

I have never used books except for occasional reference - they just don’t work for me. Instead, I decide on a project to make (Pong for example), then make it using hackery, crappy code, and a lot of plagiarism from examples on the net. Eventually I learn what it all means from doing that enough times. :slight_smile:

Although to be honest these days I don’t do that either, because I know so many programming languages that I don’t need to. Instead I just start writing code and debug it as I go.

Probably half of us learned by hacking together stuff before we had access to the Internet (let alone knew where to look for help). So just trying, failing, and trying again clearly works for some.

You sure the books aren’t to advanced for you? there’s usually a certain progression you want to follow, the lower level books help you get ready for the higher level books. I’m usually a fan of books when i can get my hands on them, there’s to many tutorials out there and they randomly become useless in some instances. Or they don’t end up teaching me what i’m looking for. With a book it knows what it’s taught you and points you to where you need to go next.

Anyway i wouldn’t give up on all books if it’s just two that your having trouble with… buut~ if you can manage to learn this without a book and would rather do it go for it. Some people can manage it, i just can’t imagine that it’s really easier.

I recall “failing” at trying to learn about Java game programming from a book by an author named Harbour. The example program was based on Asteroids. I do recall being able to change some parameters here and there, getting some of the code working. But a lot of it was incantation as far as I could tell at the time. I had to move on in order to get a better foundation in Java programming.

I would like to suggest putting some specific questions from the books you are reading onto the forum, including examples of what seems to be buggy code. It would be a lot easier to give advice if we knew more specifics. “I’m getting nowhere with books. Should I give up reading?” That’s not a lot to work on.

I have both books, but haven’t finished them. The reason being, that I started with my on game program and ideas, and used the book to improve what I already had. If the book had something that I didn’t think worked for me, I chunked it. I recommend you start with the game program you have, and as you read the book, see if it is something that you can use in your game program to improve it.
You are right the first chapter of KGPJ spent way to much time going over FPS loops. Just skip to his final game loop and see if it’s something you can use.

I’m in the same boat as the original poster. I’ve been trying to read these books but they’re old and the java3d timer is deprecated. This board doesn’t seem to help because there aren’t any basic tutorials. Every night I spend all my time trying to find something that isn’t deprecated or unclear. It’s depressing and I’m considering not returning to java game development.

The best way is to make your own game loop:


int FPStime = 1000/FPS;

while(!isGameOver) {
	long startTime = System.currentTimeMillis();
	//GAME CODE
	try{
		long sleepTime = FPStime - (System.currentTimeMillis()-startTime);
		Thread.sleep((int)sleepTime);
	} catch(Exception exc) {
		exc.printStackTrace();
	}
}

Thanks for all of the help guys. I think my problem is that Im trying to use the books in a logical order (like a book should be) but in reality I need to use them more as a reference to fix my problems. I think Im just going to hack at my own ideas and designs until I run into a wall, and then either post here or figure it out with the internet or these books. Then from that I can build better ways to do things and start making sense of all this. Any other suggestions are always appreciated! :slight_smile:

I actually disagree with a tiny point in this code. The use of currentTimeMillis over nanoTime. Maybe I am just naive but I’ve read in a few places that depending on the os currentTimeMillis is slower overall then nanoTime. The one advantage it has, is being compatible pre 5.0?

For some weird reason, nanoTime is NOT the same as currentTimeMillisx1000000.
For example, this does not work:


int FPStime = 1000000/FPS;
 
while(!isGameOver) {
    long startTime = System.nanoTime();
    //GAME CODE
    try{
        long sleepTime = FPStime - (System.nanoTime()-startTime);
        Thread.sleep(0,(int)sleepTime);
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

Look up nano versus milli.

mill = 1/1000
nano = 1/1000000000

1/1000 of a millisecond is not a nanosecond, it’s a microsecond. A nanosecond is 1/1000 of a microsecond.

That aside, currentTimeMillis() is less accurate than nanoTime() so I wouldn’t expect them to always line up exactly.

Oh how could I make such a stupid mistake :stuck_out_tongue:

However, millis and nano are very different, since if you look it up, System.nanoTime() is not from January 1, 1970.

Updated:


int FPStime = 1000000000/FPS;
  
while(!isGameOver) {
    long startTime = System.nanoTime();
    //GAME CODE
    try{
        long sleepTime = FPStime - (System.nanoTime()-startTime);
        int m = (int)(sleepTime/1000000);
        int n = (int)(sleepTime%1000000);
        Thread.sleep(m,n);
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

Why does the epoch matter? On the other hand, you’re going to get compile-time errors for exceeding the range of int. With nanos, use long.

In my learning process (which is going now), I always start from scratch. When meet a problem

  1. try break the problem by myself
  2. no result, read some books
  3. no result, post here :slight_smile:

just use any way that suit you. for me, try and error is the best.

Thread.sleep(m,n);

Oh, how we all wished that method did what it suggested… it actually rounds the nanos to the nearest millisecond and sleeps for the calculated amount in milliseconds (at which it is very inaccurate).

It’s a big joke, really.

Seriously? That’s disgusting.

What?!? Well this makes me mad, since I just starting using this for my games >:(

Bahaha agreed ;D

Might be interesting to try testing the two methods. But I’m not at all sure as to the best way to go about that. Perhaps, at the given time, print to file or system.out the trigger time and the current time by either method? Hmmm. There is probably something very wrong with that.

It’s always nice to get some sort of statement as to the expected tolerances, rather than just “it’s inaccurate” which could mean a lot of different things. :stuck_out_tongue: