📄 gamestrategy.java
字号:
import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
public class GameStrategy extends FullCanvas implements Runnable, Game{
public GameStrategy( Display d ){
display = d;
}
// interface Game
public void addGameListener( GameListener listener ){
gameListener = listener;
}
public void addSoundPlayer( SoundPlayer player ){
soundPlayer = player;
}
public void gameStart( int lev ){ // "start" item is selected
if( thread == null ){
init();
}
level = lev;
state = 2; // loading image;
display.setCurrent( this );
repaint();
serviceRepaints();
Thread t = new Thread(){
public void run(){
sImplement.restartReset();
sImplement.init( level );
}
};
t.start();
}
public void gameResume(){ // "resume" item is selected
display.setCurrent( this );
state = tmpState2;
counter = tmp2Counter;
counter2 = tmp2Counter2;
}
//
public void run(){
while( !done ){
try{
Thread.sleep( sleepTime );
loop();
}
catch( InterruptedException e ){
}
}
}
public void paint( Graphics g ){
if( state == 2 ){ // loading image
g.setClip( 0, 0, getWidth(), getHeight() );
g.setColor( 0, 0, 0 );
g.fillRect( 0, 0, getWidth(), getHeight() );
g.setColor( 255, 255, 255 );
String s = "Loading......";
int x = (getWidth()-g.getFont().stringWidth(s))/2;
int y = (getHeight()-g.getFont().getHeight())/2;
g.drawString( s, x, y, 4|16 );
}
else if( state == 11 ){
}
else if( image != null ){
g.drawImage( image, 0, 0, 4|16 );
}
}
protected void keyPressed( int keyCode ){
pressedKey = keyCode;
if( state == 3 ){ // showStart
showStartOK();
}
else if( state == 4 ){ // showDead;
// showDeadOK();
}
else if( state == 5 ){ // showCross;
// showCrossOK();
}
else if( state == 6 ){ // showComplete;
// showCompleteOK();
}
else if( state == 2 ){ // showLoading
}
else{
switch( keyCode ){
case -6:
pauseList();
break;
case -7:
pause();
break;
}
}
if( state == 1 ){
sImplement.keyPressed( keyCode );
}
}
protected void keyReleased( int keyCode ){
if( state == 1 ){
sImplement.keyReleased( keyCode );
}
}
protected void showNotify(){
if( tmpState2 == 3 || tmpState2 == 4 || tmpState2 == 5 ||
tmpState2 == 6 || tmpState2 == 7 ){
state = tmpState2;
counter = tmp2Counter;
counter2 = tmp2Counter2;
}
else if( state == 14 ){
tmpState = tmpState2;
tmpState = 1;
tmpCounter = tmp2Counter;
tmpCounter2 = tmp2Counter2;
state = 10;
// sImplement.gamePause( gg );
}
else if( state == 13 ){
counter = 0;
counter2 = 0;
state = 3;
}
}
protected void hideNotify(){
if( state != 11 && state != 10 ){ //
tmpState2 = state;
tmp2Counter = counter;
tmp2Counter2 = counter2;
state = 14;
}
}
private void loop(){
try{
// System.out.println( "start: " + state );
switch( state ){
case 1:
gameLoop();
break;
case 3: // showGameStart
showGameStart();
break;
case 4: // showGameDead
showGameDead();
break;
case 5: // showGameCross
showGameCross();
break;
case 6:
showGameComplete(); // showGameComplete
break;
case 7:
showGameAllDead();
break;
case 10:
showGamePause();
break;
case 11:
break;
case 0:
break;
}
}
catch( Exception e ){
e.printStackTrace();
}
}
// called from strategyImplement;
public void prepareOK(){
if( isShown() ){
counter = 0;
counter2 = 0;
state = 3;
}
else{
state = 13;
}
}
public void dead(){
state = 4;
}
public void cross( int lev ){
level = lev+1;
Recorder.setMax( "Migong", level );
state = 5;
}
public void complete(){
state = 6;
}
public void allDead(){
state = 7;
}
public int getCounter(){
return counter;
}
public Graphics getGraphics(){
return gg;
}
public SoundPlayer getPlayer(){
return soundPlayer;
}
//
private void showGameStart(){
if( counter2 < 20 ){
counter2++;
sImplement.gameStart( gg, counter2 );
repaint();
serviceRepaints();
}
else{
showStartOK();
}
}
private void showGameDead(){
if( counter2 < 30 ){
counter2++;
sImplement.gameDead( gg, counter2 );
repaint();
serviceRepaints();
}
else{
showDeadOK();
}
}
private void showGameCross(){
if( counter2 < 30 ){
counter2++;
sImplement.gameCross( gg, counter2 );
repaint();
serviceRepaints();
}
else{
showCrossOK();
}
}
private void showGameComplete(){
if( counter2 <= 50 ){
counter2++;
sImplement.gameComplete( gg, counter2 );
repaint();
serviceRepaints();
}
else{
showCompleteOK();
}
}
private void showGameAllDead(){
if( counter2 <= 50 ){
counter2++;
sImplement.gameAllDead( gg, counter2 );
repaint();
serviceRepaints();
}
else{
showAllDeadOK();
}
}
private void showGamePause(){
sImplement.gamePause( gg );
// sImplement.testGamePause( gg, tmpState );
repaint();
serviceRepaints();
// sImplement.gamePause( gg );
// repaint();
// serviceRepaints();
// sImplement.gamePause( gg );
// repaint();
// serviceRepaints();
}
private void showStartOK(){
counter2 = 0;
state = 1;
}
private void showDeadOK(){
sImplement.deadStartReset();
counter2 = 0;
state = 3;
}
private void showCrossOK(){
state = 0;
counter2 = 0;
if( thread == null ){
init();
}
state = 2; // loading image;
repaint();
serviceRepaints();
Thread t = new Thread(){
public void run(){
sImplement.init( level );
}
};
t.start();
}
private void showCompleteOK(){
state = 11;
counter2 = 0;
gameListener.gameStopList();
}
private void showAllDeadOK(){
state = 11;
counter2 = 0;
gameListener.gameStopList();
}
private void gameLoop(){
counter = (counter+1)&0xfff;
sImplement.gameLoop( gg, counter );
repaint();
serviceRepaints();
}
private void pauseList(){ // when game is running
tmpState2 = state;
tmp2Counter = counter;
tmp2Counter2 = counter2;
state = 11;
gameListener.gamePauseList();
}
private void pause(){
if( state == 10 ){
state = tmpState;
counter = tmpCounter;
counter2 = tmpCounter2;
}
else{
tmpState = state;
tmpCounter = counter;
tmpCounter2 = counter2;
state = 10;
}
}
private void init(){
image = Image.createImage( getWidth(), getHeight() );
gg = image.getGraphics();
sImplement = new MyStrategyImplement( this );
thread = new Thread( this );
thread.start();
}
private int sleepTime = 50;
private boolean done = false;
private Display display;
private Thread thread;
private StrategyImplement sImplement;
private GameListener gameListener;
private SoundPlayer soundPlayer;
private Image image;
private Graphics gg;
private static int level;
private int state = 0;
private int counter = 0;
private int counter2 = 0;
private int tmpState = 0;
private int tmpCounter = 0;
private int tmpCounter2 = 0;
private int tmpState2 = 0;
private int tmp2Counter = 0;
private int tmp2Counter2 = 0;
public int pressedKey = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -