游戏开发入门系列(目录)第二章第一节

想从事一下开发游戏.

然后我是根据https://blog.csdn.net/u012999985/article/details/79118198链接学习的gad视频

笔记一般会记在ipad上, 这里放一下代码.

第一个题目:

有一个炮台会发射炮弹攻击飞机, 炮台的炮弹每秒钟可发射1个, 假设目前发射的是激光炮, 没有中间的飞行距离的考虑,

但是有50%的几率命中, 飞机每3秒钟产生一架. 当炮弹命中飞机时, 飞机爆炸并消失. 整个世界在30秒后, 结束.

~在炮台发射炮弹, 炮弹命中飞机, 炮弹没有命中飞机, 飞机产生, 飞机被命中爆炸这些时间点上, 在控制台上打印信息

~提示: 可以用定时器, 可以用一个死循环, 不断计算当前时间来计时

 

下面是我的代码

#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
class Gun {
private:
    int alive_plane_num;
    time_t now_time, shoot_pasttime;
    inline void now(){ now_time=time(NULL); }
public:
    Gun(){shoot_pasttime=0;}
    int shoot(int num){
        now();
        if(now_time-shoot_pasttime>1 && num>0 ){
            shoot_pasttime = now_time;
            cout<<"shoot!"<<endl;
            return 1;
        }
        else{ return 0; }
    }
};

class PlaneArea {
private:
    int alive_plane_num;
    time_t now_time, fly_pasttime;
    inline void now() {now_time=time(NULL); }
public:
    PlaneArea(){ alive_plane_num=0; fly_pasttime=0; alive_plane_num=0;}
    int fly(){
        now();
        if(now_time-fly_pasttime>3){
            fly_pasttime = now_time;
            ++alive_plane_num;
            cout<<"出现一个飞机    飞机总数:"<<alive_plane_num<<endl;
            return alive_plane_num;
        }
        else { return alive_plane_num; }
    }
    void shoot_alive_plane_num( int num ){
        if(alive_plane_num != 0 && num!=0){
            alive_plane_num -= num;
            cout<<"天空爆炸并消失了一架飞机     剩余飞机:"<<alive_plane_num<<endl;
        }
    }
};

int goal(int bullet, int plane){
    if(bullet==0){
        return 0;
    }
    else if(bullet==1 && plane>0){
        srand( (unsigned)time(NULL) );
        if(rand()%2==0 ){
            cout<<"击中"<<endl;
            return 1;
        }
        else{
            cout<<"没击中"<<endl;
            return 0;
        }
    }
    return 0;
}

int main()
{
    Gun gun;
    PlaneArea planearea;
    time_t now_time,start_time;
    start_time = now_time = time(NULL);
    int alive_plane_flying;
    int num;
    while(now_time-start_time < 30){
        now_time = time(NULL);
        alive_plane_flying = planearea.fly();
        num = goal( gun.shoot( alive_plane_flying ), alive_plane_flying );
        planearea.shoot_alive_plane_num( num );
    }
    return 0;
}

该题目的代码我认为需要优化.

我的设计想法是:

Planearea类用来发射飞机, 并且会返回一个值----目前天上存活的飞机数量

Gun类代表激光枪, 用来发射激光.

goal函数用来判断是否击中, 如果击中会返回一个击中的值(当然在这里只能是1or0)

疑惑一: 但是不知道为什么, 这样的程序运行个十几秒. 我的笔记本电脑风扇就开始狂转.

是因为程序写的不好吗?  需要一种方法能让我测评程序的运行状况, 程序的时间, 空间代价

疑惑二: 程序分析的不好, 要解耦, 各个类还应该有各自的职责. 写到main的时候, 写的不是很舒服.

 

 

不幸的是, 策划觉得这个游戏不好玩, 需求变更:

在上个世界中, 战斗变得更加激烈, 飞机每1秒钟会出现两架. 飞机在位置x:0, y:100

及x:100, y:100的位置产生, 激光炮在位置x:50,y:0上. 飞机在y轴上以每秒钟20的速度向y:0

的方向飞行, 一旦有飞机飞到了y:0, 则世界结束

提示: 激光炮应该首先设计离y:0更近的飞机, 而不是更远

#include<iostream>

int main(){
//代码正在写.
return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值