加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Bullet.java 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
钟益鹏 提交于 2021-05-06 22:17 . update Bullet.java.
import greenfoot.*;
/**
* A bullet that can hit asteroids.
*
* @author Poul Henriksen
* @author Michael Kölling
*/
public class Bullet extends SmoothMover
{
/** The damage this bullet will deal */
private static final int damage = 16;
/** A bullet looses one life each act, and will disappear when life = 0 */
private int life = 30;
/**
* Default constructor for testing.
*/
public Bullet()
{
}
/**
* Create a bullet with given speed and direction of movement.
*/
public Bullet(Vector speed, int rotation)
{
super(speed);
setRotation(rotation);
addToVelocity(new Vector(rotation, 15));
Greenfoot.playSound("EnergyGun.wav");
}
/**
* The bullet will damage asteroids if it hits them.
*/
public void act()
{
if(life <= 0) {
getWorld().removeObject(this);
}
else {
life--;
move();
checkAsteroidHit();
}
}
/**
* Check whether we have hit an asteroid.
*/
private void checkAsteroidHit()
{
Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
if (asteroid != null)
{
getWorld().removeObject(this);
asteroid.hit(damage);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化