0
\$\begingroup\$

Say I build a snooker game, where balls need to jitter around.

Balls belong to a class (ball class), where all the ball functions are defined.

In the animation, time evolves by a clock ticking within a time loop.

I wonder if the clock function/time loop should be optimally defined in the main program or the class file?

If defined in the class, the advantage is that all functions regarding ball movements, collisions etc, which take time to happen, can be put in the class, without worrying about the time loop; the disadvantage is that time will tick for every ball separately, which obviously looks bad.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Under the single responsibility principle, the Ball class should be responsible for ball behaviour, not timekeeping.

You'll have other code in your game that cares about time. (eg. UI animations) Under the DRY principle, you should not repeat the basic time-tracking functionality in multiple places.

A typical approach is to manage your time step in your main game loop and either pass the time delta to your game entities' update methods via a dT argument, or publish it via a globally-accessible access point like Unity's static Time class

\$\endgroup\$
2
  • \$\begingroup\$ @ DMGregory Thanks for the answer. I added a sound when balls collide, importing a sound file. But playing a sound turns out to slow down the program quite a lot, as I observe substantial pauses, which look bad. I use matlab. Is it just because matlab is slow? \$\endgroup\$
    – feynman
    Commented Dec 3, 2018 at 7:46
  • \$\begingroup\$ That sounds like a new question you should ask a MatLab expert. We don't do a lot of game development in MatLab, so you might want to ask somewhere like here instead. \$\endgroup\$
    – DMGregory
    Commented Dec 3, 2018 at 12:43

You must log in to answer this question.

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