枪战C++小游戏

C++编程实现的战斗模拟与生存游戏

直接上代码

#include <bits/stdc++.h>
#include<conio.h>
#include<windows.h>
using namespace std;
int gj3 = 100, fy3 = 100, sm3 = 1000, gu = 1, sc = 0;
char* (s[]) = { "小刀","铁刀","铜刀","银刀","金刀","手枪","步枪","突击步枪","狙击枪","冲锋枪","藤甲","铁甲","铜甲","银甲","金甲" };
int sl[15] = { 0 }, ysl = 0, msx;
string name;
int sm1, sm2, gj1, gj2, fy1, fy2, rs = 999999, rw, gos, smc, rea, sr, srt, tes, sa, srf, srth; //smc是血量上限;
int gj[] = { 10,15,20,30,50,80,100,120,150,200,0,0,0,0,0 }, fy[] = { 0,5,10,15,20,0,0,0,0,0,20,30,50,80,100 }; //攻击、防御、数量、医疗箱数量
void bag()
{
    int xz1, jx;
    for (int i = 0; i < 15; i++)
    {
        cout << s << ": " << sl << "级" << endl;
    }
    cout << "医疗箱:" << ysl << "个" << endl << "0.退出 1.使用医疗箱" << endl;
    cin >> xz1;
    system("cls");
    if (xz1 == 1) {
        if (ysl == 0)
        {
            cout << "你没有医疗箱!" << endl;
            Sleep(1000);
        }
        if (smc <= sm1) 
        {
            cout << "你已满血!";
            Sleep(1000);
        }
        else
        {
            jx = sm1 + 500;
            if (jx > smc)
            {
                jx = smc;
            }
            sm1 = jx;
            cout << "加血成功!";
            Sleep(1000);
            ysl--;
        }
        system("cls");
    }
}
int into() {
    srand(time(NULL));
    sa = rand() % 16;
    switch (sa) 
    {
    case 15:
        ysl++;
        cout << "你捡到了医疗箱一级";
        break;
    default:
        cout << "你捡到了" << s[sa] << "一级";
        gj1 += gj[sa];
        fy1 += fy[sa];
        sl[sa]++;
        break;
    }
    Sleep(1000);
    system("cls");
    return 0;
}
int unp()
{
    int sm4, gj4, fy4;
    cout << "第" << gu << "关" << endl;
    cout << "你碰到了野兽" << endl;
    Sleep(2000);
    sm4 = sm3;
    gj4 = gj3;
    fy4 = fy3;
    while (sm1 > 0 || sm4 > 0)
    {
        if (gj1 - fy4 <= 0)
        {
            cout << "他被打掉了0滴血,还剩" << sm4 << "滴血";
            Sleep(1000);
            system("cls");
            sc++;
        }
        else
        {
            sm4 = sm4 - gj1 + fy4;
            cout << "他被打掉了" << gj1 - fy4 << "滴血,还剩" << sm4 << "滴血";
            Sleep(1000);
            system("cls");
        }
        if (sm4 <= 0)
        {
            cout << "他死了!";
            Sleep(1000);
            system("cls");
            into();
            gu++;
            return 0;
        }
        if (gj4 - fy1 <= 0)
        {
            cout << "你被打掉了0滴血,还剩" << sm1 << "滴血";
            Sleep(1000);
            system("cls");
            sc++;
        }
        else 
        {
            sm1 = sm1 - gj4 + fy1;
            cout << "你被打掉了" << gj4 - fy1 << "滴血,还剩" << sm1 << "滴血";
            Sleep(1000);
            system("cls");
        }
        if (sc == 2)
        {
            cout << "你们打了个平手";
            Sleep(1000);
            system("cls");
            into();
            sc = 0;
            return 0;
        }
        if (sm1 <= 0) 
        {
            cout << "你死了!";
            Sleep(1000);
            system("cls");
            return 0;
        }
        sc = 0;
    }
    gj3 += 50;
    fy3 += 50;
    sm3 += 100;
}
void puto()
{
    srf = rand() % 15;
    Sleep(199);
    srth = rand() % 2;
    if (srth == 1)
    {
        if (gj1 - srf < 0)
        {
            srf = gj1;
        }
        cout << "你丢失了" << srf << "点攻击力";
        gj1 -= srf;
    }
    if (srth == 0)
    {
        if (fy1 - srf < 0)
        {
            srf = fy1;
        }
        cout << "你丢失了" << srf << "点防御力";
        fy1 -= srf;
    }
    Sleep(1500);
    system("cls");
}

int unf() 
{
    int gf, xzi, js = 0;
    sm2 = rand() % 2200 + 800;
    gj2 = rand() % 500 + 200;
    fy2 = rand() % 300 + 10;
    cout << "你碰到了一个人 1.战斗 2.逃跑" << endl;
    cin >> tes;
    gf = (gj2 - fy1) / 3;
    if (tes == 2)
    {
        if (gf < 0)
        {
            gf = 0;
        }
        cout << "你还是被他打掉了" << gf << "滴血,然后逃脱成功!";
        Sleep(1500);
        system("cls");
        sm1 -= gf;
    }
    if (tes == 1)
    {
        while (sm1 > 0 && sm2 > 0 && js != 1) 
        {
            if (gj1 - fy2 <= 0)
            {
                cout << "他被打掉了0滴血,还剩" << sm2 << "滴血";
                Sleep(1000);
                system("cls");
                sc++;
            }
            else
            {
                sm2 = sm2 - gj1 + fy2;
                cout << "他被打掉了" << gj1 - fy2 << "滴血,还剩" << sm2 << "滴血";
                Sleep(1000);
                system("cls");
            }
            if (sm2 <= 0)
            {
                cout << "他死了!";
                Sleep(1000);
                system("cls");
                rs--;
                into();
                return 0;
            }
            if (gj2 - fy1 <= 0)
            {
                cout << "你被打掉了0滴血,还剩" << sm1 << "滴血";
                Sleep(1000);
                system("cls");
                sc++;
            }
            else
            {
                sm1 = sm1 - gj2 + fy1;
                cout << "你被打掉了" << gj2 - fy1 << "滴血,还剩" << sm1 << "滴血";
                Sleep(1000);
                system("cls");
            }
            if (sc == 2)
            {
                cout << "你们打了个平手";
                Sleep(1000);
                system("cls");
                into();
                sc = 0;
            }
            sc = 0;
        }
    }

    if (sm1 <= 0)
    {
        cout << "你死了!重新开始!";
        Sleep(1000);
        system("cls");
    }
}
void sra() 
{
    sr = rand() % 9;
    switch (sr)
    {
    case 0:
        if (msx == 2)
        {
            unf();
        }
        else
        {
            unp();
        }
        break;
    case 1:
        into();
        break;
    case 2:
        into();
        break;
    case 3:
        puto();
        break;
    case 4:
        puto();
        break;
    case 5:
        puto();
        break;
    case 6:
        puto();
        break;
    default:
        cout << "你没有获得什么……";
        Sleep(1000);
        system("cls");
        break;
    }
}
int main()
{
    srand(time(NULL));
    cout << "你的名字:";
    cin >> name;
    system("cls");
aga:
    cout << "0.退出游戏 1.开始游戏" << endl;
    cin >> gos;
    system("cls");
    if (gos == 0) {
        cout << "由似花还似飞花出品,欢迎再来!!";
        Sleep(1500);
        return 0;
    }
retu:
    while (1)
    {
        int sl[15] = { 0 }, ysl = 0;
        if (msx == 2)
        {
            cout << "1.无限闯关模式 2.实战模式" << endl;
            cin >> msx;
            system("cls");
            cout << name << ",生死枪战,现在开始!!";
            Sleep(1500);
            system("cls");
            cout << "名字:" << name;
            cout << endl << "(人越多越好玩)(包括你自己) " << "枪战人数:";
            cin >> rs;
            system("cls");
        }
    xz:
        cout << "人物选择:" << endl;
        cout << "1.步兵 生命:1000 攻击:50 防御:50" << endl;
        cout << "2.丧尸 生命:1250 攻击:35 防御:40" << endl;
        cout << "3.医生 生命:1500 攻击:20 防御:30" << endl;
        cout << "4.铠甲人 生命:800 攻击:40 防御:80" << endl;
        cin >> rw;
        system("cls");
        switch (rw)
        {
        case 1:
            sm1 = 1000;
            smc = 1000;
            gj1 = 50;
            fy1 = 50;
            break;
        case 2:
            sm1 = 1250;
            smc = 1250;
            gj1 = 35;
            fy1 = 40;
            break;
        case 3:
            sm1 = 1500;
            smc = 1500;
            gj1 = 20;
            fy1 = 30;
            break;
        case 4:
            sm1 = 800;
            smc = 800;
            gj1 = 40;
            fy1 = 80;
            break;
        default:
            goto xz;
        }
        cout << "开局一抽奖" << endl;
        Sleep(1500);
        sr = rand() % 16;
        switch (sr) 
        {
        case 15:
            ysl += 2;
            cout << "你获得了医疗箱二级";
            break;
        default:
            cout << "你获得了" << s[sr] << "二级";
            gj1 += gj[sr] * 2;
            fy1 += fy[sr] * 2;
            sl[sr] += 2;
            break;
        }
        Sleep(1000);
        system("cls");
        while (sm1 > 0 && rs > 1)
        {
            cout << "名字:" << name << endl;
            cout << "生命:" << sm1 << endl;
            cout << "攻击:" << gj1 << endl;
            cout << "防御:" << fy1 << endl;
            cout << "1.向前 2.向左 3.向右 4.向后 5.查看背包 6.退出" << endl;
            cin >> rea;
            system("cls");
            if (rea == 6)
            {
                system("cls");
                goto aga;
            }
            if (rea == 5)
            {
                bag();
            }
            if (rea >= 1 && rea <= 4)
            {
                sra();
            }
        }
        if (sm1 > 0 && rs == 1)
        {
            cout << "你赢了!再来一局!" << endl;
            Sleep(1000);
            system("cls");
            goto retu;
        }
    }
    return 0;
}

求点赞加关注!!!! 

用DDraw实现射击游戏说明文档 要点一:画图自动切割 IDirectDrawSurface7::BltFast()方法中没有自动切割功能,即当画图元素超出窗口以外时不会自动切割,DDraw选择自动忽略不画,造成一旦超出窗口,画图元素会突然消失。 解决这一问题的方法是手动切割,代码如下: //自动切割 RECT scRect; //存放当前窗口大小区域 ZeroMemory( &scRect, sizeof( scRect ) ); GetWindowRect( GetActiveWindow(), &scRect ); //防止图片左上角超过窗口左上角 if ( x < 0 ) { m_rect.left -= x; x = 0; } if ( y scRect.right ? scRect.right : x; y = y > scRect.bottom ? scRect.bottom : y; m_rect.right = x + m_rect.right - m_rect.left > scRect.right ? scRect.right - x + m_rect.left : m_rect.right; m_rect.bottom = y + m_rect.bottom - m_rect.top > scRect.bottom ? scRect.bottom - y + m_rect.top : m_rect.bottom; 只需将上述代码加在CGraphic::BltBBuffer() 中的m_bRect = m_rect; 前即可。 要点二:背景的滚轴实现 画背景可以分为以下三种情况: 情况一:背景图片与窗口等高 情况二:背景图片高度小于窗口高度 情况三:背景图片高度大于窗口高度 上述讲解图与代码相对应地看,有助于容易理解。 另外,要点一实现之后,由于已经可以自动切割,画背景可以用其它方法。 要点三:精灵图的实现 在游戏中,如RPG游戏中的人物图、射击类游戏的飞机、爆炸等,叫做精灵图。 精灵图实际上是将所有帧的图片放在一个文件中,游戏时靠一个RECT来控制画图像文件中的哪一部分,进而控制游戏显示哪一帧图,只需控制好RECT的位置即可。如下图: 控制RECT的四个角的坐标的移动,有以下代码: if (m_timeEnd – m_timeStart > 100) //只有到了100ms之后才绘图 { m_ImageID++; if(m_ImageID - m_beginID >= num) { m_ImageID = m_beginID; //最后一帧的下一帧是第一帧 } m_timeStart = timeGetTime(); } int id = m_ImageID++; SetRect(&m_rect, 41 * id, 0, 41 * (id + 1), 41); //飞机精灵图大小是41×41 m_pGraph->BltBBuffer(m_pImageBuffer, true, m_Pos.x, m_Pos.y, m_rect); 这样就实现了精灵动画的效果。 要点四:拿STL进行子弹的实现 子弹的实现可以使用STL中的vector,当按下开火键时发出一颗子弹,就往vector中添加一个结点;当子弹飞出窗口或击中敌机时,再将结点从vector中删除。每帧游戏画面中子弹飞行时只需将vector中的所有子弹进行处理、绘画即可。 参考代码如下: 1.添加子弹 if (g_ctrlDown) //当ctrl键按下时开炮! { m_BulletEnd = m_Gtime->GetTime(); if ((m_BulletEnd - m_BulletStart) * 1000 > 120) //如果连续按着开火键不放,这里控制不会发出太多子弹 { m_BulletStart = m_BulletEnd; MBULLET tmpBullet; tmpBullet.pos.x = m_SPos.x - 1; //记录开火时的子弹位置 tmpBullet.pos.y = m_SPos.y - 26; tmpBullet.speed = 5; //该子弹的飞行速度 m_BulletList.push_back(tmpBullet); //将子弹添加到vector中
### 关于C++枪战小游戏的源代码 对于寻找基于C++开发枪战小游戏的源代码或教程,可以从以下几个方面入手: #### GitHub资源推荐 GitHub是一个非常丰富的开源项目平台,其中有许多开发者分享了自己的游戏项目。以下是几个可能满足需求的项目链接[^3]: - **Project Name**: Simple Shooter Game 这个项目提供了一个基础的3D射击游戏框架,使用了OpenGL和GLFW库来渲染图形界面,并通过键盘输入实现玩家控制功能。虽然它并非完全成熟的商业级产品,但对于学习如何构建一个简单的3D环境以及处理基本的游戏逻辑很有帮助。 ```cpp void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) camera.ProcessKeyboard(FORWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) camera.ProcessKeyboard(BACKWARD, deltaTime); if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) camera.ProcessKeyboard(LEFT, deltaTime); if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) camera.ProcessKeyboard(RIGHT, deltaTime); } ``` - **Repository Link**: https://github.com/example/simple-shooter-game --- #### Unreal Engine中的C++支持 如果希望尝试更复杂的功能并利用现代引擎的优势,则可以考虑Unreal Engine 4/5作为开发工具之一。尽管UE主要面向蓝图脚本用户友好型设计,但它同样允许程序员深入底层编写自定义行为。例如,在创建分层动画时提到的技术——`Layered Blend Per Bone`[^2]——能够显著提升角色动作的真实感。 不过需要注意的是,这类解决方案通常适用于具有一定规模团队协作场景下的大型项目;如果是个人练习或者小型实验性质的话可能会显得有些大材小用了。 --- #### 教程建议 除了直接获取现成代码外,还可以参考在线课程进一步理解原理背后的知识点。下面列举了一些高质量的学习资料供参考: 1. **YouTube Channel - TheCherno** 提供了一系列关于Game Development with C++的基础教学视频,涵盖了从窗口初始化到物理碰撞检测等多个主题。 2. **Udemy Course - Learn C++ by Making Games** 面向初学者的一门综合性课程,带领学员逐步完成多个不同类型的小型游戏制作过程,其中包括第一视角射击器原型的设计思路讲解。 --- ### 总结 综上所述,无论是借助传统标准库还是现代化跨平台框架都可以达成目标效果。具体选择取决于实际应用场景和个人偏好等因素考量结果而定。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值