当前位置:网站首页>The latest tank battle 2022 - Notes on the whole development -2
The latest tank battle 2022 - Notes on the whole development -2
2022-07-29 05:20:00 【CPP programming】
6、 ... and 、 Create tanks
6.1 Define tank data types
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: own false: The enemy
IMAGE* imgBody;
IMAGE* imgSafe;
int x, y; // Column and row numbers in the map
int sn; // Number
bool used; // Whether to use
direct_t diret; // Direction
} tank_t;6.2 Define tank texture
enum {
TANK_MINE,
TANK_ENEMY_1,
TANK_TYPE_COUNT
};
IMAGE imgUnits[UNIT_COUNT];
IMAGE imgTanks[TANK_TYPE_COUNT][DIRECT_COUNT];6.3 Define tank variables
#define ENEMY_TANK_MAX 10
#define MY_TANK_MAX 3
tank_t myTank;
tank_t enemyTanks[ENEMY_TANK_MAX];
int myTankCount; // The number of our tanks 6.3 Load the texture of the tank
Define the image texture array of the tank .
enum {
TANK_MINE,
TANK_ENEMY_1,
TANK_TYPE_COUNT
};
IMAGE imgTanks[TANK_TYPE_COUNT][DIRECT_COUNT];stay init In the initialization function , Load the image texture of the enemy and our tanks in all directions .
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 Create our tanks
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];
}
}stay main Call in function createMyTank().
6.5 Create enemy tanks
int enemyCurCount; // The number of enemy tanks currently fighting
int enemyTankCountCanUsed; // Total number of enemy tanks available ( What has appeared and what is ready to appear later )
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; // stay init Function to add srand Configure random seeds
enemyTanks[index].y = 0;
enemyTanks[index].imgBody = imgTanks[TANK_ENEMY_1];
}
}stay main Call in function createEnemyTank().
6.6 Initialize the number of tanks
stay init Function to initialize the number of tanks .
myTankCount = MY_TANK_MAX;
enemyTankCountCanUsed = ENEMY_TANK_MAX;
enemyCurCount = 0;7. Render all tanks
Definition 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]);
}
}
}stay main Call in function updateAllTanks()
int main(void) {
init();
createMyTank();
createEnemyTank();
updataMap();
updateAllTanks();
system("pause");
return 0;
}Perform project , Check the running effect of the game :

Next section , We optimize the code , Design the main frame of the game .
Today's sharing is here , Everyone should study hard C Language /C++ yo ~
For preparing to learn C/C++ Programming partners , If you want to better improve your core programming skills ( Internal skill ) From now on !
C Language C++ Programming learning communication circle ,QQ Group :763855696【 Click to enter 】
C Language from entry to mastery (C Introduction to language C Language course C Language zero basis C Language foundation C Language learning C
Organize and share ( Years of learning source code 、 Project practice video 、 Project notes , Introduction to Basics )
Welcome to change careers and learn programming partners , Using more information to learn and grow faster than thinking about it yourself !

边栏推荐
- 2021-10-23
- 手写学生管理系统
- Qml控件:ComboBox
- Rimworld通过SteamCMD上传创意工坊的方法
- Jackson parsing JSON detailed tutorial
- What if the computer cannot open excel? The solution of Excel not opening
- JS daily question (11)
- Getting started with arfoundation tutorial 10- plane detection and placement
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
- 开源汇智创未来 | 2022开放原子全球开源峰会 openEuler 分论坛圆满召开
猜你喜欢

How to add a map to the legendary server

SM整合原来这么简单,步骤清晰(详细)

The method and detailed code of automatically pop-up and QQ group when players visit the website
![[untitled]](/img/04/242e85ee8eea5bd6ae8144fc048241.png)
[untitled]

后置通知的流程分析与功能实现有哪些内容你还记得吗?

WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!

Sparksql inserts or updates in batches and saves data to MySQL

Self join and joint query of MySQL

ARFoundation从零开始5-AR图像跟踪

Soft link & hard link
随机推荐
P5714 [deep foundation 3. Case 7] obesity
传奇服务端如何添加地图
网安学习-内网安全1
【config】配置数组参数
自定义Qml控件:ImageButton
ThreadPoolExecutor simple to use
What servers are needed to build mobile app
【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】
一文带你搞懂环绕通知@Around与最终通知@After的实现
Huawei ilearning AI mathematics foundation course notes
js(forEach)出现return无法结束函数的解决方法
基于注解的三层项目的改造及添加包扫描的方式
QT学习:QDropEvent拖拽事件
Network Security Learning - Intranet Security 1
Self join and joint query of MySQL
[2022 freshmen learning] key points of the third week
VirtualBox has expanded the capacity of virtual hard disk (without modifying the original data)
Understand activity workflow
【文件下载】Easyexcel快速上手
时间序列分析的表示学习时代来了?