热门标签 | HotTags
当前位置:  开发笔记 > IOS > 正文

C++实现雷霆战机可视化小游戏

这篇文章主要为大家详细介绍了C++实现雷霆战机可视化小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

用C++和easyx实现简单的雷霆战机小游戏

之前在网上看了许多关于c++或者是其他语言实现雷霆战机的帖子,大多不完整,或者是要付费才能阅读,现将源码展示如下,仅作学习交流之用。

基本原理

基本思路

代码如下

注意:运行代码所需环境为c++11或更高,代码中所涉及的图片、背景音乐等素材均需自行搜集并修改代码中的文件路径。

代码如有任何问题请在评论区指正,谢谢!

觉得帮到您了的话,可以考虑一下打赏博主呦,您的支持是我创作的最大动力!!!

/*seeting.h*/
#pragma once
void set_textstyle(LOGFONT& f, int high, int wei); //设置输出文字形式
void load_be_bk();      //加载初始背景
void help();        //帮助界面
void decorete(int x);      //初始界面装饰
void home(int i);        //初始界面
bool exit();        //退出游戏
/*seeting.cpp*/
#include 
#include 
#include 
#include 
#include "setting.h"

IMAGE be_bk01, be_bk02;
static int i = 0; //装饰时计数

void set_textstyle(LOGFONT& f, int high, int wei)
{
 gettextstyle(&f);  // 获取当前字体设置
 f.lfHeight = high;  // 设置字体高度为 48
 f.lfWeight = wei;
 _tcscpy_s(f.lfFaceName, _T("宋体")); // 设置字体
 f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿 
}

void decorate(int x) //装饰
{
 if ((x/10) % 2 == 0)
 {
 setlinecolor(RGB(255, 250, 250));
 line(202, 662, 71, 549); line(1157, 403, 1117, 259); 
 setlinecolor(RGB(0, 238, 0));
 line(1004, 413, 1157, 403); line(1188, 504, 1020, 534); line(228, 388, 202, 131);
 setlinecolor(RGB(255, 69, 0));
 line(202, 131, 404, 261); line(471, 493, 228, 388); line(1020, 534, 1307, 563);
 setlinecolor(RGB(0, 245, 255));
 line(1303, 291, 1034, 332); line(66, 373, 437, 600);
 setlinecolor(RGB(191, 62, 255));
 line(1178, 666, 1287, 616); line(404, 261, 202, 131);
 }
 else
 {
 setlinecolor(RGB(255, 218, 185));
 line(1501, 155, 23, 213); line(437, 600, 471, 493); line(1287, 616, 1034, 644);
 setlinecolor(RGB(84, 255, 159));
 line(348, 315, 103, 263); line(269, 606, 39, 697); line(1094, 462, 1004, 413);
 setlinecolor(RGB(30, 144, 255));
 line(71, 549, 269, 606); line(103, 263, 66, 373); line(1307, 563, 1303, 291);
 setlinecolor(RGB(255, 255, 0));
 line(1034, 640, 1188, 504); line(1034, 332, 985, 254); line(39, 697, 348, 315);
 setlinecolor(RGB(255, 0, 255));
 line(1117, 259, 942, 593); line(942, 593, 1178, 666); line(985, 254, 1292, 105);
 }
 setlinecolor(RGB(255, 255, 255));
}

void load_be_bk() //加载帮助界面背景
{
 loadimage(&be_bk01, L"image\\be_bk01.jpg", 1360, 760);
 putimage(0, 0, &be_bk01);
}

void help() //帮助界面
{
 while (1)
 {
 BeginBatchDraw();
 load_be_bk();
 decorate(i);
 roundrect(526, 126, 834, 634, 30, 30);
 roundrect(528, 128, 832, 632, 30, 30);
 roundrect(530, 130, 830, 630, 30, 30);
 LOGFONT f;
 set_textstyle(f, 30, 5);
 settextstyle(&f);  // 设置字体样式
 outtextxy(595, 170, _T("游 戏 须 知"));
 line(550, 220, 810, 220);
 set_textstyle(f, 20, 3);
 settextstyle(&f);
 outtextxy(555, 240, _T("① 按下 w a s d 移动飞机。"));
 outtextxy(555, 280, _T("② 按下 空格 发射子弹。"));
 outtextxy(555, 320, _T("③ 游戏中按 ESC 暂停。"));
 outtextxy(555, 360, _T("④ 每撞机一次,生命值减少20,"));
 outtextxy(555, 400, _T(" 通过本关,生命值恢复。"));

 set_textstyle(f, 15, 2);
 settextstyle(&f);
 outtextxy(625, 525, _T("按任意键返回"));
 line(550, 550, 810, 550);
 outtextxy(605, 570, _T("制作不易,请勿抄袭"));
 outtextxy(605, 600, _T("    ——木笔制作"));
 i++;
 EndBatchDraw();
 Sleep(10);
 if (_kbhit())
 {
 _getch(); break;
 }
 }

}


void home(int i) //初始界面
{
 BeginBatchDraw();
 loadimage(&be_bk02, L"image\\be_bk02.jpg", 1360, 760);
 putimage(0, 0, &be_bk02);
 int m = 465, n = 515; //输出框上下边界
 for (int t = 0; t <4; t++)
 {
 for (int j = 0; j <4; j++)
 {
 roundrect(1110 + j, m + j, 1300 - j, n - j, 30, 30);
 }
 m += 65;
 n += 65;
 }
 switch (i)
 {
 case 1:
 for (int j = 0; j <30; j++)
 {
 setlinecolor(RGB(0+j*5, 255 - j * 4, 217 - j * 5));
 line(1120, 475 + j, 1290, 475 + j);
 }
 break;
 case 2:
 for (int j = 0; j <30; j++)
 {
 setlinecolor(RGB(0 + j * 5, 255 - j * 4, 217 - j * 5));
 line(1120, 540 + j, 1290, 540 + j);
 }
 break;
 case 3:
 for (int j = 0; j <30; j++)
 {
 setlinecolor(RGB(0 + j * 5, 255 - j * 4, 217 - j * 5));
 line(1120, 605 + j, 1290, 605 + j);
 }
 break;
 case 4:
 for (int j = 0; j <30; j++)
 {
 setlinecolor(RGB(0 + j * 5, 255 - j * 4, 217 - j * 5));
 line(1120, 670 + j, 1290, 670 + j);
 }
 break;
 }
 setlinecolor(RGB(152, 245, 255));
 LOGFONT f;
 gettextstyle(&f);
 f.lfHeight = 30;
 f.lfWeight = 7;
 _tcscpy_s(f.lfFaceName, _T("隶书")); 
 f.lfQuality = ANTIALIASED_QUALITY;
 settextstyle(&f);
 setbkmode(TRANSPARENT); //透明字体背景
 settextcolor(RGB(255, 0, 0));
 outtextxy(1125, 475, _T("简单模式(1)"));
 outtextxy(1125, 540, _T("困难模式(2)"));
 outtextxy(1125, 605, _T(" 帮 助 (3) "));
 outtextxy(1125, 670, _T(" 退 出 (4) "));
 f.lfHeight = 70;
 f.lfWeight = 30;
 _tcscpy_s(f.lfFaceName, _T("方正舒体"));
 settextstyle(&f);
 for (int j = 0; j <5; j++)
 roundrect(950+j, 280+j, 1300-j, 410-j, 50, 50);
 settextcolor(RGB(255, 165, 0));
 outtextxy(960, 300, _T("外星人入侵"));
 EndBatchDraw();
}

bool exit() //退出游戏
{
 loadimage(&be_bk02, L"image\\be_bk02.jpg", 1360, 760);
 putimage(0, 0, &be_bk02);
 LOGFONT f;
 set_textstyle(f, 50, 20);
 settextcolor(RGB(255, 255, 255));
 outtextxy(390, 230, _T("是否要退出游戏&#63;"));
 outtextxy(310, 320, _T("是(回车)否(任意键)"));
 FlushBatchDraw();
 char hit = _getch();
 if (hit == 13)
 return 1;
 else
 return 0;
}
/*music.h*/
#pragma once
#include 
#include 
typedef int(__stdcall* w32mci)(const char*, char*, int, int);
typedef int(__stdcall* w32mcierror)(int, char*, int);
using namespace std;

class Mci
{
private:
 HINSTANCE hins;
 w32mci wmci;
 w32mcierror wmcierror;
public:
 Mci();
 ~Mci();
 char buf[256];
 bool send(string command);
};
class AudioClip
{
private:
 Mci mci;
 std::string filename;
 std::string alias;
 int length_ms;
public:
 AudioClip();
 ~AudioClip();
 bool load(const std::string& _filename);
 bool play(int start_ms = 0, int end_ms = -1);
 bool stop();
 bool pause();
 bool unpause();
 int milliseconds();
};

/*music.cpp*/
#include "music.h"
#include
#include 
#include 
#include
#include
Mci::Mci()
{
 HINSTANCE hins = LoadLibraryA("winmm.dll");
 wmci = (w32mci)GetProcAddress(hins, "mciSendStringA");
 wmcierror = (w32mcierror)GetProcAddress(hins, "mciGetErrorStringA");
}
Mci::~Mci()
{
 FreeLibrary(hins);
}
bool Mci::send(std::string command)
{
 int errcode = wmci(command.c_str(), buf, 254, 0);
 if (errcode)
 {
 wmcierror(errcode, buf, 254);
 return false;
 }
 return true;
}
AudioClip::AudioClip()
{
 //do nothing
}
AudioClip::~AudioClip()
{
 std::string cmd;
 cmd = "close " + alias;
 mci.send(cmd);
}
bool AudioClip::load(const std::string& _filename)
{
 filename = _filename;
 for (unsigned int i = 0; i 
/*alien.h*/
#pragma once
#include 
class Alien
{
public:
 int alien_x = 0, alien_y = -40; //外星人坐标
 IMAGE alien01;      //外星人图片

 void load_alien();   //加载外星人图片
 void show_alien();   //绘制外星人
 Alien();
};
/*alien.cpp*/
#include 
#include "alien.h"
#include 

void Alien::load_alien()
{
 loadimage(&alien01, L"image\\alien.jpg", 40, 40);
}

void Alien::show_alien()
{
 putimage(alien_x, alien_y, &alien01);
}

Alien::Alien()
{
 load_alien();
}
/*airplane.h*/
#pragma once
class Airplane
{
public:
 int airplane_x = 650, airplane_y = 720; //飞机初始坐标
 IMAGE plane01;       //飞机图片
 IMAGE background;      //背景图
 char key_hit;       //接受按键
 int airplane_speed = 10;     //飞船移动速度

 Airplane();  //构造函数
 void load_image();//加载飞机图片
 void show_plane();//绘制飞机图片
 void show_bg(); //绘制背景图片
 void key_Down(char hit); //按键响应
};

/*airplane.cpp*/
#include 
#include 
#include 
#include "Airplane.h"

void Airplane::load_image() //加载飞机图片和背景图
{
 loadimage(&plane01, L"image\\plane01.jpg", 60, 40);
 loadimage(&background, L"image\\background01.jpg", 1360, 760);
 rotateimage(&plane01, &plane01, 3.1415926);
}

void Airplane::show_bg() //绘制背景图
{
 putimage(0, 0, &background);
}

void Airplane::show_plane() //绘制飞机图片
{
 putimage(airplane_x, airplane_y, &plane01);
}

void Airplane::key_Down(char hit) //按键响应
{
 key_hit = hit;
 switch (key_hit)
 {
 case 'w':
 case 'W': if(airplane_y > 500) airplane_y-=airplane_speed; break;
 case 's':
 case 'S': if (airplane_y <720) airplane_y+= airplane_speed; break;
 case 'a':
 case 'A': if (airplane_x > 0) airplane_x-= airplane_speed; break;
 case 'd':
 case 'D': if (airplane_x <1300) airplane_x+= airplane_speed; break;
 default: break;
 }
}

Airplane::Airplane()
{ 
 load_image();
 
}
/*bullet.h*/
#pragma once
class Bullet
{
public:
 int bullet_x = 0, bullet_y = 0; //子弹坐标
 IMAGE bullet01;     //子弹图片 
 int bullet_speed = 15;   //子弹速度

 void load_bullet(); //加载子弹图片
 void show_bullet(); //绘制子弹图片
 Bullet();
};
/*bullet.cpp*/
#include 
#include "bullet.h"

void Bullet::load_bullet()
{
 loadimage(&bullet01, L"image\\bullet.jpg", 10, 30);
}

void Bullet::show_bullet()
{
 putimage(bullet_x, bullet_y, &bullet01);
}

Bullet::Bullet()
{
 load_bullet();
}
/*launch.h*/
#pragma once

bool choose_all(char key, int &i); //初始选择界面按键处理
void load_music01();     //加载初始界面音乐

/*launch.cpp*/
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include "airplane.h"
#include "bullet.h"
#include "alien.h"
#include "music.h"
#include "setting.h"
using namespace std;

static int score = 0;  //得分
int tx = 0;     //存储上次得分信息
static int level = 1;  //关卡
int player_eneygy = 100; //玩家血量
static int alien_speed = 1; //外星人移动速度
int alien_num = 5;   //初始外星人数量

AudioClip mu_be01, choose, level13, level46, level710, bulbul, gameov, levelup;   //音乐
void load_music01() //加载初始界面音乐
{
 mu_be01.load("music\\be_bg01.mp3");
 mu_be01.play();
}

void trans_int_ch(wchar_t*& wides, int num) //将数字转换为能够被outtextxy输出的wchar_t型
{
 string temp = to_string(num);
 char s[100];
 int i;
 for (i = 0; i = 0 && point.x <= 1360 && point.y >= 500 && point.y <= 760)
 {
 b.bullet_x = point.x;
 b.bullet_y = point.y;
 }
}

void add_alien(deque& a) //添加外星人
{
 Alien ali;
 int x = 0;  //作为随机数
 while (1)
 {
 int i = 0;
 x = rand() % 1320;
 for (int k = 0; k = a[k].alien_x && x <= a[k].alien_x + 40)
 i++;
 }
 if (i == 0)
 break;
 else
 i = 0;
 }
 ali.alien_x = x;
 a.push_back(ali);
}

void update_level(deque& a) //更新等级和外星人数量信息
{
 if (score > tx)
 if (score >= 10 && score % 10 == 0)
 {
 level++;
 levelup.load("music\\levelup.mp3");
 levelup.play();
 player_eneygy = 100;
 alien_speed *= 1.5;
 add_alien(a);
 tx = score;
 }
}

void update_energy(Airplane pl, deque &a) //更新血量
{
 int i = 0, t = 0;
 for ( ; i = a[i].alien_x && pl.airplane_y == a[i].alien_y + 30)||
 (pl.airplane_y <= a[i].alien_y + 40 && pl.airplane_y + 40 >= a[i].alien_y && 
 pl.airplane_x <= a[i].alien_x + 40 && pl.airplane_x + 60 >= a[i].alien_x))
 {
 player_eneygy -= 20;
 t++;
 break;
 }
 }
 if (t != 0)
 {
 a.erase(a.begin() + i);
 add_alien(a);
 }

}

void draw_bu_al(deque& a, deque& b)  //更新并绘制外星人和子弹
{
 vector as;
 vector bs;
 for (int i = 0; i = b[j].bullet_x && a[i].alien_y+40 >= b[j].bullet_y)
 {
 bulbul.load("music\\bullet.mp3");
 bulbul.play();
 as.push_back(i);
 bs.push_back(j);
 score++;
 }
 }
 }
 if (!as.empty())
 for (int i = a.size() - 1; i >= 0; i--) //删除碰撞后的外星人
 {
 int k = as.size() - 1;
 if (i == as[k])
 {
 a.erase(a.begin() + i);
 k--;
 add_alien(a); //每次删除都新增一个外星人
 }
 }
 if (!bs.empty())
 for (int i = b.size() - 1; i >= 0; i--)  //删除碰撞后的子弹
 {
 int k = bs.size() - 1;
 if (i == bs[k])
 {
 b.erase(b.begin() + i);
 k--;
 }
 }
 if(!a.empty())   //删除飞出屏幕的外星人
 if (a[0].alien_y > 720)
 {
 a.pop_front();
 add_alien(a); //每次删除都新增一个外星人
 }
 if (!b.empty())   //删除飞出屏幕的子弹
 if (b[0].bullet_y <-20)
 b.pop_front();
 for (int i = 0; i  0 && b[i].bullet_x <1350 && b[i].bullet_y > -29 && b[i].bullet_y <730)
 b[i].show_bullet();
 b[i].bullet_y -= b[i].bullet_speed;
 }
}


bool launch(int a) //游戏运行
{
 Airplane plane;
 Bullet bul;
 Alien ali;
 if (a == 1)
 {
 alien_speed = 3;
 alien_num = 5;
 plane.airplane_speed = 10;
 level13.load("music\\level01.mp3");
 level13.play();
 }
 if (a == 2)
 {
 alien_speed = 6;
 alien_num = 7;
 plane.airplane_speed = 20;
 level46.load("music\\level03.mp3");
 level46.play();
 }
 deque aliens;
 deque bullets;
 ali.alien_x = rand() % 1320;
 aliens.push_back(ali);  //放入头,便于后续操作
 
 for (int i = 0; i = 2)
 {

 choose.load("music\\choose.mp3");
 choose.play();
 i--;
 home(i);
 }
 return 0;
 break;
 case 80:
 if (i <= 3)
 {

 choose.load("music\\choose.mp3");
 choose.play();
 i++;
 home(i);
 }
 return 0;
 break;
 case 13:
 choose.play();
 switch (i)
 {
 case 1:
 mu_be01.stop(); launch(1); return 0;
 break;
 case 2:
 mu_be01.stop(); launch(2); return 0;
 break;
 case 3:
 help(); return 0;
 break;
 case 4:
 if (exit())
 return 1;
 break;
 }
 break;
 default:
 return 0;
 break;
 }
}
/*main.cpp*/
#include 
#include 
#include 
#include
#include
#include "launch.h"
#include "setting.h"
#pragma comment(lib,"winmm.lib")

using namespace std;

int main()
{
 int i = 1;
 char be_key; //初始界面按键输入
 load_music01();
 initgraph(1360, 760);
 home(1);
 while (1)
 {
 home(i);
 be_key = _getch();
 if (choose_all(be_key, i))
 return 0;
 }

 closegraph();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • Vue 2 中解决页面刷新和按钮跳转导致导航栏样式失效的问题
    本文介绍了如何通过配置路由的 meta 字段,确保 Vue 2 项目中的导航栏在页面刷新或内部按钮跳转时,始终保持正确的 active 样式。具体实现方法包括设置路由的 meta 属性,并在 HTML 模板中动态绑定类名。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 在 Windows 10 中,F1 至 F12 键默认设置为快捷功能键。本文将介绍几种有效方法来禁用这些快捷键,并恢复其标准功能键的作用。请注意,部分笔记本电脑的快捷键可能无法完全关闭。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
author-avatar
啊123
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有