0

Okay, so i'm currently working on a duplicate of T-Rex game from Chrome, the one that is available if you do not have internet access. I am trying to do it in C language and use ncurses library for this purpose. So far i have a jumping dinosaur in static world, but i encoutered a problem i cannot solve for life. I need the world to loop, possibly endlessly but even a limited world works, but at the same time i need to listen to keyboard input in order to Jump with the dinosaur to avoid obstacles. How can i achieve that? Right now it's jumping based on a getch() function, but that function pauses the code and waits for the user input, so the world won't move until you push some key, and that's obviously not what i need. Is there some way to do this, or is it too advanced? Thanks!

Already tried getch() function, kbhit() function which is not available for me since i'm also working on a Linux OS, using getch() as a condition for IF statement, all without any luck ...

  • Do you have an event loop to hook into? – tadman Dec 05 '22 at 20:21
  • Uhh i do not really know what an event loop is :( – Andrej Rišian Dec 05 '22 at 20:23
  • Look for [non-blocking keyboard input](https://www.google.co.uk/search?q=stack+overflow+non-blocking+keyboard+linux+input+C+-c%2B%2B+-c%23+site:stackoverflow.com). – Weather Vane Dec 05 '22 at 20:27
  • thank you for the tip, i already tried googling this and many more but nothing seems to work or be actually usable for my case.. – Andrej Rišian Dec 05 '22 at 20:29
  • 1
    The latter portion of an answer I wrote [here](https://stackoverflow.com/a/72036607/2505965) briefly describes an *event loop*. The important thing to note is the use of [`timeout`](https://linux.die.net/man/3/timeout) to make [`getch`](https://linux.die.net/man/3/getch) *non-blocking*. – Oka Dec 05 '22 at 20:31
  • Thanks y'all for help, i'm too behind for this i think... should be a school project to use ncurses to make some sort of game and i thought of this as a cool easter-egg game. Seems it's too much for me or what... – Andrej Rišian Dec 05 '22 at 20:43
  • If you are still interested, you should edit your question to include a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) of your game so far. An answer that contains a refactored version of your own code might help you to better understand these concepts, because you may be able to better see the ways in which the code needs to change. – Oka Dec 06 '22 at 13:07

0 Answers0