?? fly.java
字號:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.util.Random;
/**
*
* @author TOM
*/
public class Fly {
private Hunt_Canvas manCanvas; //游戲畫面
private int[] Locus;
private Sprite fly;
private Random rnd; // 隨機種子
private int SWidth, SHeight; // 設備屏幕的寬度與高度
static int t=0;
public Fly(Hunt_Canvas manCanvas, Image img, int SWidth, int SHeight) {
this.manCanvas = manCanvas;
fly = new Sprite(img, img.getWidth()/3, img.getHeight());
fly.setFrame(0);
Locus = new int[5];
rnd = new Random();
this.SWidth = SWidth;
this.SHeight = SHeight;
}
// 初始化運行軌跡隨機數組
public void InitFlyLocus() {
Locus[0] = (rnd.nextInt() & 0x7fffffff) % 4;
switch (Locus[0]) {
case 0:
Locus[1] = -50; //初始X坐標值
Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40); //初始的Y坐標值
Locus[3] = (rnd.nextInt() & 0x7fffffff) % 12 + 7; //X坐標增量
Locus[4] = 0; //Y坐標增量
break;
case 1:
Locus[1] = SWidth + 50;
Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40);
Locus[3] = ( (rnd.nextInt() & 0x7fffffff) % 12 + 7) * -1;
Locus[4] = 0;
break;
case 2:
Locus[1] = -50;
Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40);
Locus[3] = (rnd.nextInt() & 0x7fffffff) % 12 + 7;
Locus[4] = (rnd.nextInt()) % 10+5;
break;
case 3:
Locus[1] = SWidth + 50;
Locus[2] = (rnd.nextInt() & 0x7fffffff) % (SHeight-40);
Locus[3] = (rnd.nextInt() & 0x7fffffff) % 12 + 7;
Locus[4] = (rnd.nextInt()) % 10+5;
break;
}
}
// 顯示
public void paint(Graphics g) {
fly.setPosition(Locus[1], Locus[2]);
fly.nextFrame();
fly.paint(g);
UpdataFlyLocus(); // 更新運行軌跡數組
}
// 更新運行軌跡數組
private void UpdataFlyLocus() {
Locus[1] += Locus[3];
if(Locus[4]!=0){
t++;
if(t<10){
Locus[2] += Locus[4];
}
else{
Locus[4]*=-1;
t=0;
}
}
else
Locus[2] += (rnd.nextInt()) % 10+5;
//超出邊界的處理
if (Locus[1] < -50 || Locus[1] > SWidth + 50) {
InitFlyLocus();
manCanvas.setFlag(4,false);
}
if (Locus[2] < 0 || Locus[2] > SHeight-40 ) {
Locus[4] *= -1;
}
}
public Sprite getFly(){
return fly;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -