?? mousesprite.java
字號(hào):
/***************************************************
* 程序文件名稱: MouseSprite.java
* 功能:構(gòu)造地鼠精靈
***************************************************/
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class MouseSprite extends Sprite implements Runnable
{
private int level;
private long score, recordScore;
private int shuX, shuY, count;
private int []shunXu;
private Random rand;
private boolean sleeping, gameOver, newLevel;
private Thread mouseT;
private int beatMouse;
public MouseSprite( Image image, int frameWidth, int frameHeight )
{
//地鼠精靈貼片圖像及精靈的寬度與高度
super(image, frameWidth, frameHeight );
//設(shè)置初始值
initGame();
}
//確定地鼠出現(xiàn)的初始位置
private void initS()
{
//定義數(shù)組
shunXu = new int[10*level];
//確定隨機(jī)數(shù),作為地鼠出現(xiàn)的位置
for( int i = 0; i < 10*level; i++ )
{
shunXu[i] = rand.nextInt()%5;
}
}
//過(guò)關(guān)卡
private void checkPass()
{
if ( beatMouse >= ( 8*level ) && count == 10*level )
{
level++;
newLevel = true;
gameOver = false;
}
else
{
gameOver = true;
newLevel = false;
sleeping = true;
}
}
//設(shè)置初始值
public void initGame()
{
//定義線程
mouseT = new Thread(this);
//確定地鼠精靈x坐標(biāo)
shuX = 42;
//確定地鼠精靈y坐標(biāo)
shuY = 110;
//計(jì)數(shù)回零
count = 0;
//第1關(guān)
level = 1;
//累計(jì)得分回零
score = 0;
//打中地鼠的次數(shù)
beatMouse = 0;
//地鼠精靈的狀態(tài)(是否可見(jiàn))
sleeping = false;
//允許進(jìn)行游戲
gameOver = false;
//新的一關(guān)
newLevel = false;
//地鼠精靈坐標(biāo)
this.setPosition( shuX, shuY);
//設(shè)定地鼠精靈不可見(jiàn)
this.setVisible(false);
//隨機(jī)數(shù)
rand = new Random();
}
//啟動(dòng)線程
public void start()
{
if(mouseT.isAlive())
{
sleeping = false;
}
else
{
mouseT.start();
}
}
//運(yùn)行線程
public void run()
{
while(!sleeping)
{
//取初始位置
initS();
//計(jì)數(shù)回零
count = 0;
//改變地鼠出現(xiàn)的位置
for ( int i = 0; i < 10*level; i++ )
{
//移動(dòng)地鼠出現(xiàn)的位置
moveTo(shunXu[i]);
//根據(jù)關(guān)卡數(shù)加快地鼠出現(xiàn)的頻率
sleep((2000-100*(level-1)));
//計(jì)數(shù)
count++;
}
//過(guò)關(guān)
checkPass();
//記錄得分
recordScore = score;
}
}
public void sleep( long time )
{
try
{
Thread.sleep(time);
}
catch( InterruptedException ie )
{ }
}
//移動(dòng)地鼠精靈到指定的坐標(biāo)位置
public void moveTo( int s )
{
//地鼠不可見(jiàn)
this.setVisible(false);
//暫停500毫秒
this.sleep( 500 );
//根據(jù)隨機(jī)數(shù),指定坐標(biāo)位置
switch(s)
{
case 1:
shuX = 42;
shuY = 93;
break;
case 3:
shuX = 110;
shuY = 95;
break;
case 0:
shuX = 180;
shuY = 92;
break;
case 2:
shuX = 100;
shuY = 134;
break;
case 4:
shuX = 120;
shuY = 73;
break;
}
//地鼠出現(xiàn)的位置
this.setPosition( shuX, shuY);
//地鼠可見(jiàn)
this.setVisible(true);
}
//開(kāi)始第1關(guān)
public void setLevel( int l )
{
level = l;
}
//打中地鼠的次數(shù)歸零
public void initBeatMouse()
{
beatMouse = 0;
}
//打中了地鼠(精靈碰撞)
public void checkBeat()
{
this.setVisible(false);
beatMouse++;
}
//計(jì)分
public void setScore()
{
score = beatMouse*100;
}
//過(guò)關(guān)
public int getLevel()
{
return level;
}
//得分
public long getScore()
{
return score;
}
//繼續(xù)游戲
public boolean isGameOver( )
{
return gameOver;
}
//新的1關(guān)
public boolean isNewLevel()
{
return newLevel;
}
//增加1關(guān)
public void setNewLevel(boolean b)
{
newLevel = b;
}
//暫停
public void stop()
{
sleeping = true;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -