函数设计 要求:对创建的void createEnemy()函数贴出对应代码和注释,代码为:void createEnemy() { t4 = GetTickCount();//获取当前系统的运行时间 if (t4 - t3 > 2000) { for (int i = 0; i < ENEMY_NUM; i++) { if (!enemy[i].flag)//若敌机未摧毁 { enemy[i].flag = true; int result = rand() % 10; if (result == 0) { enemy[i].Myunion.type = BIG; enemy[i].hp = BIG_HP;//大型敌机 enemy[i].x = rand() % (WIN_WIDTH - 100); enemy[i].y = -153; } else if (result == 1 || result == 2) { enemy[i].Myunion.type = MEDIUM; enemy[i].hp = MEDIUM_HP;//中型敌机 enemy[i].x = rand() % (WIN_WIDTH - 69); enemy[i].y = -99; } else { enemy[i].Myunion.type = SAMLL; enemy[i].hp = SMALL_HP;//小型敌机 enemy[i].x = rand() % (WIN_WIDTH - 57); enemy[i].y = -43; } break; } } t3 = t4; } }
时间: 2024-02-29 20:52:50 浏览: 57
这是一个创建敌机的函数,主要功能是在游戏画面中生成敌机。以下是对代码的注释:
```c++
void createEnemy() {
t4 = GetTickCount(); // 获取当前系统的运行时间
if (t4 - t3 > 2000) { // 如果时间超过2秒
for (int i = 0; i < ENEMY_NUM; i++) { // 循环ENEMY_NUM次,生成敌机
if (!enemy[i].flag) { // 若敌机未摧毁
enemy[i].flag = true; // 将敌机标记为已生成
int result = rand() % 10; // 生成随机数
if (result == 0) { // 若随机数为0,生成大型敌机
enemy[i].Myunion.type = BIG;
enemy[i].hp = BIG_HP;
enemy[i].x = rand() % (WIN_WIDTH - 100);
enemy[i].y = -153;
} else if (result == 1 || result == 2) { // 若随机数为1或2,生成中型敌机
enemy[i].Myunion.type = MEDIUM;
enemy[i].hp = MEDIUM_HP;
enemy[i].x = rand() % (WIN_WIDTH - 69);
enemy[i].y = -99;
} else { // 若随机数为其他,生成小型敌机
enemy[i].Myunion.type = SAMLL;
enemy[i].hp = SMALL_HP;
enemy[i].x = rand() % (WIN_WIDTH - 57);
enemy[i].y = -43;
}
break; // 跳出循环,生成一个敌机即可
}
}
t3 = t4; // 将上次生成敌机的时间更新为当前时间
}
}
```
阅读全文