OK, let me explain what I'm trying to do. Being bored, I started writing a old computer game called sokoban today. The simple example looks like this and it runs on Xterm.

XXXXXXXXXXXXXXXXXX XX XXXXXXXXXXXXX XX XXXXXXXXXXX XX @ XXXXXXXXXXXX XX@XXXXXXXXXXX sX X XXXXXXXXXXX sX X * X XXXXXXXXXXXXXX XX XXXXXXXXXXXXXXXXXX

* is a "player" which you manipulete its move by h/k/l/j key and the "player" pushes '@'s through the open space(where no 'X's are)to move them to the locatins marked as 's'. I want to measure the time and display it while the game is going on.

The below is the initial code. $c could be h/k/l/j to move the player('*') to left/up/right/down(vi's key-bind) and whenever these keys are typed, the player gets moved accordingly. Initially, I did it with non-blocking read like this.

while(1){ if($c = Readkey(-1)){ # running/playing game etc. } sleep(1); print $count++; }

however, the time lag caused by sleep command was annoying especially when typing in continuously. The next I did was making sleep time very short, but after a while, CPU fan of my laptop started running and got noisy. At this point, I thought using sleep and ReadKey(-1) in the same loop didn't work well.

So I was wondering how I could put the sleep command and ReadKey to the separate while loops, then I came up with the idea to separate the process for game handling and time measurement. By separating the tasks, time measurement and game handling work independently, and I don't have to use nonblocking read(because halting the looping by blocking read doesn't hurt anything anymore) so that the loop gets active only when keys are pressed which helps my laptop keep cooler.

At this moment, I modified the program based on the 2nd option suggested by spazm and it's working, but if there are any better ways (or right ways), I'd like to know. As you can see, this is not something to handle the system tasks and the measured time is not necessarily very accurate.


In reply to Re^2: Parent process finished. How to exit the child process. by ybnormal
in thread Parent process finished. How to exit the child process. by ybnormal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.