- 论坛徽章:
- 0
|
谁有能实现“打飞机”的程序
经过几天的修炼,终于写出来了,只能自己回复自己了
#include <curses.h>;
#include <signal.h>;
#include <stdio.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/time.h>;
#include <sys/select.h>;
#define StartX 1
#define StartY 1
int px,py; //plane x,y
void initial()
{
initscr();
cbreak();
nonl();
noecho();
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
refresh();
}
void draw_plane(int x,int y) //draw plane
{
int ix,iy;
ix=x;
iy=y;
move(ix,iy);
mvaddstr(iy,ix," o" ;
iy++;
mvaddstr(iy,ix,"ooooooo" ;
iy++;
mvaddstr(iy,ix," o" ;
}
void cls_plane(int x,int y)
{
int ix,iy;
ix=x;
iy=y;
move(ix,iy);
mvaddstr(iy,ix," " ;
iy++;
mvaddstr(iy,ix-1," " ;
iy;
mvaddstr(iy,ix-1," " ;
mvaddstr(3,60," " ;
}
void draw_gun(int x)
{
int ix,iy;
iy=24;
ix=x;
move(ix,iy);
mvaddstr(iy,ix,"(^)" ;
}
void cls_gun(int x)
{
int ix,iy;
iy=24;
ix=x;
move(ix,iy);
mvaddstr(iy,ix," " ;
}
int draw_pd(int x)
{
int ix,iy;
ix=x+1;
iy=0;
int iflag=0;
for(;iy<24;iy+=2)
{
if (ix>;px && ix<(px+6) && iy>;py && iy<(py+3))
{
mvaddstr(12,35,"You win" ;
//refresh();
iflag=1;
}
else
mvaddstr(iy,ix,"|");
}
if(iflag==1)
return 1;
else
return 0;
}
void cls_pd(int x)
{
int ix,iy;
ix=x+1;
iy=23;
for(;iy>;=0;iy--)
mvaddstr(iy,ix," ");
}
int main(void)
{
int x=StartX; //gun x,y
int y=StartY;
int ch;
px=StartX; //plane x,y
py=StartY;
int ret;
fd_set fds;
struct timeval tv;
initial(); //curses init
draw_gun(1);
do{
FD_ZERO(&fds);
FD_SET(0,&fds);
tv.tv_sec=0;
tv.tv_usec=50000;
ret=select(1,&fds,NULL,NULL,&tv);
if (ret==0)
{
cls_plane(px,py);
if(px<75)
px++;
else
{
px=1;
py=1;
}
draw_plane(px,py);
mvaddstr(12,35," ");
move(1,1);
refresh();
//sleep(1);
}
if(FD_ISSET(0,&fds))
{
ch=getch();
switch(ch){
case KEY_UP:
draw_pd(x);
refresh();
sleep(1);
cls_pd(x);
break; //fire
case KEY_DOWN:
break;
case KEY_RIGHT:
cls_gun(x); //gun move right
if (x<75)
++x;
else
x=75;
draw_gun(x);
break;
case KEY_LEFT:
cls_gun(x); //gun move left
if(x>;1)
--x;
else
x=1;
draw_gun(x);
break;
case 27:
clear();
refresh();
endwin(); //ESC is quit
exit(1);
default:
break;
}
move(1,1);
}
}while(1);
} |
|