Brick Breaker game running slow

Hi everyone! This is my first post on this forum. I am on the path of learning java for making games. I have close to intermediate level of java when it comes to making normal swing applications and such but i am new to gaming. I made a brick breaker game to get started but it is skipping frames and running slow. It even stops in the middle while i am trying to play. It shoots up the CPU usage to 15 to 20% as well. Please have a look at the code. Thank you!

http://pastebin.java-gaming.org/6dced309e201c

Well, first off, you’re using Java2D. Which isn’t really good. I know, it’s hypocritical for me to be criticizing the tool I use. Anyway, also, you’re making 14 instances of Brick here:

And right after that, you make an ArrayList of Bricks. The key to this is to just use the array list and do brickList.add(new Brick()); 14 times instead of creating 14 instances. Something like this:


for (int i = 0; i < 14; i++) {
brickList.add(new Brick(...));
}

That takes care of those instances. Then, to render, just do this:


for (int i = 0; i < brickList.size(); i++) {
brickList.get(i).drawBrick(g2d, brickList.get(i).getColor(), brickList.get(i).getX(), brickList.get(i).getY());
}

You may have to add some getters to your Brick class, but I’m pretty sure this should take care of some of the CPU usage

Absolutely wrong. Even when you inlined the new statement into the add method call, you are still creating an instance. However this is clean, as this is short and easy to read.

@cookiecompiler

The actual issue is your game loop, the swing timer is not reliable for game programming, as it is never guaranteed to be accurate. Instead, use System.nanoTime() and write your own game loop instead. See this post for more information on the topic.

http://gameprogrammingpatterns.com/game-loop.html

The next issue is that you are using Java2D. I won’t say Java2D is very bad, but it isn’t the right choice for programming games, though it can be used to get high frame rates (~120) if used correctly. For starters, I still recommend Java2D. Once you start feeling the limitations, you can move to Mercury (similar to Java2D’s Graphics interface, but uses hardware acceleration).

Good luck.

Stop plugging Mercury to newbies. Seriously.

I really like to ask you why, because as I know, it is in a usable state, and not my own (I’m a contributor though). I really don’t want to point newbies to un-finished or heavy WIP engines or libraries, but Mercury is usable as I know it.

Because there are many libraries out there with better structure, performance, community, and support. To put it simply.

To be fair to Java2D, it does use hardware acceleration.

Thank you so much @DarkCart and @SHC for your helpful replies. I have i good experience with Swing and not a lot of experience with Java2D. Do you suggest that i move on to a gaming engine? I read a little bit about LibGDX but i dont know a good way to start learning the engine. Can you please help me in this regard? Because ultimately i will have to move on to a gaming engine. Thanks again

You’re welcome :slight_smile: I say stick with Java2D for a little longer, then maybe switch to something like Slick2D (I know, it’s outdated) so you can get a feel for OpenGL, then move on to LWJGL. I’d honestly would not use LibGDX because the Gradle setup is a nightmare. I know from experience.

You think ??? I think it’s extremely simple, especially importing projects into Eclipse. Gradle seems to make it much simpler.

I wouldn’t advise against LibGDX just because of the setup/Gradle. It’s a powerful tool, great for creating games for different platforms and requires way less knowledge than core LWJGL. They are both great tools and I would advise looking into both and making a decision. I would suggest ignoring Slick2D as it’s pretty much dead. Might as well use something up to date :point:

If CookieCompiler is just interested in making games fast I would suggest LibGDX, If he/she wants to really have complete control over the project and use lots of OpenGL then LWJGL is the way to go. Could even learn both :slight_smile:

What DarkCart said, I agree with him. Use Java2D for some time, and move out of it once you start feeling it’s limitations (Maybe slow performance when lots of entities, or you might want to use effects such as lights in your game). Then, it is entirely up to your interest, if you want to make games fast, then use a framework like LibGDX, but if you want to learn what happens under the hood, LWJGL is the way to go.

I would suggest ignoring Slick2D too, because, it’s author is now using LibGDX himself, and the project is not appearing to be actively maintained. Compared to that, LibGDX is better I think (I never actually used it, I went the LWJGL way).

Thanks. This community is really helpful. I would then go with Java2D for sometime since i feel that it teaches me a lot of things that i need to know. I gives me all the information about what is occurring under the hood. Would you guys please point me to good tutorials or good books that i can learn from? I tried using LibGDX with IntelliJ and yes it was some sort of a mess that i didnt want to deal with at this point. Even though it might be easier to make games with it, i think its not for me at this point. I have decided to continue with Java2D. Please guide me regarding the books thanks!