当前位置:网站首页>最新坦克大战2022-全程开发笔记-1
最新坦克大战2022-全程开发笔记-1
2022-07-29 05:08:00 【cpp编程】
一、项目介绍
坦克大战1990作为一个经典的单机游戏,是80后、90后的美好回忆,也是C/C++初学者的必备项目。
不过坦克1990,游戏画面太过朴素。我们现在对坦克1990进行全新升级,使用C语言, 打造最新款的坦克2022!游戏效果如下:
本项目的配套视频地址
二、项目目的
整个项目,完全使用C语言,从零开始,从游戏框架设计到游戏渲染,融合了C语言的主要技术要点,对于C语言初学者,有很大的帮助作用。可以作为初学者的快速提升项目,也可以直接作为大学生的课设。
三、项目准备
1.Windows系统,苹果电脑不合适哦。
2.已掌握C语言的简单基础,比如常量、变量、if-for-while控制语句。
3.安装VS任意版本,建议使用VS2019或VS2022.
4.安装easyx图形库
直接在官网下载最新版本的easyx图形库
下载后,直接双击安装即可安装。
5.下载“坦克大战2020”的游戏素材和音效文件。
直接给我发私信即可获取。
四、创建项目
使用VS2019,或者其它版本的VS, 创建新项目,选择空项目模板。
五、实现坦克大战的战场布局
5.1 导入游戏资源
把资源目录res导入项目目录。
5.2 实现游戏背景
添加 main.cpp
#include <stdio.h>
#include <graphics.h>
IMAGE imgBG;
void init() {
initgraph(1300, 900);
loadimage(&imgBG, "res/bg1.png");
}
void updataMap() {
putimage(0, 0, &imgBG); //更新游戏背景
}
int main(void) {
init();
updataMap();
system("pause");
return 0;
}
5.3 实现游戏地图
先添加最基本的地图元素,后续再添加其他元素,比如丛林覆盖物、河流、护盾等各种道具。
定义枚举类型,以表示各种地图元素。
enum {
EMPTY, //空地
TU_WALL, //土墙
GANG_WALL, //钢墙
MY_HOME, //我方指挥官
ENEMY_HOME, //对方指挥官
UNIT_COUNT
};
定义图片数组imgUnits来表示各个地图元素
IMAGE imgUnits[UNIT_COUNT];
在init初始化函数中,加载地图元素。
imgUnits[EMPTY] = NULL;
loadimage(&imgUnits[TU_WALL], "res/wall1.png");
loadimage(&imgUnits[GANG_WALL], "res/wall2.png");
loadimage(&imgUnits[MY_HOME], "res/pj2.png");
loadimage(&imgUnits[ENEMY_HOME], "res/wukelan2.png");
使用二维数组表示地图布局,后续再优化为使用多个文件来表示各个关卡的地图数据。
int map[18][26] = {
{ 0,0,1,1,0,0,1,1,0,0,0,1,4,0,1,0,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,0,1,0,0,1,0,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
{ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1 },
{ 2,2,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,2,2 },
{ 0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,1,3,0,1,0,0,0,0,0,0,0,0,0,0,0 },
{ 0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0 }
};
根据二维数组地图数据,在updataMap函数中更新地图数据。
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 26; j++) {
putimage(j * 50, i * 50, &imgUnits[map[i][j]]);
}
}
运行程序,查看游戏地图效果:
5.4 实现指挥所的透明背景导入自定义的tools.h和tools.cpp
1)导入自定义的tools.h和tools.cpp
2)在main.cpp中添加
#include "tools.h"
3)修改代码
//putimage(j * 50, i * 50, &imgUnits[map[i][j]]);
putimagePNG(j * 50, i * 50, &imgUnits[map[i][j]]);
测试效果如下:
下一步,我们将创建敌我双方的坦克。
今天的分享就到这里了,大家要好好学C语言/C++哟~
对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
C语言C++编程学习交流圈子,QQ群:763855696
【点击进入】
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!
C语言从入门到精通(C语言入门C语言教程C语言零基础C语言基础C语言学习C
边栏推荐
- How to add a map to the legendary server
- [wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
- Diagram of odoo development tutorial
- Qml类型:MouseArea
- Quick start JDBC
- WDDM学习
- [untitled]
- Mapper agent development
- QT系列---安装
- Mysql语句中的函数
猜你喜欢
Mapper agent development
How to solve the problem of configuring the progress every time Office2010 is opened?
Northeast University Data Science Foundation (matlab) - Notes
Sparksql inserts or updates in batches and saves data to MySQL
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
Deep learning brush a bunch of tricks of SOTA
2021-10-23
那个准时上下班,从不愿意加班加点的人,在我前面升职了...
scikit-learn——机器学习应用开发的步骤和理解
关于servlet中实现网站的页面跳转
随机推荐
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
2021-10-23
QT系列---安装
三层项目的架构分析及构造方法的参数名称注入
What if the computer cannot open excel? The solution of Excel not opening
6.2 function-parameters
Big silent event Google browser has no title
JS (in ES6) sync & await understanding
Qml类型:State 状态
How to install Office2010 installation package? How to install Office2010 installation package on computer
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
Arfoundation starts from scratch 3- create an arfoundation project
[sudden] solve remote: support for password authentication was removed on August 13, 2021. please use a perso
What if the office prompts that the system configuration cannot run?
QT学习:QDropEvent拖拽事件
P2181 diagonal
Jackson解析JSON详细教程
On AspectJ framework
Vivo market API event reporting and docking
Introduction of JDBC preparestatement+ database connection pool