2
\$\begingroup\$

I'm looking at developing a puzzle game, it might be like tetris or something similar, perhaps more like bejewelled in that the player will select objects with the mouse to manipulate them.

I've been studying java part-time in college for the past year, really beginner stuff, but I've decided to build a game over the summer to keep the brain active :-D

So far I've animated some primitive shapes moving around the screen using the timer from the utilities package. This is all good, they're behaving the way I want. So now I've decided to try manipulate the shapes with the mouse. Just some really simple stuff, maybe change the colour of an object when I click on it.

To test the mouse functions I've successfully completed the MouseEvent and MouseListener tutorials on The New Boston. However, I can't get these events working in my code. I suspect I'm going to have to re-write my classes.

Will I need separate threads within the game, for example, one thread to animate the objects and another to listen for and handle mouse events.

My understanding is that I should implement runnable on my classes when using threads in Java games. Am I thinking on the right lines?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ For something like Tetris? Nope, unless you've developed some kind of Tetris that is more complicated than Dwarf Fortress. And even then I'd doubt it. \$\endgroup\$ Commented Jun 12, 2011 at 20:36
  • \$\begingroup\$ Using separate threads for these tasks isn't a bad idea, and either way is certainly possible. If you do start using threads, the educational value will certainly make for a good lesson and may help prepare you for writing more complicated games/applications in the future that do need threads. \$\endgroup\$ Commented Jun 12, 2011 at 20:55
  • \$\begingroup\$ Dwarf Fortress looks scairy! :-D I may use threads in this game for the educational value. Threads are something I definitely need to get my head around, and better now when there's no pressure! Thanks guys. \$\endgroup\$
    – mal
    Commented Jun 13, 2011 at 9:33

1 Answer 1

2
\$\begingroup\$

For simple games you won't need separate threads. Threads are usually used in games to calculate things that take longer than 1 frame.

For example: in your puzzle game it takes 5ms to update your logic, and 10ms to draw everything to screen. This gives you a frame time of 15ms, which gives you about 60 frames per second. This is perfect :).

Now for some reason you sometimes need to do an expensive path-finding algorithm that takes between 10 and 50ms to complete. To not have your frame rate drop to 20 frames per second you calculate the path finding in a separate thread so that your game just runs at 60 frames per second, but the path finding data is only available a few frames after it was requested. (You just have your avatar stand still until the data is available for example, 20ms wont be noticed).

In some applications input handling is done in separate threads, but again this is only relevant if there are other tasks that could cause your game not reading input fast enough. As long as your total frame time is lower than 16 ms or something like that then you won't need threads.

\$\endgroup\$
2
  • \$\begingroup\$ Thankyou for your answer, that clears things up a lot for me. \$\endgroup\$
    – mal
    Commented Jun 13, 2011 at 9:31
  • \$\begingroup\$ Multi-threading is also necessary for networking. \$\endgroup\$ Commented Jun 13, 2011 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .