9.僵尸位置的改变和吃植物的判定
位置检测
首先检测僵尸的位置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| void changeZombie() { if (zb[zombielist->gx - 1][zombielist->gy - 1] > 0 && zombielist->gx != 0 && zombielist->status == 1) { zombielist->status = 2; } else { zombielist->gx = (zombielist->x >= 590) ? 9 : (zombielist->x >= 503) ? 8 : (zombielist->x >= 432) ? 7 : (zombielist->x >= 351) ? 6 : (zombielist->x >= 271) ? 5 : (zombielist->x >= 185) ? 4 : (zombielist->x >= 101) ? 3 : (zombielist->x >= 30) ? 2 : (zombielist->x >= -59) ? 1 : 0; } }
|
如果僵尸所在的格子有植物,并且僵尸当前格子不为0,并且僵尸状态为走路(1),那么将僵尸状态改为2(吃植物),否则判断僵尸的位置,并计算出当前僵尸所在的x单元格。如果僵尸位置到最左边,僵尸所在单元格设置为0.
吃植物判定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| void zombieEatPlant() { if(zombielist->status==2){ int position = 5 * (zombielist->gx - 1) + zombielist->gy; int currentCell = zb[zombielist->gx - 1][zombielist->gy - 1]; newplant[position - 1].hp -= zombielist->zombieattack; if (newplant[position - 1].hp <= 0) { zb[zombielist->gx - 1][zombielist->gy - 1] = 0; zombielist->status = 1; } if (currentCell == 1) { stime += 1; } else if (currentCell == 2) { credit++; } else if (currentCell <= 0) { zombielist->status = 1; } } }
|
本段代码的基本逻辑是,如果当前僵尸状态为2,则当前格子植物血量减少,如果血量小于0,则将植物的状态设置为0,将僵尸的状态改为1(走路状态)。如果当前单元格是豌豆射手则增加积分,如果是向日葵则增加时间。