📄 mainsprite.java
字号:
import java.util.Random;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
//主精灵类
public class MainSprite extends Sprite
{
private Random rand;
private int speed;
private TiledLayer barrier;//障碍图层
public static final int UP = 0;
public static final int RIGHT = 1;
public static final int DOWN = 2;
public static final int LEFT = 3;
public MainSprite(Image img, int tw, int th, int speed, TiledLayer barrier)
{
super(img, tw, th);
rand = new Random();
this.speed = speed;
this.barrier = barrier;
this.RandomPosition();
}
public Random getRandom()
{
return rand;
}
public int getSpeed()
{
return this.speed;
}
public TiledLayer getBarrier()
{
return this.barrier;
}
//随机生成精灵位置
public void RandomPosition()
{
// 第一次生成
this.setPosition(Math.abs(rand.nextInt() % (barrier.getWidth()- this.getWidth())),
Math.abs(rand.nextInt() % (barrier.getHeight()- this.getHeight())));
// 如果发生碰撞,重新生成
while (this.collidesWith(barrier, true))
{
this.setPosition(Math.abs(rand.nextInt() % (barrier.getWidth()- this.getWidth())),
Math.abs(rand.nextInt() % (barrier.getHeight()- this.getHeight())));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -