在做安卓开发实际项目当中,尤其是在编写游戏时,我们一定要判断一下子弹的问题,应为这个是非常重要的。如果不判断的话,就会给玩家视觉上带来不好的效果。
Java代码:
for (int i = 0; i < enemy_bullets.length; i++)
{
enemy_bullets[i].paint(c);
if (enemy_bullets[i].getX() < 0|| enemy_bullets[i].getX() - 5 > max_width
|| enemy_bullets[i].getY() < 0|| enemy_bullets[i].getY() - 5 > max_height) {
// 子弹越界处理 enemy_bullets[i].setVisible(false); }
if (enemy_bullets[i].isVisible() == false) {
// 子弹不可见时,设为可见,位置为对应敌人发射处 enemy_bullets[i].setVisible(true); x = enemys[i].getX();
y = enemys[i].getY();
//这个是玩家在游戏当中的方向(上、右、下、左) switch (enemys[i].getTransform()) {
case Sprite.TRANS_NONE: enemy_bullets[i].setPosition(x + 5, y + 16); enemy_bullets[i].setTransform(Sprite.TRANS_NONE);
break;
case Sprite.TRANS_ROT90: enemy_bullets[i].setPosition(x - 6, y + 5); enemy_bullets[i].setTransform(Sprite.TRANS_ROT90);
break;
case Sprite.TRANS_ROT180: enemy_bullets[i].setPosition(x + 5, y - 6); enemy_bullets[i].setTransform(Sprite.TRANS_ROT180);
break;
case Sprite.TRANS_ROT270: enemy_bullets[i].setPosition(x + 16, y + 5); enemy_bullets[i].setTransform(Sprite.TRANS_ROT270);
break;
default: break;
}
}
else { // 子弹可见时
if (isBullerMeetTank(enemy_bullets[i].getX(), enemy_bullets[i].getY(), player.getX(),player.getY()))
{
// 如果遇上玩家,两者同时不见 enemy_bullets[i].setVisible(false); player.setVisible(false); explodes[4].setVisible(true);//爆炸 explodes[4].setPosition(player.getX(), player.getY()); }
if (enemy_bullets[i].isVisible()&& ! canBulletPass(enemy_bullets[i].getX(), enemy_bullets[i].getY()))
{
// 判断子弹是否能穿过 enemy_bullets[i].setVisible(false);
if (background.getCell(enemy_bullets[i].getY() / 16, enemy_bullets[i].getX() / 16) != 2) {
background.setCell(enemy_bullets[i].getY() / 16, enemy_bullets[i].getX() / 16, 0); map[enemy_bullets[i].getY() / 16][enemy_bullets[i].getX() / 16] = 0; }
}
switch (enemy_bullets[i].getTransform()) {
// 延原来方向move前进或后退 case Sprite.TRANS_NONE: enemy_bullets[i].move(0, 16); break;
case Sprite.TRANS_ROT90: enemy_bullets[i].move(-16, 0); break;
case Sprite.TRANS_ROT180: enemy_bullets[i].move(0, -16); break;
case Sprite.TRANS_ROT270: enemy_bullets[i].move(16, 0); break;
default: break; }
}
}
player.paint(c);
Thread.sleep(200);//时间为毫秒
}
} catch (Exception e) { e.printStackTrace();
} finally { if (c != null) { holder.unlockCanvasAndPost(c);
}
}
}
}
中国足彩网信息请查看IT技术专栏
2025国考·省考课程试听报名