大战"/>
弹指间计算机协会做游戏学编程活动2019:飞机大战
整体流程:
1.清屏
2.生成随机数
3.检测有无输入
4.输出飞机
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
int main()
{bool isFire = false; //是否发射了子弹bool isKilled = false; //是否击中了目标//竖直方向是x轴,水平方向是y轴,向右向下为正int x = 5, y = 10; //(x, y)是飞机最左边一个点的坐标int nx = 0, ny = 5; //(x, y)是目标的坐标srand((unsigned)time(NULL));while (true) {system("cls"); //清屏if (isKilled) //如果目标被命中{//随机生成一个新目标nx = (rand() % 6);//[0, 5]ny = (rand() % 9) + 2;//[2, 10]//将新目标标记为未被击中isKilled = false;}if (!isKilled){//输出目标for (int i = 0; i < nx; i++)cout << endl;for (int i = 0; i < ny; i++)cout << " ";cout << "+" << endl;}if (isFire == false) //输出飞机上面的空行{for (int i = 0; i < x - nx; i++)cout << endl;}else //输出飞机上面的激光竖线{for (int i = 0; i < x - nx; i++){for (int j = 0; j < y; j++)cout << " ";cout << " |" << endl;}//飞机头与目标在一条线上if (y + 2 == ny)isKilled = true; //击中目标isFire = false;}//下面输出一个飞机图案for (int i = 0; i < y; i++)cout << " ";cout << " *" << endl;for (int i = 0; i < y; i++)cout << " ";cout << "*****" << endl;for (int i = 0; i < y; i++)cout << " ";cout << " * * " << endl;if (kbhit()) //判断是否有输入{char input = getch(); //根据用户的不同输入来移动,不必输入回车switch (input) {case 'a':case 'A':y--;break;case 'd':case 'D':y++;break;case 'w':case 'W':x--;break;case 's':case 'S':x++;break;case ' ':isFire = true;}}}return 0;
}
更多推荐
弹指间计算机协会做游戏学编程活动2019:飞机大战
发布评论