So I am trying to make a virtual pet type thing in C.
The ascii animations work, they change according to the thing’s mood, they move around, its cute.
The actual changing of the mood is confusing me though.

At the moment it’s mood is contained in two variables. An integer called MOOD which determines which version of the thing shows up in the animation and an array of integers called STATUS[4] where the values represent sickness, hunger, tiredness and boredom.
The working out of MOOD based on STATUS works adequately.

What doesn’t work is the changing of STATUS over time… it does not work because of time. I want it to only change after a certain period of time, but I cannot figure out how to get the changing to wait for the time, without EVERYTHING waiting, including the animation. I can stick the changing in between animation cycles, but that is frustrating because it means I can’t have the whole status changing thing as a single function neatly in one place where I know where it is and it doesn’t pour out all over the screen confusing me.

Is there a way to make it depend on the time without making anything stop without mixing it all up in the animations or spliting it into a bunch of untidy bits?

Another thing that I am not worrying about just yet, but that has similarities is, I want all the time to have the program be receptive to ceirtain keypresses. Like pressing F to feed the pet. I have done nothing on that side of things yet, but I do wonder how to make that happen because again, everything I know how to do involves everything stopping to wait if someone is supposed to input something. I don’t know a way to be receptive but not demanding of input.

I thought if it was possible to have two functions running at the same time instead of one after the other then that would make both of these problems go away, but is there another way?

LOL. My attempt to solve the problem has sent my program into a loop that doesn’t seem to stop when I hit ctrl-C. :stuck_out_tongue: ::slight_smile:

Now I had to kill it :frowning: :stuck_out_tongue:

Ok, you could use timers, but it sounds like the easy approach in this instance is to use ‘threads’. Essentially you can run a function in a separate thread, and each thread will run in parallel, hence you could simply sleep each thread until the next phase is due. It’s actually easier in an OOP, but not that difficult in ‘C’;


int daytime = 1; int feedtime = 0;

function time_of_day() { while 1 { sleep(12 hrs) ; daytime = !daytime; } } // alternate each 12 hours
function feed_me() { while 1 { sleep(4 hrs); feedtime = 1; } } // feed me every 4h, assume ‘feed’ resets ‘feedtime’


… stuff goes here …

For keyboard input, what you ‘really’ want is an event loop, as used by X, Windows and GUI’s in general, essentially;

[ run loads of threads to do stuff ] [ have threads generate events and stick the events in a queue ]

while 1
read next event;
switch( event_type )
case: … service_event()

Then just have one thread that reads characters from the keyboard, and on each keypress, generate an appropriate event … eg; “KEYPRESS+A”, and make sure your event look knows what to do with events of this type. i.e. build up the key press in buffer and echo it on the screen.


(1) try “ctrl-Z”, if that works, “kill %1”
(2) ps ax, identify process id, “kill ” …


Yeah, I was reading about that thread stuff. It looked scary and complicated. But I think it would be fun to try out. I have been trying out so many new things already what is one more lol.

Hehehehe, this is fun. It will be more fun though if I succeed! So I should stop dilly-dallying and get on with it… hahaha.

My program is now using over 100% of cpu!!!

Mmm, I take it you have a multi-core CPU … :wink:


I put lots of sleeps in there now to slow it down a mite.

But wow, how cool is that, both cores!!! I never saw that before :smiley:

So today we learned that a single process can only span one CPU core … but a multi-threaded program will spread threads across multiple cores … :wink:

If you really want a but of fun, you can “pin” specific processes to specific CPU cores, so (for example) on a desktop machine with 2 cores you could pin the application to one core, leaving the other core 100% available for your X server / desktop apps etc … (See: taskset)

The other thing to look at is “nice”, try “nice -n19 ” and you should find that although it appears to eat all your CPU, nothing else really slows down that much … :o