当前位置:网站首页>最新坦克大战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
边栏推荐
- Adb常用命令列表
- Youxuan database failed to start and reported network error
- [wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
- Google GTEST event mechanism
- P1009 [noip1998 popularization group] sum of factorials
- How to add traffic statistics codes to the legendary Development Zone website
- 向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
- Sparksql inserts or updates in batches and saves data to MySQL
- 网安学习-内网安全1
- Modification of annotation based three-tier project and the way of adding package scanning
猜你喜欢

Soft link & hard link

学习数据库的第一个程序

7.2-function-overloading

JS (foreach) return cannot end the function solution

How to add a map to the legendary server

What if the computer cannot open excel? The solution of Excel not opening

The method and detailed code of automatically pop-up and QQ group when players visit the website

Google GTEST event mechanism

Introduction of JDBC preparestatement+ database connection pool

Sparksql inserts or updates in batches and saves data to MySQL
随机推荐
【[第一次写博客]Uda课程中的P控制器实现说明】
Learn the first program of database
玩家访问网站自动弹窗加QQ群方法以及详细代码
【2022新生学习】第三周要点
Activity workflow table structure learning
Mapper agent development
Soft link & hard link
How to add traffic statistics codes to the legendary Development Zone website
优炫数据库启动失败,报网络错误
"Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
Network Security Learning - Intranet Security 1
ThreadPoolExecutor simple to use
开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开
【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】
On AspectJ framework
QML定制TabBar
7.1-default-arguments
开区网站打开自动播放音乐的添加跟修改教程
The method and detailed code of automatically pop-up and QQ group when players visit the website
Open the tutorial of adding and modifying automatically playing music on the open zone website