当前位置:网站首页>最新坦克大战2022-全程开发笔记-2
最新坦克大战2022-全程开发笔记-2
2022-07-06 09:19:00 【程序员Rock】
六、创建坦克
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;
}
执行项目,检查游戏运行效果:
下一节,我们对代码进行优化,设计好游戏的主体框架。
边栏推荐
- Database operation of tyut Taiyuan University of technology 2022 database
- Detailed explanation of balanced binary tree is easy to understand
- 错误:排序与角标越界
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- [while your roommate plays games, let's see a problem]
- [Topic terminator]
- 凡人修仙学指针-2
- 用栈实现队列
- Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
- Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
猜你喜欢
121道分布式面试题和答案
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
IPv6 experiment
抽象类和接口
XV Function definition and call
System design learning (I) design pastebin com (or Bit.ly)
12 excel charts and arrays
Detailed explanation of balanced binary tree is easy to understand
Answer to "software testing" exercise: Chapter 1
阿里云一面:并发场景下的底层细节 - 伪共享问题
随机推荐
165. Compare version number - string
系统设计学习(一)Design Pastebin.com (or Bit.ly)
记录:newInstance()过时的代替方法
Introduction pointer notes
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
arduino+水位传感器+led显示+蜂鸣器报警
WSL common commands
MySQL Database Constraints
Inheritance and polymorphism (I)
Alibaba cloud microservices (IV) service mesh overview and instance istio
First acquaintance with C language (Part 1)
View UI Plus 发布 1.1.0 版本,支持 SSR、支持 Nuxt、增加 TS 声明文件
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
Employment of cashier [differential constraint]
ROS machine voice
System design learning (I) design pastebin com (or Bit.ly)
Application architecture of large live broadcast platform
阿里云微服务(四) Service Mesh综述以及实例Istio
Redis介绍与使用