当前位置:网站首页>The latest tank battle 2022 - Notes on the whole development -2
The latest tank battle 2022 - Notes on the whole development -2
2022-07-06 13:27:00 【Programmer rock】
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 .
边栏推荐
- Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
- Arduino+ water level sensor +led display + buzzer alarm
- 【毕业季·进击的技术er】再见了,我的学生时代
- 西安电子科技大学22学年上学期《信号与系统》试题及答案
- 20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
- Quickly generate illustrations
- TYUT太原理工大学2022数据库大题之概念模型设计
- 4.二分查找
- Aurora system model of learning database
- Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
猜你喜欢
IPv6 experiment
Pit avoidance Guide: Thirteen characteristics of garbage NFT project
Experience summary of autumn recruitment of state-owned enterprises
2. C language matrix multiplication
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Arduino+ water level sensor +led display + buzzer alarm
System design learning (III) design Amazon's sales rank by category feature
(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow
Interview Essentials: talk about the various implementations of distributed locks!
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
随机推荐
Inheritance and polymorphism (Part 2)
魏牌:产品叫好声一片,但为何销量还是受挫
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
vector
最新坦克大战2022-全程开发笔记-3
Redis介绍与使用
View UI plus released version 1.2.0 and added image, skeleton and typography components
There is always one of the eight computer operations that you can't learn programming
System design learning (III) design Amazon's sales rank by category feature
Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
[while your roommate plays games, let's see a problem]
Introduction and use of redis
arduino+水位传感器+led显示+蜂鸣器报警
Common method signatures and meanings of Iterable, collection and list
最新坦克大战2022-全程开发笔记-1
西安电子科技大学22学年上学期《信号与系统》试题及答案
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Change vs theme and set background picture
TYUT太原理工大学2022数据库大题之概念模型设计
string