当前位置:网站首页>最新坦克大战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]]);
测试效果如下:
下一步,我们将创建敌我双方的坦克。
边栏推荐
- TYUT太原理工大学2022数据库大题之分解关系模式
- XV Function definition and call
- Atomic and nonatomic
- Code example of MATLAB reading GNSS observation value o file
- Redis介绍与使用
- TYUT太原理工大学2022数据库大题之数据库操作
- 一文搞定 UDP 和 TCP 高频面试题!
- Tyut Taiyuan University of technology 2022 introduction to software engineering summary
- 凡人修仙学指针-2
- Record: newinstance() obsolete replacement method
猜你喜欢
Inheritance and polymorphism (Part 2)
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
arduino+水位传感器+led显示+蜂鸣器报警
MySQL Database Constraints
121 distributed interview questions and answers
Novatel board oem617d configuration step record
Detailed explanation of balanced binary tree is easy to understand
阿里云微服务(三)Sentinel开源流控熔断降级组件
架构师怎样绘制系统架构蓝图?
TYUT太原理工大学2022数据库大题之概念模型设计
随机推荐
165. Compare version number - string
Pride-pppar source code analysis
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
Design a key value cache to save the results of the most recent Web server queries
TYUT太原理工大学2022软工导论考试题型大纲
如何保障 MySQL 和 Redis 的数据一致性?
Introduction pointer notes
Record: newinstance() obsolete replacement method
Inheritance and polymorphism (Part 2)
学编程的八大电脑操作,总有一款你不会
Database operation of tyut Taiyuan University of technology 2022 database
Application architecture of large live broadcast platform
Record: I accidentally wrote a recursion next time
Answer to "software testing" exercise: Chapter 1
MySQL Database Constraints
TYUT太原理工大学2022数据库大题之数据库操作
121道分布式面试题和答案
First acquaintance with C language (Part 1)
Interview Essentials: talk about the various implementations of distributed locks!
All in one 1405: sum and product of prime numbers