加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Asteroid.java 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
luyanfei78 提交于 2021-04-13 22:33 . add initial files.
import greenfoot.*;
/**
* A rock in space.
*
* @author Poul Henriksen
* @author Michael Kölling
*/
public class Asteroid extends SmoothMover
{
/** Size of this asteroid */
private int size;
/** When the stability reaches 0 the asteroid will explode */
private int stability;
/**
* Create an asteroid with default size and random direction of movement.
*/
public Asteroid()
{
this(50);
}
/**
* Create an asteroid with a given size and random direction of movement.
*/
public Asteroid(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
/**
* Create an asteroid with a given size and direction of movement.
*/
public Asteroid(int size, Vector velocity)
{
super(velocity);
setSize(size);
}
public void act()
{
move();
}
/**
* Set the size of this asteroid. Note that stability is directly
* related to size. Smaller asteroids are less stable.
*/
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
/**
* Return the current stability of this asteroid. (If it goes down to
* zero, it breaks up.)
*/
public int getStability()
{
return stability;
}
/**
* Hit this asteroid dealing the given amount of damage.
*/
public void hit(int damage)
{
stability = stability - damage;
if (stability <= 0)
{
breakUp();
}
}
/**
* Break up this asteroid. If we are still big enough, this will create two
* smaller asteroids. If we are small already, just disappear.
*/
private void breakUp()
{
Greenfoot.playSound("Explosion.wav");
if (size <= 16) {
getWorld().removeObject(this);
}
else {
int r = getVelocity().getDirection() + Greenfoot.getRandomNumber(45);
double l = getVelocity().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
Asteroid a1 = new Asteroid(size/2, speed1);
Asteroid a2 = new Asteroid(size/2, speed2);
getWorld().addObject(a1, getX(), getY());
getWorld().addObject(a2, getX(), getY());
a1.move();
a2.move();
getWorld().removeObject(this);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化