小型飞机大战

6 篇文章 0 订阅

Problem F: 小型飞机大战Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1374 Solved: 1138
[Submit][Status]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见样例~Sample Input
    3
    enemy1 10 0
    enemy2 20 20
    enemy3 30 10
    4
    a
    s
    w
    d
    Sample Output
    WUDI at 50 50
    enemy1 at 10 0
    enemy2 at 20 20
    enemy3 at 30 10
    WUDI at 49 50
    enemy1 at 10 1
    enemy2 at 20 21
    enemy3 at 30 11
    WUDI at 49 51
    enemy1 at 10 2
    enemy2 at 20 22
    enemy3 at 30 12
    WUDI at 49 50
    enemy1 at 10 3
    enemy2 at 20 23
    enemy3 at 30 13
    WUDI at 50 50
    enemy1 at 10 4
    enemy2 at 20 24
    enemy3 at 30 14
    HINTAppend Codeappend.cc,
#include <iostream>
#include <string>
using namespace std;
class FlyThing{
public:
    string name;
    double x, y;
 
    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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值