美文网首页
网易c++------课时42---飞机大战,案例未完成

网易c++------课时42---飞机大战,案例未完成

作者: heiqimingren | 来源:发表于2020-10-13 16:10 被阅读0次

    这个案例完成度85%左右,
    还有以下几个细节没有做到位。
    01,背景音乐播放之后,发出子弹的声音会被覆盖掉,不知道如何解决?
    02,飞机中弹之后,飞机爆炸的效果,还没有完善。

    #include<stdio.h>
    #include<stdlib.h>    //清屏命令在这里。
    #include <windows.h> //延时10毫秒-sleep,gotoxy函数
    #include <iostream>
    #include <conio.h>       //getch()----不用按回车,就可以输入字符。    
    #include <graphics.h>      //EasyX库,所使用的!
    #include <math.h>         //三角函数
    
    using namespace std;
    
    
    #pragma comment(lib,"Winmm.lib" )
    //游戏画面尺寸
    #define High 864
    #define  Width 591
    IMAGE img_bk;  //背景图片
    IMAGE img_planeNormal1, img_planeNormal2;  //正常飞机图片
    int position_x, position_y;  //飞机的x坐标和Y坐标
    
    
    IMAGE img_bullet1, img_bullet2;  //子弹图片
    int bullet_x, bullet_y;      //子弹的x坐标和Y坐标
    
    IMAGE img_enemyPlane1, img_enemyPlane2;  //敌机图片
    float enemy_x, enemy_y;            //敌机的x坐标和Y坐标
    
    
    
    
    void startup()
    {
        initgraph(Width, High);
    
        //播放背景音乐
        mciSendString(_T("open E:\\飞机大战图片音乐素材\\game_music.mp3 alias bkmusic"), NULL, 0, NULL);  //打开XX
        mciSendString(_T("play bkmusic"), NULL, 0, NULL);  //仅播放一次
    
        
        loadimage(&img_bk, _T("E:\\飞机大战图片音乐素材\\background.jpg"));  //读取图片到img对象中。
        
        loadimage(&img_planeNormal1, _T("E:\\飞机大战图片音乐素材\\planeNormal_1.jpg"));  //读取遮罩图片到img对象中。
        loadimage(&img_planeNormal2, _T("E:\\飞机大战图片音乐素材\\planeNormal_2.jpg"));  //读取正常飞机图片到img对象中。
    
        loadimage(&img_bullet1, _T("E:\\飞机大战图片音乐素材\\bullet1.jpg"));  //读取遮罩子弹图片到img对象中。
        loadimage(&img_bullet2, _T("E:\\飞机大战图片音乐素材\\bullet2.jpg"));  //读取正常子弹图片到img对象中。
    
        loadimage(&img_enemyPlane1, _T("E:\\飞机大战图片音乐素材\\enemyPlane1.jpg"));  //读取遮罩子弹图片到img对象中。
        loadimage(&img_enemyPlane2, _T("E:\\飞机大战图片音乐素材\\enemyPlane2.jpg"));  //读取正常子弹图片到img对象中。
    
    
        position_x = Width*0.5;
        position_y = High*0.5;   //给飞机设置一下x坐标和Y坐标
    
        bullet_x = position_x;
        bullet_y = -100;     //设置子弹初始位置,让子弹放在屏幕外面。
    
        enemy_x = Width*0.5;
        enemy_y = 0;             //敌机的初始位置。
    
        
    
        BeginBatchDraw();
    
    }
    
    
    void show()
    {
        putimage(0, 0, &img_bk);     //在坐标0.0位置处显示背景图像对象
    
        putimage(position_x, position_y, &img_planeNormal1, NOTSRCERASE); //正常飞机,遮罩图片
        putimage(position_x, position_y, &img_planeNormal2, SRCINVERT);  //原始飞机图片
    
        putimage(bullet_x, bullet_y, &img_bullet1, NOTSRCERASE); //正常子弹,遮罩图片
        putimage(bullet_x, bullet_y, &img_bullet2, SRCINVERT);  //原始子弹图片
    
        putimage(enemy_x, enemy_y, &img_enemyPlane1, NOTSRCERASE); //敌机,遮罩图片
        putimage(enemy_x, enemy_y, &img_enemyPlane2, SRCINVERT);  //敌机图片
    
    
        FlushBatchDraw();
        Sleep(5);
    }
    
    void updataWithoutInput()
    {
        if (bullet_y>-50)     
        {
            bullet_y = bullet_y - 2;  //子弹向上移动
        }
        //让敌机自动往下落
        if (enemy_y<High)
        {
            enemy_y = enemy_y + 1.5;  //防止敌机下落过快,给敌机坐标设置成浮点数
        }
        else //飞机落下之后,重新生成代码
        {
            enemy_x = rand()%Width;
            enemy_y = 0;
        }
        //判断子弹是否打中了飞机
        if (abs(enemy_x-bullet_x )+abs(enemy_y-bullet_y )<150 ) //写法新颖
        {
            //敌机死掉之后,位置重置
            enemy_x = rand() % Width;
            enemy_y = 0;
        }
        //敌机击中我的飞机
        if (abs(enemy_x - position_x) + abs(enemy_y - position_y) <80) //写法新颖
        {
            //敌机死掉之后,位置重置
            exit(0);
        }
    
    }
    void updataWithInput()
    {   
        MOUSEMSG m;   //记录鼠标消息,创建一个鼠标对象m
        while (MouseHit())
        {
            m = GetMouseMsg();
            if (m.uMsg==WM_MOUSEMOVE )  //鼠标移动时候,飞机坐标等于鼠标指针坐标
            {
                position_x = m.x-50;
                position_y = m.y-50;
            }
            else if (m.uMsg==WM_LBUTTONDOWN )  //鼠标左键按下的时候,发射子弹
            {
                //按一下空格键,播放音效
                mciSendString(_T("close f_gun"), NULL, 0, NULL);   //先把前面一次的音乐关闭
                mciSendString(_T("open E:\\飞机大战图片音乐素材\\f_gun.mp3 alias  jpmusic"), NULL, 0, NULL);  //打开XX
                mciSendString(_T("play jpmusic"), NULL, 0, NULL);  //仅播放一次
    
                bullet_x = position_x+40;
                bullet_y = position_y - 25;    //通过修改子弹的位置,来实现发射子弹的效果。
            }
        }
    
    
        //mciSendString("close jpmusic", NULL, 0, NULL);   //先把前面一次的音乐关闭
        //mciSendString("open E:\\Jump.mp3 alias    jpmusic  ", NULL, 0, NULL);  //打开XX
        //mciSendString("play jpmusic", NULL, 0, NULL);  //仅播放一次
    }
    
    void gameover()
    {
        EndBatchDraw();
        getch();
        closegraph();
    }
    int main()
    {
        startup();
        while (1)
        {
            show();
            updataWithoutInput();
            updataWithInput();
        }
        gameover();
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:网易c++------课时42---飞机大战,案例未完成

          本文链接:https://www.haomeiwen.com/subject/jpbvpktx.html