当前位置:网站首页>最新坦克大战2022-全程开发笔记-2
最新坦克大战2022-全程开发笔记-2
2022-07-29 05:08:00 【cpp编程】
六、创建坦克
6.1 定义坦克数据类型
typedef enum Direction {
DIRRECT_START,
DIRECT_UP = DIRRECT_START,
DIRECT_RIGHT,
DIRECT_DOWN,
DIRECT_LEFT,
DIRECT_COUNT
} direct_t;
typedef struct tank {
bool heroFlag; // true:自己 false: 敌人
IMAGE* imgBody;
IMAGE* imgSafe;
int x, y; // 在地图中的列号和行号
int sn; // 编号
bool used; // 是否使用
direct_t diret; //方向
} tank_t;6.2 定义坦克纹理
enum {
TANK_MINE,
TANK_ENEMY_1,
TANK_TYPE_COUNT
};
IMAGE imgUnits[UNIT_COUNT];
IMAGE imgTanks[TANK_TYPE_COUNT][DIRECT_COUNT];6.3 定义坦克变量
#define ENEMY_TANK_MAX 10
#define MY_TANK_MAX 3
tank_t myTank;
tank_t enemyTanks[ENEMY_TANK_MAX];
int myTankCount; //我方坦克数量6.3 加载坦克的纹理
定义坦克的图片纹理数组。
enum {
TANK_MINE,
TANK_ENEMY_1,
TANK_TYPE_COUNT
};
IMAGE imgTanks[TANK_TYPE_COUNT][DIRECT_COUNT];在init初始化函数中,加载敌我坦克各个方向的图片纹理。
loadimage(&imgTanks[TANK_MINE][DIRECT_UP], "res/tankUp.png");
loadimage(&imgTanks[TANK_MINE][DIRECT_RIGHT], "res/tankRight.png");
loadimage(&imgTanks[TANK_MINE][DIRECT_DOWN], "res/tankDown.png");
loadimage(&imgTanks[TANK_MINE][DIRECT_LEFT], "res/tankLeft.png");
loadimage(&imgTanks[TANK_ENEMY_1][DIRECT_UP], "res/tankEnemyUp.png");
loadimage(&imgTanks[TANK_ENEMY_1][DIRECT_RIGHT], "res/tankEnemyRight.png");
loadimage(&imgTanks[TANK_ENEMY_1][DIRECT_DOWN], "res/tankEnemyDown.png");
loadimage(&imgTanks[TANK_ENEMY_1][DIRECT_LEFT], "res/tankEnemyLeft.png");6.4 创建我方坦克
void createMyTank() {
if (myTank.used) return;
if (myTankCount > 0) {
myTankCount--;
myTank.used = true;
myTank.heroFlag = true;
myTank.diret = DIRECT_UP;
myTank.x = 9;
myTank.y = 16;
myTank.imgBody = imgTanks[TANK_MINE];
}
}在main函数中调用 createMyTank().
6.5 创建敌方坦克
int enemyCurCount; //当前正在战斗的敌方坦克数量
int enemyTankCountCanUsed; //敌方可用坦克总数(已经出现的和后续准备出现的)
void createEnemyTank() {
if (enemyCurCount < 4 && enemyTankCountCanUsed > 0) {
int index;
for (index = 0; index < ENEMY_TANK_MAX && enemyTanks[index].used; index++);
if (index >= ENEMY_TANK_MAX) return;
enemyCurCount++;
enemyTanks[index].heroFlag = false;
enemyTanks[index].used = true;
enemyTanks[index].direct = DIRECT_DOWN;
enemyTanks[index].x = rand() % 2 ? 9 : 15; //在init函数中添加srand配置随机种子
enemyTanks[index].y = 0;
enemyTanks[index].imgBody = imgTanks[TANK_ENEMY_1];
}
}在main函数中调用 createEnemyTank().
6.6 对坦克数量进行初始化
在init函数中对坦克数量进行初始化。
myTankCount = MY_TANK_MAX;
enemyTankCountCanUsed = ENEMY_TANK_MAX;
enemyCurCount = 0;7. 渲染所有坦克
定义updateAllTanks()
void updateTank(tank_t* tank) {
if (!tank->used) return;
putimagePNG(tank->x * 50 + 5, tank->y * 50 + 5, &tank->imgBody[tank->direct]);
}
void updateAllTanks() {
updateTank(&myTank);
for (int i = 0; i < ENEMY_TANK_MAX; i++) {
if (enemyTanks[i].used) {
updateTank(&enemyTanks[i]);
}
}
}在main函数中调用updateAllTanks()
int main(void) {
init();
createMyTank();
createEnemyTank();
updataMap();
updateAllTanks();
system("pause");
return 0;
}执行项目,检查游戏运行效果:

下一节,我们对代码进行优化,设计好游戏的主体框架。
今天的分享就到这里了,大家要好好学C语言/C++哟~
对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!
C语言C++编程学习交流圈子,QQ群:763855696【点击进入】
C语言从入门到精通(C语言入门C语言教程C语言零基础C语言基础C语言学习C
整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)
欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

边栏推荐
- TCP three handshakes and four waves
- 2021-11-02
- 7.3-function-templates
- Vivo market API event reporting and docking
- 2021-10-11
- How does excel filter out the content you want? Excel table filtering content tutorial
- 学习数据库的第一个程序
- Solution | get the relevant information about the current employees' highest salary in each department |
- Scikit learn -- steps and understanding of machine learning application development
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
猜你喜欢

Jackson parsing JSON detailed tutorial

ODOO开发教程之图表

NumPy基础

How to add traffic statistics codes to the legendary Development Zone website

使用Jstack、Jconsole和jvisualvm进行死锁分析

6.3 references

How to add a map to the legendary server

Arfoundation starts from scratch 8-geospatial API (geospatial) development

Sparksql inserts or updates in batches and saves data to MySQL

ARFoundation从零开始3-创建ARFoundation项目
随机推荐
How does word view document modification traces? How word views document modification traces
[from_bilibili_DR_CAN][【Advanced控制理论】9_状态观测器设计][学习记录]
【config】配置数组参数
Diagram of odoo development tutorial
This article takes you to understand the implementation of surround notification @around and final notification @after
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Sparksql inserts or updates in batches and saves data to MySQL
AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
Pytorch learning notes
Deadlock analysis using jstack, jconsole, and jvisualvm
Getting started with arfoundation tutorial 10- plane detection and placement
About realizing page Jump of website in Servlet
How to add traffic statistics codes to the legendary Development Zone website
Quick start JDBC
NumPy基础
Solve the warning prompt of MySQL mapping file
Let you understand several common traffic exposure schemes in kubernetes cluster
P1009 [noip1998 popularization group] sum of factorials
ThreadPoolExecutor simple to use
ARFoundation入门教程10-平面检测和放置