找回密码
 立即注册
查看: 436|回复: 0

C语言开发《飞机大战游戏》

[复制链接]
发表于 2021-12-6 12:15 | 显示全部楼层 |阅读模式
我们通过C语言的学习及图形API函数的应用,直接实现一个简单的飞机空战游戏。
一、游戏操作说明:

w、s、a、d 控制飞机移动
k 发射子弹
空格暂停
ESC退出
二、敌机自动发射子弹、自动移动

当飞船碰到敌机、飞船血量为 0、敌机碰到底部时游戏结束
敌机血量为 0 时,会出现一个新的敌机
小敌机随机出现
结束后按 ESC 退出,按 R 重来。
三、游戏运行截图如下:





四、游戏环境(图形库下载)

该游戏的编译环境为:Visual C++ 2013,EasyX_20190219(beta)。
四、游戏完整源码如下:

#include "view.h"
#include "easyx.h"
#include "Enemy.h"
#include "SmallEnemy.h"

IMAGE boom1, boom2, boom3, boom4;
IMAGE smallboom1, smallboom2, smallboom3, smallboom4;

void LoadBoom()
{       
        loadimage(&boom1, _T("res\\boom\\boom1.png"));
        loadimage(&boom2, _T("res\\boom\\boom2.png"));
        loadimage(&boom3, _T("res\\boom\\boom3.png"));
        loadimage(&boom4, _T("res\\boom\\boom4.png"));

        loadimage(&smallboom1, _T("res\\boom\\smallboom1.png"));
        loadimage(&smallboom2, _T("res\\boom\\smallboom2.png"));
        loadimage(&smallboom3, _T("res\\boom\\smallboom3.png"));
        loadimage(&smallboom4, _T("res\\boom\\smallboom4.png"));
}

void InitGame()
{
        LoadBoom();
        mciSendString(_T("open res\\music.mp3 alias mysong"), NULL, 0, NULL);                // 打开音乐
        mciSendString(_T("play MySong repeat"), NULL, 0, NULL);                                 // 循环播放

        // 初始化开始界面窗口
        loadimage(&background, _T("res\\sky2.jpg"), GAMEWIDTH, GAMEHIGHT, true);
        putimage(0, 0, &background);                // 开始界面

        setbkmode(TRANSPARENT);                                // 字体透明
        settextcolor(BLUE);
        settextstyle(50, 20, _T("黑体"));
        outtextxy(GAMEWIDTH / 2 - 170, 25, _T("太空飞机大战 V3.0"));
        settextstyle(30, 20, _T("黑体"));
        outtextxy(GAMEWIDTH / 2 - 100, 280, _T("W S A D移动"));
        outtextxy(GAMEWIDTH / 2 - 100, 320, _T("K->发射子弹"));
        outtextxy(GAMEWIDTH / 2 - 110, 360, _T("空格暂停游戏"));
        outtextxy(GAMEWIDTH / 2 - 110, 400, _T("按任意键继续"));
        system("pause");
        cleardevice();

        // 加载飞机
        loadimage(&ship, _T("res\\redship.png"));
        loadimage(&ship1, _T("res\\redship1.png"));

        // 加载敌机
        loadimage(&enemy, _T("res\\enemy.png"));       
        loadimage(&enemy1, _T("res\\enemy1.png"));
        // 加载小敌机
        loadimage(&smallenemy, _T("res\\smallenemy.png"));
        loadimage(&smallenemy1, _T("res\\smallenemy1.png"));

        myenemy.direction = DOWN;
        myenemy.m_nStartHight = 0;
        myenemy.m_Move = 0;
        myenemy.m_Fire = 0;
        myenemy.m_Boom = 0;       
}

void ShowBullet()
{
        // 显示飞船子弹
        for (Iter = VectorBullets.begin(); Iter != VectorBullets.end(); Iter++)
        {
                if (VectorBullets.size() == 0)
                {
                        break;
                }
                if (*Iter == nullptr)
                {
                        break;
                }
                (*Iter)->ShowClear();
                (*Iter)->MoveUp();
                (*Iter)->ShowYellow();
        }

        // 显示敌机子弹
        for (Iter = VectorEnenyBullets.begin(); Iter != VectorEnenyBullets.end(); Iter++)
        {
                if (VectorEnenyBullets.size() == 0)
                {
                        break;
                }
                if (*Iter == nullptr)
                {
                        break;
                }
                (*Iter)->ShowClear();
                (*Iter)->MoveDown();
                (*Iter)->ShowRed();
        }
}

void ShowInPut()
{
        if (myinput & UPINPUT)
        {
                myship.ShowClear();
        }

        if (myinput & DOWNINPUT)
        {
                myship.ShowClear();
        }

        if (myinput & LEFTINPUT)
        {
                myship.ShowClear();
        }

        if (myinput & RIGHTINPUT)
        {
                myship.ShowClear();
        }
}
全部代码下载直接QQ哦:



#pragma once
#include "easyx.h"
#include "control.h"
#include "GameRole.h"

class CEnemy :public CGameRole
{
public:
        CEnemy::CEnemy(int nRow, int nCol, int nLife);
        ~CEnemy();
        char direction;                        // 敌机方向
        int m_nStartHight;                        // 敌机起始高度
        int m_Move;                                        // 敌机移动状态
        int m_Fire;                 // 敌机发射子弹状态

        void MoveDown();
        void MoveLeft();
        void MoveRight();
        void ShowClear();                        // 掩盖敌机
        void Show();                                // 绘制敌机
        void ShowClearLife();                // 掩盖敌机血量
        void ShowLife();                        // 显示敌机血量
        void GetHit();                                // 敌机中弹
        void ShowBoom();                        // 显示爆炸       
};
#include <time.h>
#include "Enemy.h"
#include "easyx.h"
#include "View.h"

CEnemy::CEnemy(int nRow, int nCol, int nLife)
{
        m_nRow = nRow;
        m_nCol = nCol;
        m_nLife = nLife;
        m_Boom = 0;
}

CEnemy::~CEnemy()
{

}

void CEnemy::MoveDown()
{
        m_nCol += 10;
}

void CEnemy::MoveLeft()
{
        m_nRow -= 10;
}

void CEnemy::MoveRight()
{
        m_nRow += 10;
}

void CEnemy::ShowClear()                                        // 掩盖敌机
{
        putimage(m_nRow, m_nCol, &enemy1, SRCAND);
}

void CEnemy::ShowClearLife()                                        // 掩盖敌机血量
{
        setfillcolor(BLACK);
        solidrectangle(m_nRow, m_nCol - 10, m_nRow + m_nLife, m_nCol - 5);
}

void CEnemy::Show()                                                // 绘制敌机               
{
        putimage(m_nRow, m_nCol, &enemy, SRCPAINT);
}

void CEnemy::ShowLife()                                        // 显示敌机血量
{
        setfillcolor(RED);
        fillroundrect(m_nRow, m_nCol - 10, m_nRow + m_nLife, m_nCol - 5, 6, 3);
}

void CEnemy::GetHit()                                                        // 敌机中弹
{
        if (m_nLife > 0)
        {
                m_nLife -= 5;
        }
}

void CEnemy::ShowBoom()                                                        // 显示爆炸
{       
        if (m_Boom >= 1 && m_Boom <= 3)
        {       
                putimage(m_nRow, m_nCol, enemy.getwidth(),
                        enemy.getheight(), &boom1, 0, 0);
        }

        if (m_Boom >= 4 && m_Boom <= 6)
        {
                putimage(m_nRow, m_nCol, enemy.getwidth(),
                        enemy.getheight(), &boom2, 0, 0);
        }
        if(m_Boom >= 7 && m_Boom <= 9)
        {
                putimage(m_nRow, m_nCol, enemy.getwidth(),
                        enemy.getheight(), &boom3, 0, 0);
        }

        if (m_Boom >= 10 && m_Boom <= 13)
        {
                putimage(m_nRow, m_nCol, enemy.getwidth(),
                        enemy.getheight(), &boom4, 0, 0);
        }
        if (m_Boom == 14)
        {
                cleardevice();
                m_Boom = 15;
        }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-15 04:08 , Processed in 0.140352 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表