C++ 坦克大战小游戏EGE图形界面

C++ EGE 实现坦克大战小游戏

因为有过一次用EGE写小游戏的经验,所以这一次写坦克大战快了很多。并且使用对象编程也简化了很多编程时繁琐的步骤。
写出坦克大战使我在学习编程的道路上又迈出了一大步。

如果您需要图片素材的,我可以单独发给您。

技术环节:
编译环境:Windows VS2019

需求:
控制坦克移动发射炮弹,炮弹可以消灭敌军坦克,且可以消灭砖块。坦克遇到方块会被挡住。敌军消灭我军三次或基地被毁则游戏失败,共摧毁十次敌方坦克游戏胜利。

思路:
先写出坦克的父类,我方坦克类和敌方坦克类继承坦克父类,实例化我方坦克和敌方坦克。地图使用list容器存储。

在代码注释中标注了每一步是怎么实现的。

注意:
因为我在程序中用了一些不规范的写法,所以要在VS中正常编译运行,需要右键源文件->属性->C/C+±>符合模式,改为否。

包含<graphics.h>图形库需要提前配置EGE图形库。
如要在其他graphics图形库下编译,可能需要修改某些地方。

运行效果:
1
2
3

#include <graphics.h>	//图形库
#include <ctime>		//time();
#include <list>			//list容器

using namespace std;	//标准命名空间 list等

//设置图片对象中图片的宽高  全局函数
//参数:宽、高、对象名
void setimage(int pwidth, int pheight, PIMAGE img_1);

//地图全局结构
struct mapstr
{
   
	int m_x;	//xy坐标
	int m_y;
	int prop;	//属性
};

//地图类
class Mymap
{
   
private:
	list<mapstr> listmap;		//地图容器,存储全地图信息,全地图1350个20*20的格子

public:
	//设置地图
	Mymap()
	{
   
		mapstr temp;

		//构造函数为链表容器中的地图赋值

		//全地图横向45个格子 竖向30个格子

		//基地部分
		{
   
			temp.prop = 0;
			for (int i = 0; i < 4; i++) {
    temp.m_x = 380 + i * 20; temp.m_y = 540; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 380 + i * 20; temp.m_y = 520; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 380; temp.m_y = 560 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 360; temp.m_y = 520 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 440; temp.m_y = 560 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 460; temp.m_y = 520 + i * 20; listmap.push_back(temp); }
			temp.prop = 4, temp.m_x = 400, temp.m_y = 560;	listmap.push_back(temp);
		}

		//左上角部分
		{
   
			temp.prop = 0;
			//左上角单独砖块
			for (int i = 0; i < 2; i++) {
    temp.m_x = 40 + i * 20; temp.m_y = 80; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 40 + i * 20; temp.m_y = 100; listmap.push_back(temp); }
			//竖铁块
			for (int i = 0; i < 4; i++) {
    temp.m_x = 160; temp.m_y = i * 20; temp.prop = 1; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 180; temp.m_y = i * 20; listmap.push_back(temp); }
			//砖块
			for (int i = 0; i < 4; i++) {
    temp.m_x = 160; temp.m_y = 160 + i * 20; temp.prop = 0; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 180; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			//草块
			for (int i = 0; i < 4; i++) {
    temp.m_x = 0; temp.m_y = 200 + i * 20; temp.prop = 2; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 20; temp.m_y = 200 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 40 + i * 20; temp.m_y = 240; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 40 + i * 20; temp.m_y = 260; listmap.push_back(temp); }
		}

		//中上部分
		{
   
			//铁块
			for (int i = 0; i < 2; i++) {
    temp.m_x = 320; temp.m_y = i * 20; temp.prop = 1; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 340; temp.m_y = i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 280 + i * 20; temp.m_y = 160; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 280 + i * 20; temp.m_y = 180; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 400 + i * 20; temp.m_y = 200; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 400 + i * 20; temp.m_y = 220; listmap.push_back(temp); }
			//砖块
			for (int i = 0; i < 4; i++) {
    temp.m_x = 320; temp.m_y = 40 + i * 20; temp.prop = 0; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 340; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 240; temp.m_y = 200 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 260; temp.m_y = 200 + i * 20; listmap.push_back(temp); }
		}

		//右上部分
		{
   
			//砖块
			for (int i = 0; i < 4; i++) {
    temp.m_x = 480; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 500; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 480; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 500; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 600; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 620; temp.m_y = 40 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 600; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 2; i++) {
    temp.m_x = 620; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 6; i++) {
    temp.m_x = 680 + i * 20; temp.m_y = 200; listmap.push_back(temp); }
			for (int i = 0; i < 6; i++) {
    temp.m_x = 680 + i * 20; temp.m_y = 220; listmap.push_back(temp); }
			for (int i = 0; i < 6; i++) {
    temp.m_x = 760; temp.m_y = 0 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 6; i++) {
    temp.m_x = 780; temp.m_y = 0 + i * 20; listmap.push_back(temp); }
			//草块
			for (int i = 0; i < 6; i++) {
    temp.m_x = 560; temp.m_y = 160 + i * 20; temp.prop = 2; listmap.push_back(temp); }
			for (int i = 0; i < 6; i++) {
    temp.m_x = 580; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 520; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 540; temp.m_y = 160 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 4; i++) {
    temp.m_x = 860; temp.m_y = 80 + i * 20; listmap.push_back(temp); }
			for (int i = 0; i < 
  • 9
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值