Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/games/worm support arrow keys.



details:   https://anonhg.NetBSD.org/src/rev/70b488b8c22e
branches:  trunk
changeset: 477648:70b488b8c22e
user:      cgd <cgd%NetBSD.org@localhost>
date:      Tue Oct 26 06:35:49 1999 +0000

description:
support arrow keys.

diffstat:

 games/worm/worm.6 |   5 +++--
 games/worm/worm.c |  54 ++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 43 insertions(+), 16 deletions(-)

diffs (141 lines):

diff -r 67fb1a28cb68 -r 70b488b8c22e games/worm/worm.6
--- a/games/worm/worm.6 Tue Oct 26 05:57:48 1999 +0000
+++ b/games/worm/worm.6 Tue Oct 26 06:35:49 1999 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: worm.6,v 1.5 1997/10/12 02:12:45 lukem Exp $
+.\"    $NetBSD: worm.6,v 1.6 1999/10/26 06:35:49 cgd Exp $
 .\"
 .\" Copyright (c) 1989, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -46,7 +46,8 @@
 In
 .Nm "" ,
 you are a little worm, your body is the "o"'s on the screen
-and your head is the "@".  You move with the hjkl keys (as in the game
+and your head is the "@".  You move with the hjkl keys and the arrow
+keys (as in the game
 snake).  If you don't press any keys, you continue in the direction you
 last moved.  The upper case HJKL keys move you as if you had pressed
 several (9 for HL and 5 for JK) of the corresponding lower case key
diff -r 67fb1a28cb68 -r 70b488b8c22e games/worm/worm.c
--- a/games/worm/worm.c Tue Oct 26 05:57:48 1999 +0000
+++ b/games/worm/worm.c Tue Oct 26 06:35:49 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: worm.c,v 1.16 1999/09/12 09:02:24 jsm Exp $    */
+/*     $NetBSD: worm.c,v 1.17 1999/10/26 06:35:49 cgd Exp $    */
 
 /*
  * Copyright (c) 1980, 1993
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)worm.c     8.1 (Berkeley) 5/31/93";
 #else
-__RCSID("$NetBSD: worm.c,v 1.16 1999/09/12 09:02:24 jsm Exp $");
+__RCSID("$NetBSD: worm.c,v 1.17 1999/10/26 06:35:49 cgd Exp $");
 #endif
 #endif /* not lint */
 
@@ -80,7 +80,7 @@
 int slow = 0;
 int score = 0;
 int start_len = LENGTH;
-char lastch;
+int lastch;
 char outbuf[BUFSIZ];
 
 void   crash __P((void)) __attribute__((__noreturn__));
@@ -89,7 +89,7 @@
 void   leave __P((int)) __attribute__((__noreturn__));
 void   life __P((void));
 void   newpos __P((struct body *));
-void   process __P((char));
+void   process __P((int));
 void   prize __P((void));
 int    rnd __P((int));
 void   setup __P((void));
@@ -100,7 +100,6 @@
        int argc;
        char **argv;
 {
-       char ch;
 
        /* Revoke setgid privileges */
        setregid(getgid(), getgid());
@@ -117,6 +116,9 @@
        initscr();
        crmode();
        noecho();
+#ifdef KEY_LEFT
+       keypad(stdscr, TRUE);
+#endif
        slow = (baudrate() <= 1200);
        clear();
        stw = newwin(1, COLS-1, 0, 0);
@@ -141,8 +143,7 @@
                else
                {
                    fflush(stdout);
-                   if (read(0, &ch, 1) >= 0)
-                       process(ch);
+                   process(getch());
                }
        }
 }
@@ -237,7 +238,7 @@
 
 void
 process(ch)
-       char ch;
+       int ch;
 {
        int x,y;
        struct body *nh;
@@ -247,17 +248,42 @@
        y = head->y;
        switch(ch)
        {
-               case 'h': x--; break;
-               case 'j': y++; break;
-               case 'k': y--; break;
-               case 'l': x++; break;
+#ifdef KEY_LEFT
+               case KEY_LEFT:
+#endif
+               case 'h':
+                       x--; break;
+
+#ifdef KEY_DOWN
+               case KEY_DOWN:
+#endif
+               case 'j':
+                       y++; break;
+
+#ifdef KEY_UP
+               case KEY_UP:
+#endif
+               case 'k':
+                       y--; break;
+
+#ifdef KEY_RIGHT
+               case KEY_RIGHT:
+#endif
+               case 'l':
+                       x++; break;
+
                case 'H': x--; running = RUNLEN; ch = tolower(ch); break;
                case 'J': y++; running = RUNLEN/2; ch = tolower(ch); break;
                case 'K': y--; running = RUNLEN/2; ch = tolower(ch); break;
                case 'L': x++; running = RUNLEN; ch = tolower(ch); break;
                case '\f': setup(); return;
-               case CNTRL('C'): crash(); return;
-               case CNTRL('D'): crash(); return;
+
+               case ERR:
+               case CNTRL('C'):
+               case CNTRL('D'):
+                       crash();
+                       return;
+
                default: if (! running) alarm(1);
                           return;
        }



Home | Main Index | Thread Index | Old Index