Problem A: 小型飞机大战

网友投稿 263 2022-11-27

Problem A: 小型飞机大战

Problem A: 小型飞机大战

Description

飞机大战这个小游戏很好玩,作为一名编程狂人,自己不去写个好玩的游戏,怎么能说得过去呢?

现在,请使用C++来编写一个小的飞机大战游戏雏形,至少包括如下类:

1. FlyThing类:敌机和战机的父类,是一个抽象类。

(1)拥有名字属性和位置属性,其中名字是一个字符串,位置是二维空间下的坐标,代表了在屏幕上的位置。

(2)纯虚函数void fly(),模拟飞机的飞行动作。

(3)void show():显示飞机的名字和位置。格式为:“$ at x y”,其中$为飞机名字,x和y是横坐标和纵坐标。

(4)其他必要的函数。

2. EnemyPlane类:敌机类,是FlyThing的子类。其Fly()函数实现将飞机的纵坐标加1。

3. MyPlane类:战机类,是FlyThing的子类。其Fly()函数根据输入实现飞行。具体是:

(1)如果输入A或a,则横坐标减1;

(2)如果输入S或s,则纵坐标加1;

(3)如果输入W或w,则纵坐标减1;

(4)如果输入D或d,则横坐标加1。

Input

第1行N是一个正整数,之后N行,每行包括1个字符串、2个整数分别是敌机名字和横、纵坐标。

第N+2行是一个正整数M,之后M行每行是一个字符,为a、s、d、w之一,用于战机的飞行。

Output

见样例~

#include #include using namespace std;class FlyThing {protected: string name; double x, y;public: virtual void fly() = 0; void show() { cout << name << " at " << x << " " << y << endl; } FlyThing(string s, double x, double y) : name(s), x(x), y(y) {} ~FlyThing() {}};class EnemyPlane : public FlyThing {public: EnemyPlane(string name, double x, double y) : FlyThing(name, x, y){} void fly() { y++; }};class MyPlane : public FlyThing {public: MyPlane(string name, double x, double y) : FlyThing(name, x, y){} void fly() { char c; cin >> c; if (c == 'a' || c == 'A') x--; else if (c == 's' || c == 'S') y++; else if (c == 'w' || c == 'W') y--; else if (c == 'd' || c == 'D') x++; }};int main(){ FlyThing **planes; int numOfEnemplanes, i, x, y, numOfMoves, j; string str; cin>>numOfEnemplanes; planes = new FlyThing*[numOfEnemplanes + 1]; planes[0] = new MyPlane("WUDI", 50, 50); for(i = 1; i <= numOfEnemplanes; i++) { cin>>str>>x>>y; planes[i] = new EnemyPlane(str, x, y); } for (j = 0; j < numOfEnemplanes + 1; j++) { planes[j]->show(); } cin>>numOfMoves; for (i = 0; i < numOfMoves; i++) { for (j = 0; j < numOfEnemplanes + 1; j++) { planes[j]->fly(); planes[j]->show(); } } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微雪电子电容触摸 按键模块简介
下一篇:最先进和广泛使用的测试和诊断软件工具STBSuite简析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~