資源簡介
學習了Linux的終端控制之后編寫的一個小游戲,使用WASD這幾個鍵來控制坦克在終端下移動,按j鍵開炮,目前一次只能有一個炮彈飛出,按e鍵退出。非Linux用戶請勿下載,該程序只能運行在Linux終端下(也許Unix也可以)。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?HEIGHT?30//地圖高度
#define?WIDE?100//地圖寬度
enum?direction?{UP?LEFT?RIGHT?DOWN};//坦克的朝向
struct?spot?{
int?i?j;//子彈坐標
int?fly;//子彈是否在飛行
enum?direction?dir;//飛行方向
};
void?tank(int?i?int?j?enum?direction?dir);//繪制坦克
void?era(int?i?int?j?enum?direction?dir);//擦除原圖像
void?fire(struct?spot?*bullet?int?i?int?j?enum?direction?dir);//開炮
int?moveable(int?i?int?j?int?height?int?wide);//測試是否可以移動
int?fireable(struct?spot?*bullet);//測試是否能繼續開炮
void?fly(struct?spot?*bullet);//繪制子彈
char?*cursor?*clean;
int?main(void)
{
char?choice?=?‘a‘;
int?row?col;
enum?direction?dir?=?LEFT;
struct?termios?initial_term?new_term;
struct?spot?bullet?=?{0?0?0?LEFT};
int?hit?=?0??con?=?1;
//配置輸入模式
tcgetattr(fileno(stdin)?&initial_term);
new_term?=?initial_term;
new_term.c_lflag?&=?~ICANON;//非標準輸入模式
new_term.c_lflag?&=?~ECHO;//取消回顯
new_term.c_cc[VMIN]?=?0;//沒有讀入字符也可以返回
new_term.c_cc[VTIME]?=?1;//等待時間0.1秒
tcsetattr(fileno(stdin)?TCSANOW?&new_term);
//匹配終端信息,獲取功能字符串
setupterm(NULL?fileno(stdout)?(int?*)0);
cursor?=?tigetstr(“cup“);
clean?=?tigetstr(“clear“);
col?=?row?=?2;
putp(tparm(cursor?0?0));
putp(clean);
tank(row?col?dir);
while(con)?{
if(scanf(“%c“?&choice)?==?1)?{//此處由于輸入模式的設置,不會堵塞
switch(choice)?{
case?‘w‘: if(moveable(row-1?col?HEIGHT?WIDE))?{
row--;
dir?=?UP;
}?
else?putchar(‘\a‘);;
break;
case?‘s‘: if(moveable(row+1?col?HEIGHT?WIDE))?{
row++;
dir?=?DOWN;
}
else?putchar(‘\a‘);
break;
case?‘a‘: if(moveable(row?col-1?HEIGHT?WIDE))?{
col--;
dir?=?LEFT;
}
else?putchar(‘\a‘);
break;
case?‘d‘: if(moveable(row?col+1?HEIGHT?WIDE))?{
col++;
dir?=?RIGHT;
}?
else?putchar(‘\a‘);
break;
case?‘j‘: if(fireable(&bullet))?fire(&bullet?row?col?dir);
break;
case?‘e‘: con?=?0;
break;
}
era(row?col?dir);
tank(row?col?dir);
}
fly(&bullet);
}
printf(“Bye?Bye!\n“);
sleep(2);
putp(tparm(cursor?
- 上一篇:C++獲取所有局域網可用IPping
- 下一篇:純C語言寫的hex轉bin小工具
評論
共有 條評論