1 Star 0 Fork 79

Dio/asteroids

forked from 吕焱飞/asteroids 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Asteroid.java 3.13 KB
一键复制 编辑 原始数据 按行查看 历史
Dio 提交于 2021-05-01 21:51 . update Asteroid.java.
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)
{
Space space = (Space)getWorld();
Counter counter = space.getCounter();
counter.add(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);
}
}
public void collide(){
if(isTouching(Rocket.class)){
removeTouching(Rocket.class);
Space world = (Space)getWorld();
Explosion explosion = new Explosion();
getWorld().addObject(explosion,getX() ,getY());
world.gameOver();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/i-am-the-ghost-cicada/asteroids.git
git@gitee.com:i-am-the-ghost-cicada/asteroids.git
i-am-the-ghost-cicada
asteroids
asteroids
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385