当前位置:网站首页>最新坦克大战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]]);
测试效果如下:
下一步,我们将创建敌我双方的坦克。
边栏推荐
- 西安电子科技大学22学年上学期《射频电路基础》试题及答案
- Music playback (toggle & playerprefs)
- 记录:动态Web项目servlet访问数据库404错误之解决
- TYUT太原理工大学2022软工导论考试题型大纲
- Application architecture of large live broadcast platform
- Record: I accidentally wrote a recursion next time
- 阿里云微服务(三)Sentinel开源流控熔断降级组件
- 《软件测试》习题答案:第一章
- View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
- One article to get UDP and TCP high-frequency interview questions!
猜你喜欢
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
Rt-ppp test using rtknavi
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
IPv6 experiment
Interview Essentials: talk about the various implementations of distributed locks!
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
国企秋招经验总结
Alibaba cloud microservices (II) distributed service configuration center and Nacos usage scenarios and implementation introduction
System design learning (I) design pastebin com (or Bit.ly)
Heap sort [handwritten small root heap]
随机推荐
Sharing ideas of on-chip transplantation based on rtklib source code
Record: Navicat premium can't connect to MySQL for the first time
错误: 找不到符号
Record: solution of 404 error of servlet accessing database in dynamic web project
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
初识指针笔记
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
First acquaintance with C language (Part 2)
图书管理系统小练习
Data manipulation language (DML)
Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component
TYUT太原理工大学2022数据库大题之概念模型设计
RTKLIB: demo5 b34f. 1 vs b33
Interview Essentials: talk about the various implementations of distributed locks!
Relational algebra of tyut Taiyuan University of technology 2022 database
记录:newInstance()过时的代替方法
Application architecture of large live broadcast platform
4.30 dynamic memory allocation notes
Introduction and use of redis
How to ensure data consistency between MySQL and redis?