当前位置:网站首页>最新坦克大战2022-全程开发笔记-1
最新坦克大战2022-全程开发笔记-1
2022-07-06 09:19:00 【程序员Rock】
一、项目介绍
坦克大战1990作为一个经典的单机游戏,是80后、90后的美好回忆,也是C/C++初学者的必备项目。
不过坦克1990,游戏画面太过朴素。我们现在对坦克1990进行全新升级,使用C语言, 打造最新款的坦克2022!游戏效果如下:
二、项目目的
整个项目,完全使用C语言,从零开始,从游戏框架设计到游戏渲染,融合了C语言的主要技术要点,对于C语言初学者,有很大的帮助作用。可以作为初学者的快速提升项目,也可以直接作为大学生的课设。
三、项目准备
1.Windows系统,苹果电脑不合适哦。
2.已掌握C语言的简单基础,比如常量、变量、if-for-while控制语句。
3.安装VS任意版本,建议使用VS2019或VS2022.
VS2019安装指导
4.安装easyx图形库
直接在官网下载最新版本的easyx图形库
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]]);测试效果如下:

下一步,我们将创建敌我双方的坦克。
边栏推荐
- 继承和多态(下)
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- 2022 National Games RE1 baby_ tree
- Edit distance (multi-source BFS)
- KF UD decomposition pseudo code implementation advanced [2]
- 4.30动态内存分配笔记
- XV Function definition and call
- Introduction pointer notes
- How to ensure data consistency between MySQL and redis?
- Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
猜你喜欢

TYUT太原理工大学2022数据库大题之概念模型设计

121道分布式面试题和答案

Inheritance and polymorphism (I)

Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche

Database operation of tyut Taiyuan University of technology 2022 database

Abstract classes and interfaces

TYUT太原理工大学2022数据库大题之数据库操作

Smart classroom solution and mobile teaching concept description

View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件

Detailed explanation of balanced binary tree is easy to understand
随机推荐
Redis介绍与使用
Quickly generate illustrations
How to ensure data consistency between MySQL and redis?
Fairygui bar subfamily (scroll bar, slider, progress bar)
六种集合的遍历方式总结(List Set Map Queue Deque Stack)
2022 National Games RE1 baby_ tree
Relational algebra of tyut Taiyuan University of technology 2022 database
System design learning (I) design pastebin com (or Bit.ly)
[GNSS] robust estimation (robust estimation) principle and program implementation
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
架构师怎样绘制系统架构蓝图?
Dark chain lock (lca+ difference on tree)
What are the advantages of using SQL in Excel VBA
167. Sum of two numbers II - input ordered array - Double pointers
FileInputStream和BufferedInputStream的比较
《软件测试》习题答案:第一章
Data manipulation language (DML)
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
IPv6 experiment
Heap sort [handwritten small root heap]