#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<pthread.h>
#include<windows.h>
#include<process.h>
int t=5,score=0,blood=5;
//地图绘制
int map[22][17]=
{
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //0
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //1
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //2
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //3
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //4
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //5
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //6
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //7
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //8
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //9
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //10
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //11
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //12
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //13
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //14
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //15
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //16
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //17
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //18
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, //19
{1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,1}, //20
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} //21
};
//地图打印
void print()
{
int i,j;
for(i=0;i<22;i++)
{
for(j=0;j<17;j++)
{
if(map[i][j]==1)printf("▇ ");
else if(map[i][j]==0)printf(" ");
else if(map[i][j]==2)printf("▲");
else if(map[i][j]==3)printf("▽");
else if(map[i][j]==4)printf("||");
}
printf("\n");
}
printf("\n\n");
printf("血量:");
while(t--){printf("▇ "); }
t= blood;
printf("\n你的得分:%d",score);
printf("\n操作:a左 s右 k攻击\n");
printf("游戏名:飞机大战 作者:洋仔_ 2022.1.22");
}
//飞机横坐标
int x=8;
//移动操作
void control(char operation)
{
if(operation=='a'&&map[20][x-1]!=1)
{
map[20][x]=0;
map[20][--x]=2;
}
else if(operation=='s'&&map[20][x+1]!=1)
{
map[20][x]=0;
map[20][++x]=2;
}
else if(operation=='k')
{
map[19][x]=4;
}
}
void enemy_and_attack()
{
int i,j;
for(i=20;i>0&&blood!=0&&score<=20;i--)
for(j=1;j<17&&blood!=0&&score<=20;j++)
{
if(map[i][j]==3)//敌机
{
if(map[i+1][j]==0||map[i+1][j]==1)//向前飞
{
if(map[i+1][j]==1)
{
blood--; //撞到底墙己方掉血
map[i+1][j]==0;
}
map[i][j]=0;
map[i+1][j]=3;
}
else if(map[i+1][j]==4)//被击毁
{
map[i][j]=0;
map[i+1][j]=0;
score++;
}
}
if(map[21-i][j]==4)//子弹
{
if(map[20-i][j]!=1||map[20-i][j]!=3)
{
map[20-i][j]=4;
map[21-i][j]=0;
}
if(map[20-i][j]==3)//击中敌机
{
map[21-i][j]=0;
map[20-i][j]=0;
score++;
}
}
}
srand((unsigned)time(NULL));
map[1][(rand()%15)+1]=3;
}
void refresh()
{
clock_t start;
start = clock();
while(blood!=0&&score<=20)//血量不为0
{
if((clock()-start)==1000)
{
enemy_and_attack();
system("cls");
print();
start = clock();
}
}
}
void function_1()
{
print();
refresh();
}
void function_2()
{
while(blood!=0&&score<=20)//血量不为0
{
char operation;
operation=getch();
control(operation);
system("cls");
print();
}
system("cls");
if(blood<=0)printf("YOU LOSE!\n得分:%d\n单击右上角关闭",score);
else if(score>=20)printf("YOU WIN!\n得分:%d\n单击右上角关闭",score);
}
void function_3()
{
while(1)
{
system("cls");
print();
sleep(100);
}
}
int main()
{
_beginthread(function_1,NULL,NULL);
_beginthread(function_2,NULL,NULL);
//_beginthread(function_3,NULL,NULL);
sleep(3600000);
return 0;
}