When playing worm(6) and you crash there's a sleep of 2 seconds that
still allows input to be put into the terminal buffer that doesn't get  
read until the application stops.
A similar case is possible in a more limited timespan if you win the
game.

The diff below flushes the input buffer just before exiting curses and
the application, leaving me with no clutter in my $SHELL.
I choose to also add '\n' as an escape character, since it's not part
of the game and allows me to exit after a crash faster (which always
annoys me) and allows me to add a new command straight away.

The crash() case might be a bit elaborate, but it's the closest to the
behaviour we have right now, minus the garbage.

Thoughts? OK?

martijn

Index: worm.c
===================================================================
RCS file: /cvs/src/games/worm/worm.c,v
retrieving revision 1.39
diff -u -p -r1.39 worm.c
--- worm.c      24 Aug 2018 11:14:49 -0000      1.39
+++ worm.c      1 Apr 2020 08:27:03 -0000
@@ -40,6 +40,7 @@
 #include <poll.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <time.h>
 #include <unistd.h>
 
 #define HEAD '@'
@@ -239,7 +240,12 @@ rnd(int range)
 void
 newpos(struct body *bp)
 {
+       int ch;
+
        if (visible_len == (LINES-3) * (COLS-3) - 1) {
+               timeout(0);
+               while ((ch = getch()) != '\n' && ch != ERR)
+                       continue;
                endwin();
                printf("\nYou won!\nYour final score was %d\n\n", score);
                exit(0);
@@ -387,7 +393,17 @@ newlink(void)
 void
 crash(void)
 {
-       sleep(2);
+       int ch, delay;
+       struct timespec tstart, tnow, texp;
+
+       timeout(2000);
+       (void) clock_gettime(CLOCK_MONOTONIC, &tstart);
+       while ((ch = getch()) != ERR && ch != '\n') {
+               (void) clock_gettime(CLOCK_MONOTONIC, &tnow);
+               timespecsub(&tnow, &tstart, &texp);
+               delay = 2000 - (texp.tv_sec * 1000) - (texp.tv_nsec / 1000000);
+               timeout(delay >= 0 ? delay : 0);
+       }
        clear();
        endwin();
        printf("Well, you ran into something and the game is over.\n");

Reply via email to