Add alt code for 2001/kev - arrow key support
Since arrow keys can be more natural than other keys like the program's ',' and '.' for movement I added a version which lets you use arrow keys instead. One player could use the ',' and the other '.' or both can play with the same, whichever they pick. I also slowed down the speed of the ball as it moved very fast considering the slow speed the paddle moves. The README.md shows how to reset it or change the value via make. Updated .gitignore.
This commit is contained in:
1 parent
ad26801380
commit
f757f66b99
4 files changed
+179
-23
No files matched your search
@@ -1 +1,2 @@
|
||||
kev
|
||||
kev.alt
|
||||
+6
-3
@@ -59,7 +59,7 @@ ARCH=
|
||||
|
||||
# Defines that are needed to compile
|
||||
#
|
||||
CDEFINE= -DPORT=5455 -DSPEED=50 -Dl_="socket(il.sin_family=AF_INET,SOCK_STREAM,0)"
|
||||
CDEFINE= -DPORT=5455 -DSPEED=100 -Dl_="socket(il.sin_family=AF_INET,SOCK_STREAM,0)"
|
||||
|
||||
# Include files that are needed to compile
|
||||
#
|
||||
@@ -115,8 +115,8 @@ OBJ= ${PROG}.o
|
||||
DATA=
|
||||
TARGET= ${PROG}
|
||||
#
|
||||
ALT_OBJ=
|
||||
ALT_TARGET=
|
||||
ALT_OBJ= ${PROG}.alt.o
|
||||
ALT_TARGET= ${PROG}.alt
|
||||
|
||||
|
||||
#################
|
||||
@@ -138,6 +138,9 @@ ${PROG}: ${PROG}.c
|
||||
alt: data ${ALT_TARGET}
|
||||
@${TRUE}
|
||||
|
||||
${PROG}.alt: ${PROG}.alt.c
|
||||
${CC} ${CFLAGS} $< -o $@ ${LIBS}
|
||||
|
||||
# data files
|
||||
#
|
||||
data: ${DATA}
|
||||
|
||||
+50
-20
@@ -1,11 +1,8 @@
|
||||
# Best Curses Game
|
||||
|
||||
Kevin Pulo
|
||||
School of Information Technologies
|
||||
Madsen Building F09
|
||||
University of Sydney, NSW, 2006
|
||||
Australia
|
||||
<http://www.kev.pulo.com.au/>
|
||||
Kevin Pulo<br>
|
||||
Australia<br>
|
||||
<http://www.kev.pulo.com.au/><br>
|
||||
Mastodon: <https://fosstodon.org/@devkev>
|
||||
|
||||
## To build:
|
||||
@@ -17,15 +14,49 @@ make
|
||||
## To run:
|
||||
|
||||
```sh
|
||||
./kev hostname
|
||||
./kev # in one terminal
|
||||
|
||||
./kev hostname # in another terminal
|
||||
```
|
||||
|
||||
NOTE: each player's paddle is at the bottom and the opponent at the top.
|
||||
|
||||
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) slowed down the ball
|
||||
just a tad as it went too fast for the speed at which the paddles move even when
|
||||
holding down the movement keys. If you need to change the speed you can
|
||||
reconfigure it like (say for over a network and not the same host):
|
||||
|
||||
```sh
|
||||
make clobber SPEED=50 all # default value which is very fast
|
||||
```
|
||||
|
||||
There is an alternate version which lets you use the arrow keys to control your
|
||||
paddle. See below Alternate code.
|
||||
|
||||
## Try:
|
||||
|
||||
```sh
|
||||
./kev
|
||||
./kev localhost
|
||||
```
|
||||
|
||||
### Alternate code:
|
||||
|
||||
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) provided an alternate
|
||||
version which lets you use the arrow keys on your keyboard instead of the more
|
||||
awkward ',' and '.'. To use:
|
||||
|
||||
```sh
|
||||
make alt
|
||||
```
|
||||
|
||||
Use `kev.alt` as you would `kev`.
|
||||
|
||||
What happens if you use both, one in one terminal and the other in another
|
||||
terminal?
|
||||
|
||||
Thank you Cody!
|
||||
|
||||
## Judges' remarks:
|
||||
|
||||
Continuing with this year's "retro game" theme, this is another old game;
|
||||
@@ -45,7 +76,7 @@ box.
|
||||
|
||||
* TCP sockets
|
||||
* curses
|
||||
* usleep
|
||||
* `usleep()`
|
||||
|
||||
### DESCRIPTION
|
||||
|
||||
@@ -117,18 +148,17 @@ terminal size).
|
||||
|
||||
### BUILDING
|
||||
|
||||
Just run `make`to use the Makefile as above. The `SPEED` variable in the Makefile
|
||||
controls the speed of the game (defaults to `50`). This speed is
|
||||
the delay between frames in milliseconds. Thus a value of 0 will
|
||||
make the game go as fast as your network can handle, and values
|
||||
less than 50 are useful for larger terminals or more exciting
|
||||
games. The program needs bandwidth equal to 1/s bytes per second
|
||||
(where `s` is the speed value). Any latency on the network will
|
||||
be added to `s` - so you should probably subtract any latency from
|
||||
the value of `s` you want to have. Given that `loopback` and fast
|
||||
Ethernet LANs are both much less than 1ms of latency, you should
|
||||
only need to worry about this if you plan on playing over a slower
|
||||
link like a cable modem, DSL, or (God-forbid) dial-up.
|
||||
Just run `make`to use the Makefile as above. The `SPEED` variable in the
|
||||
Makefile controls the speed of the game (defaults to `50`). This speed is the
|
||||
delay between frames in milliseconds. Thus a value of 0 will make the game go
|
||||
as fast as your network can handle, and values less than 50 are useful for
|
||||
larger terminals or more exciting games. The program needs bandwidth equal to
|
||||
1/s bytes per second (where `s` is the speed value). Any latency on the network
|
||||
will be added to `s` - so you should probably subtract any latency from the
|
||||
value of `s` you want to have. Given that `loopback` and fast Ethernet LANs are
|
||||
both much less than 1ms of latency, you should only need to worry about this if
|
||||
you plan on playing over a slower link like a cable modem, DSL, or (God-forbid)
|
||||
dial-up.
|
||||
|
||||
The TCP port to use for network communications is specified by the
|
||||
`PORT` variable in the Makefile. If the default of `5455` is
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <unistd.h>
|
||||
#include <curses.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define o0(M,W) mvprintw(W,M?M-1:M,"%s%s ",M?" ":"",_)
|
||||
#define O0(M,W) M##M=(M+=W##M)-W##M
|
||||
#define l1(M,W) M.tv_##W##sec
|
||||
#define L1(m,M,l,L,o,O) for(L=l;L--;)((char*)(m))[o]=((char*)(M))[O]
|
||||
#define I1 lL,(struct sockaddr*)&il
|
||||
#define i1 COLS
|
||||
#define j LINES
|
||||
#define L_ ((j%2)?j:j-1)
|
||||
|
||||
fd_set I;struct socka\
|
||||
ddr_in il;struct host\
|
||||
ent*LI; struct timeval
|
||||
IL,l;char L[9],_[1<<9]
|
||||
;void ___(int __
|
||||
){_[__--]=+0;if(
|
||||
++__)___(--__);_
|
||||
[__]='=';}double
|
||||
o,oo=+0,Oo=+0.2;
|
||||
long O,OO=0,oO=1
|
||||
,ii,iI,Ii,Ll,lL,
|
||||
II=sizeof(il),Il
|
||||
,ll,LL=0,i=0,li,
|
||||
lI;int main(int\
|
||||
iL,char *Li[]){\
|
||||
initscr();keypad
|
||||
(stdscr,1);cbreak
|
||||
();noecho();nonl
|
||||
();___(lI=i1/4);
|
||||
_[0]='[';_[lI-1]
|
||||
=']';L1(&il,&_,\
|
||||
II,O,+O,+lI);il.
|
||||
sin_port=htons((
|
||||
unsigned long)(\
|
||||
PORT&0xffff));lL
|
||||
=l_;if(iL=!--iL)
|
||||
{il. sin_addr .\
|
||||
s_addr=0;bind(I1
|
||||
,II);listen(lL,5
|
||||
);lL=accept(I1,&
|
||||
II);}else{oO-=2;
|
||||
LI=gethostbyname
|
||||
(Li[1]);L1(&(il.
|
||||
sin_addr),(*LI).
|
||||
h_addr_list[0],\
|
||||
LI->h_length,iI,
|
||||
iI,iI);(*(&il)).
|
||||
sin_family=(&(*\
|
||||
LI))->h_addrtype
|
||||
;connect(I1,II);
|
||||
}ii=Ii=(o=i1*0.5
|
||||
)-lI/2;iI=L_-1;O
|
||||
=li=L_*0.5;while
|
||||
(_){mvaddch(+OO,
|
||||
oo,' ');o0(ii,iI
|
||||
);o0(Ii,Il-=Il);
|
||||
mvprintw(li-1,Il
|
||||
,"%d\n\n%d",i,LL
|
||||
);mvhline(li,+0,
|
||||
'-',i1);mvaddch(
|
||||
O,o,'*');move(li
|
||||
,Il);refresh();\
|
||||
timeout(+SPEED);
|
||||
gettimeofday(&IL
|
||||
,+0);Ll=getch();
|
||||
timeout(0);while
|
||||
(getch()!=ERR);\
|
||||
if(Ll=='q'&&iL)\
|
||||
write(lL,_+1,1);
|
||||
if(ii>(ll=0)&&Ll
|
||||
==KEY_LEFT){write(lL,
|
||||
_,-(--Il));}else
|
||||
if(Ll==KEY_RIGHT&&ii+\
|
||||
lI<i1){write(lL,
|
||||
_+lI,++Il);}else
|
||||
if(iL||!Il)write
|
||||
(lL,_+lI-1,4-3);
|
||||
gettimeofday(&l,
|
||||
0);II=((II=l1(IL
|
||||
,)+(l1(l,u)-=l1(
|
||||
IL,u))-l1(l,)+(\
|
||||
l1(l,)-=l1(IL,))
|
||||
)<0)?1+II-l1(l,)
|
||||
+1e6+(--l1(l,)):
|
||||
II;usleep((II+=\
|
||||
l1(l,)*1e6-SPEED
|
||||
*1e3)<0?-II:+0);
|
||||
if(Ll=='q'&&!iL)
|
||||
break;FD_ZERO(&I
|
||||
);FD_SET(lL,&I);
|
||||
memset(&*&IL,ll,
|
||||
sizeof(l));if((\
|
||||
Ll=select(lL+1,&
|
||||
I,0,0,&IL)));{if
|
||||
(read(lL,&L,ll+1
|
||||
)){if(!*L){ll++;
|
||||
}else if(*L==ll[
|
||||
_]){ll--; }else\
|
||||
if(*(&(*L))==1[_
|
||||
]){break;}}else{
|
||||
break;}}O0(o,O);
|
||||
O0(O,o);if(o<0){
|
||||
o*=-1;Oo*=-1;}if
|
||||
(o>i1){o=i1+i1-o
|
||||
;Oo*=-1;}if(o>=(
|
||||
Ii+=ll)&&O<1&&oO
|
||||
<0&&o<Ii+lI){O=2
|
||||
;oO=~--oO;Oo+=ll
|
||||
*4e-1;}if(O<0){O
|
||||
=iI;LL++;}if(o>=
|
||||
(ii+=Il)&&O>iI-1
|
||||
&&oO>0&&o<ii+lI){O=iI-
|
||||
2;oO=~--oO;Oo+=Il*4e-1
|
||||
;}if(+O>+iI){O-=O;i++;
|
||||
}}endwin();return(0);}
|
||||
|
||||
Reference in new issue
Block a user