0
\$\begingroup\$

I have just finished a 2D game in Java that I've been working on for a while.

I found that on some computers it runs fine, and on others (which are not particularly slow in general), the game runs a little slower, and with minor interruptions every second or so. Not completely smooth.

Since I am a beginner to making games, and probably since I didn't plan everything about the game in advance, the code of the game is rather long, clumsy and probably inefficient.

I want to improve this in my next projects. So my question is - In general, what would be the main causes for a common 2D game to slow down on a computer?

What should I pay the most attention to, next time, in order to design an efficient game?

  • Making a small amount of classes? (Even if the classes are small ones?)
  • Avoiding repetition of code? (Even small sections of code, such as short if statements).
  • Avoiding too many threads running?
  • Anything else?

Obviously, all of the above are recommended for an efficient program.

But I'd like to know, what in a game's code, could be especially significant for making an efficient application, and what would be less important and will not save significant amounts of memory.

Any advice would be welcome - could be regarding game design, or regarding more specific coding issues.

I don't know if this matters, but please note that I'm talking mainly about real-time games, using a 'game-loop' that constantly updates the game and the dispaly.

Thank you

\$\endgroup\$

closed as too broad by concept3d, Josh Jan 24 '14 at 16:02

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ make it work -> make it work right -> profile -> fix any necessary problem areas -> repeat \$\endgroup\$ – wes Jan 24 '14 at 15:43
  • \$\begingroup\$ Like Wes says, profile (once you've got it right - optimizing wrong code just makes the wrong thing happen faster). For game development, I've found that visualising performance data as it happens (like this: clayworks3d.blogspot.co.uk/2014/01/…) one the most useful tricks. As for usual gotchas - naive use of graphics data is a common problem. If you fail to batch calls in vertex/index buffers, performance will suffer. \$\endgroup\$ – Luther Jan 24 '14 at 15:51
  • 1
    \$\begingroup\$ The symptom you describe - hiccups every few seconds - suggests it's not your frame-to-frame code that's the issue, but something that happens infrequently/asynchronously. Suspect #1 is the garbage collector, for which you want to look at improving memory use, avoiding unnecessary "new" object creation, deleting things when you're done with them, etc. Other possibilities include an edge case that demands more processing every few dozen frames, like resolving a messy collision, but check suspect #1 first. \$\endgroup\$ – DMGregory Jan 24 '14 at 16:00
  • \$\begingroup\$ @DMGregory Is it a problem that I create a Object object = new Object() every cycle of a loop? I thought that the garbage collector erases that previous object when a new one is created with the same reference. \$\endgroup\$ – user3150201 Jan 25 '14 at 0:59
  • \$\begingroup\$ @wes For example, let's assume that I learned how to use a profiler. How do I know if an area of code is a 'necessary problem area'? \$\endgroup\$ – user3150201 Jan 25 '14 at 1:06
2
\$\begingroup\$

In my experience, there are some things that greatly affect the performance or my games, and some that do not, as much.

Most Significant:

  • Running lengthy code all of the time. This can be avoided, at least partly, by checking if any given code needs to be run that frame:

    if change == True: do code

    else:don't

  • With concern for repetition of code, it is always good to make a method or function for it, depending on the programming language.

  • Also, you want to find the edge cases, like DMGregory said, and improve their efficiency, or at least do something to make the framerate smoother. Smoother frames look much better than jittery ones, even if they have a slightly less overall framerate.

Less Significant

  • The more threads that there are, or background processes, or anything else that takes processor time, the less processor use will be allocated to the game

  • Your engine: sometimes code that is built into engines is bulky, and has things that you don't need, which you can disable. This will usually save processor time, also.

\$\endgroup\$
  • \$\begingroup\$ In my game, I have on the screen two tanks, a lot of missiles, and four walls. Every cycle of the game loop, I pretty much check with every missile of tank1 if it collides with tank2, and check with every missile of tank2 if it collides with tank1. I also check for every missile if it collides with a wall. And I also check constantly if the two tanks collide. My question is: In your experience, do you think that this obviously inefficient way to do things, will significantly harm the performance? Could this be the main reason for the low performance of my program? \$\endgroup\$ – user3150201 Jan 25 '14 at 1:03
  • \$\begingroup\$ @user3150201 it depends. What language are you using? how does your collision detection work? all of these can affect performance. Also, I would would do this: check if the tank's position has changed since last frame. If it hasn't, don't check collision, if it has, do. \$\endgroup\$ – Pip Jan 26 '14 at 1:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.