简易飞机空战小游戏
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>#define width 30 //屏幕的寬
#define high 40 //屏幕的高
#define EnemyAirportNum 5 //敵機出現的數量
#define MyFly 1 //我方飛機
#define EnemyFly 2 //敵方飛機
#define Bullet 3 //子彈int FlyShow[high][width] = {0}; //游戲界面
int EnemyAirport_x[EnemyAirportNum], EnemyAirport_y[EnemyAirportNum]; //敵機的坐標
int MyAirport_x, MyAirport_y; //我方戰機坐標
int speed; //敵方戰機速度
int score; //分數
int BulletWidth; //子彈寬度void gotoxy(int x, int y)//清屏
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);COORD pos;pos.X = x;pos.Y = y;SetConsoleCursorPosition(handle, pos);
}
void HideCursor()//隱藏光標
{CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}void Init()
{MyAirport_x = width / 2;MyAirport_y = high-5;FlyShow[MyAirport_y][MyAirport_x] = MyFly;for (int k = 0; k < EnemyAirportNum; k++)//依次初始化EnemyAirportNum個敵機位置{EnemyAirport_x[k] = rand() % (width-1);EnemyAirport_y[k] = rand() % high / 2;FlyShow[EnemyAirport_y[k]][EnemyAirport_x[k]] = EnemyFly;}speed = 10;score = 0;BulletWidth = 1;HideCursor();
}void show()
{gotoxy(0, 0);static int i = 0;if (i < 100)i++;if (i == 100)//主循環100次刷一次屏{system("cls");i = 0;}//打印戰機及子彈for (int y = 0; y < high; y++){for (int x = 0; x < width; x++){for (int k = 0; k < EnemyAirportNum; k++){if ((y == EnemyAirport_y[k]) && (x == EnemyAirport_x[k]))//打印敵機printf("@");}if (FlyShow[y][x] == MyFly)//打印我方戰機printf("*");else if (FlyShow[y][x] == Bullet)//打印子彈printf("!");elseprintf(" ");}printf("\n");}printf("得分:%d\n", score);
}void Player()
{char input;if (kbhit()){input = getch();switch (input){case 72: {FlyShow[MyAirport_y][MyAirport_x] = 0;//將當前位置置為0,即在移動戰機時清除軌跡if (MyAirport_y>0)MyAirport_y--;FlyShow[MyAirport_y][MyAirport_x] = MyFly;//將戰機原位置的上一個位置顯示出戰機}break; //上移戰機case 80: {FlyShow[MyAirport_y][MyAirport_x] = 0;if (MyAirport_y<high-1)MyAirport_y++;FlyShow[MyAirport_y][MyAirport_x] = MyFly;}break; //下移戰機case 75: {FlyShow[MyAirport_y][MyAirport_x] = 0;if (MyAirport_x>0)MyAirport_x--;FlyShow[MyAirport_y][MyAirport_x] = MyFly;} break; //左移戰機case 77: {FlyShow[MyAirport_y][MyAirport_x] = 0;if (MyAirport_x<width-2)MyAirport_x++;FlyShow[MyAirport_y][MyAirport_x] = MyFly;} break; //右移戰機case 32: {//顯示一定寬度的子彈int left, right;left = MyAirport_x - BulletWidth;if (left < 0)left = 0;right = MyAirport_x + BulletWidth;if (right > width - 1)right = width - 1;for (int x = left; x < right;x++)FlyShow[MyAirport_y-1][x] = Bullet;} break;}}
}int UpdateComputer()
{static int i = 0;if (i < speed)//減小敵機的移速i++;for (int y = 0; y < high; y++){for (int x = 0; x < width; x++){if (FlyShow[y][x] == Bullet)//當前位置是子彈{FlyShow[y][x] = 0;if (y>0)FlyShow[y - 1][x] = Bullet;//顯示子彈上移軌跡for (int k = 0; k < EnemyAirportNum; k++)//判斷是否擊中敵機{if ((EnemyAirport_x[k] == x) && (EnemyAirport_y[k]) == y)//當前位置以敵機位置相同時{score++;//分數+1//再次出現一架敵機EnemyAirport_x[k] = rand() % (width-1);EnemyAirport_y[k] = rand() % high / 2;FlyShow[EnemyAirport_y[k]][EnemyAirport_x[k]] = EnemyFly;}}}}}if (i == speed)//計次滿時移動敵機{for (int k = 0; k < EnemyAirportNum; k++){EnemyAirport_y[k]++;//敵機移動if (EnemyAirport_y[k] > high)//敵機出界時{score--;//分數-1//再次出現一架敵機EnemyAirport_x[k] = rand() % (width - 1);EnemyAirport_y[k] = rand() % high / 2;FlyShow[EnemyAirport_y[k]][EnemyAirport_x[k]] = EnemyFly;}if ((EnemyAirport_x[k] == MyAirport_x) && (EnemyAirport_y[k]) == MyAirport_y)//我方戰機與敵機相撞時游戲結束 {printf("游戲結束\n");return 0;}}i = 0;}if (score > 30 && (score % 20 == 0)&&speed>0){speed -= 1;i = 0;}if (score > 30 && (score % 15 == 0) && BulletWidth <width){BulletWidth++;}return 1;
}int main()
{Init();int flag = 1;while (flag){show();Player();flag=UpdateComputer();}system("pause");return 0;
}
總結
- 上一篇: Android--快速接入微信支付
- 下一篇: VisualSVNServer的使用