📄 bonus.java
字号:
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class Bonus extends Sprite {
private boolean isAlive=false;
public int bonusType;
public final static int ADD_BULLET_BONUS=0;
public final static int SPEED_UP_BONUS=1;
public final static int ADD_LIFE_BONUS=2;
public final static int DISPERSE_BULLET_BOUNS=3;
public final static int ADD_SCORE=4;
private int frameWidth,frameHeight;
private int canvasWidth,canvasHeight;
private Image imgBonus;
private Bonus bonus;
private Random random;
private int direction;
public final static int LEFT_DOWN=0;
public final static int RIGHT_DOWN=2;
public final static int MIDDLE_DOWN=1;
public final static int RIGHT_UP=3;
public final static int UP_UP=4;
public final static int LEFT_UP=5;
private int reflexNum;
public Bonus(Image image, int frameWidth, int frameHeight,int bonusType) {
super(image, frameWidth, frameHeight);
// TODO Auto-generated constructor stub
imgBonus=image;
this.bonusType=bonusType;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
defineReferencePixel(frameWidth / 2, frameHeight / 2);
random=new Random();
direction = (random.nextInt() & 0x0fffffff) % 3;
reflexNum=0;
}
public void setCanvasSize(int canvasWidth, int canvasHeight) {
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
}
public void setAlive(int x, int y) {
isAlive = true;
setPosition(x, y);
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public boolean isAlive() {
return isAlive;
}
public Image getImage(){
return imgBonus;
}
public void tick(){
if (isAlive) {
switch (direction) {
case LEFT_DOWN:
move(-4, 4);
break;
case RIGHT_DOWN:
move(4, 4);
break;
case MIDDLE_DOWN:
move(0, 6);
break;
case RIGHT_UP:
move(4, -4);
break;
case UP_UP:
move(0, -4);
break;
case LEFT_UP:
move(-4, -4);
break;
}
if (getX() <= 0) {
switch (direction) {
case LEFT_DOWN:
direction = RIGHT_DOWN;
break;
case LEFT_UP:
direction = RIGHT_UP;
break;
}
reflexNum++;
}
if (getX() >= canvasWidth-frameWidth) {
switch (direction) {
case RIGHT_DOWN:
direction = LEFT_DOWN;
break;
case RIGHT_UP:
direction = LEFT_UP;
break;
}
reflexNum++;
}
if (getY() <= 0) {
switch (direction) {
case LEFT_UP:
direction = LEFT_DOWN;
break;
case UP_UP:
direction = MIDDLE_DOWN;
break;
case RIGHT_UP:
direction = RIGHT_DOWN;
break;
}
reflexNum++;
}
if (getY() >= canvasHeight-frameHeight) {
switch (direction) {
case LEFT_DOWN:
direction = LEFT_UP;
break;
case MIDDLE_DOWN:
direction = UP_UP;
break;
case RIGHT_DOWN:
direction = RIGHT_UP;
break;
}
reflexNum++;
}
if (reflexNum == 3) {
isAlive = false;
}
}
// if(isAlive){
// move(4,4);
// if(getY()>canvasHeight){
// isAlive=false;
// }
// }
}
public void getBonus(Bonus bonus){
this.bonus=bonus;
}
// public void draw(Graphics g){
// switch (bonusType) {
// case ADD_BULLET_BONUS:
// //g.setClip(bonus.getX(), bonus.getY(), 14, 13);
// g.drawImage(imgBonus, bonus.getX(), bonus.getY(), 0);
// break;
// case SPEED_UP_BONUS:
// //g.setClip(bonus.getX(), bonus.getY(), 14, 13);
// g.drawImage(imgBonus, bonus.getX(), bonus.getY(), 0);
// break;
// case ADD_LIFE_BONUS:
// //g.setClip(bonus.getX(), bonus.getY(), 14, 13);
// g.drawImage(imgBonus, bonus.getX(), bonus.getY(), 0);
// break;
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -