# How should I structure the Main loop for my game?

**URL:** <https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009>\
**Category:** Coding Questions\
**Created:** [October 30, 2018, 2:22am UTC](https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009 "2018-10-30T02:22:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/angle/32/2026_2.png) [@Angle](https://discourse.processing.org/u/Angle)\
**Post date:** [October 30, 2018, 2:22am UTC](https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009/1 "2018-10-30T02:22:20Z")

</div>

Should I just use the draw() method from processing? Or should I use noLoop() and write my own main loop code instead? I want to implement dt (deltaTime, used to keep gameplay smooth) and all that jazz, so it seems like implementing my own loop is a must. Is there a library I should use instead? I looked at Hermes, but it doesn’t seem to be functional right now. Even the experimental new version won’t run. Though I guess I could look at the source and try and get it running. Thoughts?

---

<div class="post-metadata">

**Author:** ![makoho](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/makoho/32/2176_2.png) [@makoho](https://discourse.processing.org/u/makoho)\
**Post date:** [October 30, 2018, 10:02am UTC](https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009/2 "2018-10-30T10:02:49Z")

</div>

Hi Angle,

I assume you need user input for a game, but if you use noLoop(), you won’t be able to receive mouse/keyboard events in your sketch. You can toggle the noLoop() in this simple sketch to illustrate this. Maybe you can use java libraries to work around this, I am not familiar with them. Sorry if that is actually your question 🙂

```auto
void setup()
{
  size(300, 300);
  background(239);
  fill(10, 200);
  noLoop();
}

void keyPressed()
{
  background(239); 
  text(mouseX, 50, 50);
  text(mouseY, 50, 70);
  text(key, 50, 90);
}

void mousePressed()
{
  background(239); 
  text(mouseX, 50, 50);
  text(mouseY, 50, 70);
}

void draw()
{
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 30, 2018, 10:14am UTC](https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009/3 "2018-10-30T10:14:42Z")

</div>

Hi Angle,

You can use the idea of a delta time even with the draw loops. The idea is simply to get the time your loop took to execute and update a global `dt` variable at the end of your draw loop. This way when you enter the draw loop again, your global variable `dt` have the right value to update your game!

---

<div class="post-metadata">

**Author:** ![Angle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/angle/32/2026_2.png) [@Angle](https://discourse.processing.org/u/Angle)\
**Post date:** [October 31, 2018, 12:42am UTC](https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009/4 "2018-10-31T00:42:05Z")

</div>

Hmm. Those are both good points. I think I will try something simple with draw() for the moment, and perhaps switch to something more complicated later. Thank you!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [November 1, 2018, 11:23pm UTC](https://discourse.processing.org/t/how-should-i-structure-the-main-loop-for-my-game/5009/5 "2018-11-01T23:23:16Z")

</div>

@Angle – in general, Processing is all designed around using draw as the main animation loop – the events / controls, things like frameCount and frameRate – they are all built around using draw. It isn’t impossible to not use the loop, but unless you have a very good reason, you are probably just making things hard for yourself.
