#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 50
#define RANGE 20 //飞机射程 值越小射程越远
int X = 40; //飞机x坐标
int Y = 24; //飞机y坐标
int COUNT = 0; //得分
void gotoxy(int x, int y);
void printmain(void); //打印主菜单
void map_set_up(char (*map)[MAX]); //生成地图
void plane_set_up(char (*map)[MAX]); //生成飞机
void enemy_set_up(char (*enemymap)[MAX]); //生成敌机
void print_all(char (*map)[MAX], char (*enemymap)[MAX]); //打印所有物品
int get_move(char (*map)[MAX], char (*enemymap)[MAX]); //获取指令
void attack(char (*map)[MAX], char (*enemymap)[MAX]); //进行攻击
int check_attack(int i, char (*map)[MAX], char (*enemymap)[MAX]); //检测攻击是否命中
int do_move(char move, char (*map)[MAX], char (*enemymap)[MAX]); //进行移动
int check_plane(char(*map)[MAX], char (*enemymap)[MAX]); //检测飞机是否越界或碰到敌机
void reset_plane(char (*map)[MAX]); //刷新飞机
void gotoxy(int x, int y)
{
printf("%c[%d;%df", 0x1B, x, y);
}
void reset_plane(char (*map)[MAX]) //刷新飞机
{
map[X][Y] = ' ';
}
int check_plane(char(*map)[MAX], char (*enemymap)[MAX]) //检测飞机是否越界或碰到敌机
{
if(enemymap[X][Y] == '*')
{
return 0;
}
if(X == 0 || X == MAX - 1 || Y == 0 || Y == MAX - 1)
{
return 0;
}
return 1;
}
int do_move(char move, char (*map)[MAX], char (*enemymap)[MAX]) //进行移动
{
reset_plane(map);
switch(move)
{
case 'w':
X--;
break;
case 'a':
Y--;
break;
case 's':
X++;
break;
case 'd':
Y++;
break;
}
plane_set_up(map);
return check_plane(map, enemymap);
}
int check_attack(int i, char (*map)[MAX], char (*enemymap)[MAX]) //检测攻击是否命中
{
if(enemymap[X - i][Y] == '*')
{
COUNT++;
enemymap[X - i][Y] = ' ';
}
print_all(map, enemymap);
}
void attack(char (*map)[MAX], char (*enemymap)[MAX]) //进行攻击
{
int i;
time_t begin, end;
begin = clock();
for(i = 1; i < RANGE && X - i > 0; i++)
{
check_attack(i, map, enemymap);
map[X - i][Y] = '|';
}
end = clock();
print_all(map, enemymap);
do
{
end = clock();
}
while((end - begin) <= 80000);
for(i = 1; i < RANGE && X - i > 0; i++)
{
map[X - i][Y] = ' ';
}
print_all(map, enemymap);
}
int get_move(char (*map)[MAX], char (*enemymap)[MAX]) //获取指令
{
char move;
system("stty raw -echo");
do
{
scanf("%c", &move);
}
while(move != 'w' && move != 'a' && move != 's' && move != 'd' && move != ' ' && move != 'q');
system("stty cooked echo");
if(move == 'q')
{
return 0;
}
if(move == ' ')
{
attack(map, enemymap);
}
else return do_move(move, map, enemymap);
}
void print_all(char (*map)[MAX], char (*enemymap)[MAX]) //打印所有物品
{
int i, j;
gotoxy(0, 0);
printf("得分:%d\n", COUNT);
for(i = 0; i < MAX; i++)
{
for(j = 0; j < MAX; j++)
{
if(enemymap[i][j] != ' ')
{
printf("%c ", enemymap[i][j]);
}
else printf("%c ", map[i][j]);
}
putchar('\n');
}
}
void enemy_set_up(char (*enemymap)[MAX]) //生成敌机
{
int count, i, j;
srand(time(NULL) + 1.345);
for(count = 0; count < 5; count++)
{
do
{
i = rand()%47 + 1;
j = rand()%47 + 1;
}
while(i != X && j != Y);
enemymap[i][j] = '*';
}
}
void plane_set_up(char (*map)[MAX]) //生成飞机
{
map[X][Y] = '^';
}
void map_set_up(char (*map)[MAX]) //生成地图
{
int i, j;
memset(map, ' ', MAX * MAX);
for(i = 0; i < MAX; i++)
{
for(j = 0; j < MAX; j++)
{
if(i == 0 || j == 0 || i == MAX - 1 || j == MAX - 1)
{
map[i][j] = '0';
}
}
}
}
void printmain(void) //打印主菜单
{
printf("1.开始游戏\n");
printf("2.退出游戏\n");
printf(":");
}
int main(void)
{
char move;
char map[MAX][MAX];
char enemymap[MAX][MAX];
time_t begin, end;
memset(enemymap, ' ', MAX * MAX);
do
{
printmain();
scanf("%c", &move);
getchar();
}
while(move != '1' && move != '2');
if(move == '1')
{
map_set_up(map);
plane_set_up(map);
system("clear");
begin = time(NULL);
do
{
end = time(NULL);
if(((end - begin) % 3) == 0)
{
enemy_set_up(enemymap);
}
print_all(map, enemymap);
gotoxy(52, 0);
printf("请输入指令(w/a/s/d/j/q):");
}
while(get_move(map, enemymap));
}
system("stty cooked echo");
return 0;
}