弹指间计算机协会做游戏学编程活动2019:飞机大战

编程入门 行业动态 更新时间:2024-10-08 04:28:02

弹指间计算机协会做游戏学编程活动2019:飞机<a href=https://www.elefans.com/category/jswz/34/1770057.html style=大战"/>

弹指间计算机协会做游戏学编程活动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:飞机大战

本文发布于:2024-02-19 19:36:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大战   飞机   协会   计算机   弹指间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!