📄 blackbomb.java
字号:
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import java.util.Timer;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;
import BombPkg.Bomb;
public class BlackBomb extends UiApplication
{
public static void main(String[] args)
{
BlackBomb app = new BlackBomb();
app.enterEventDispatcher();
}
public BlackBomb()
{
pushScreen(new StartScreen());
}
}
final class StartScreen extends MainScreen
{
public static final Bitmap signBitmap = Bitmap.getBitmapResource("Sign.png");
public static final Bitmap handBitmap = Bitmap.getBitmapResource("hand.png");
public static final Bitmap timeBitmap = Bitmap.getBitmapResource("time16.png");
public static final Bitmap startBitmap = Bitmap.getBitmapResource("start.png");
public static final int _SIDELENOFBMP = 16;
public static final int _BMPICONNUM = 16;
class bomb_profile
{
public int enterWidth;
public int enterHeight;
public int enterBombNum;
public bomb_profile(int iw,int ih,int ib)
{
enterWidth = iw;
enterHeight = ih;
enterBombNum = ib;
}
}
public bomb_profile bp = new bomb_profile(14,14,30);
class Top_5
{
public String name[];
public int bomb_num[];
public int score[];
public Top_5()
{
name = new String[5];
bomb_num = new int[5];
score = new int[5];
}
}
public Top_5 top5 = new Top_5();
private class startField extends Field implements DrawStyle
{
private int fieldWidth, fieldHeight, xCoord, yCoord;
public startField(int xVal, int yVal)
{
super(Field.NON_FOCUSABLE);
xCoord = xVal;
yCoord = yVal;
fieldHeight = Graphics.getScreenHeight();
fieldWidth = Graphics.getScreenWidth();
}
public void setXCoord(int xVal)
{
if (xVal != xCoord)
{
xCoord = xVal;
invalidate();
}
}
public int getXCoord(){return xCoord;}
public void setYCorrd(int yVal)
{
if (yVal != yCoord)
{
yCoord = yVal;
invalidate();
}
return;
}
public int getYCoord() { return yCoord; }
public int getPreferredWidth(){ return fieldWidth; }
public int getPreferredHeight() { return fieldHeight; }
public void layout(int width, int height)
{
setExtent(getPreferredWidth(),
getPreferredHeight());
}
public void paint(Graphics graphics)
{
graphics.drawBitmap(0,0,240,260,startBitmap,0,0);
}
}
public StartScreen()
{
super();
startField sf = new startField(50,20);
add(sf);
try
{
javax.microedition.io.file.FileConnection fconn = (javax.microedition.io.file.FileConnection)Connector.open("file:///store/home/user/bbMine.txt",Connector.READ_WRITE);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if ((!fconn.exists())||(fconn.fileSize()==0))
{
fconn.create(); // create the file if it doesn't exist
java.io.DataOutputStream dataOut = fconn.openDataOutputStream();
for(int i=0;i<5;i++)
{
top5.name[i] = "NoName";
top5.bomb_num[i] = 30;
top5.score[i] = 65535;
//dataOut.writeChars(top5.name[i]);
dataOut.writeInt(top5.bomb_num[i]);
dataOut.writeInt(top5.score[i]);
}
dataOut.flush();
dataOut.close();
}
else //read and write file
{
java.io.DataInputStream dataIn = fconn.openDataInputStream();
for(int i=0;i<5;i++)
{
//top5.name[i] = dataIn.readChars();
top5.bomb_num[i] = dataIn.readInt();
top5.score[i] = dataIn.readInt();
}
dataIn.close();
}
fconn.close();
}
catch (java.lang.Exception e)
{
Dialog.alert(e.toString());
}
}
private MenuItem _bombMenu = new MenuItem("New Game", 110, 10)
{
public void run()
{
BombScreen _bombScreen = new BombScreen();
UiApplication.getUiApplication().pushScreen(_bombScreen);
}
};
/*
private MenuItem _bombSetting = new MenuItem("Setting", 110, 10)
{
public void run()
{
SettingScreen _settingScreen = new SettingScreen();
UiApplication.getUiApplication().pushScreen(_settingScreen);
}
};
*/
private MenuItem _closeItem = new MenuItem("Close", 200000, 10)
{
public void run()
{
onClose();
}
};
private MenuItem _bombAbout = new MenuItem("About", 200000, 10)
{
public void run()
{
Dialog.alert("ericlaidev@126.com");
}
};
private MenuItem _bombScoreList = new MenuItem("Top Five", 200000, 10)
{
public void run()
{
String sc = new String("Time used:\n");
for(int i=0;i<5;i++)
{
sc=sc+top5.score[i]+"\n";
}
Dialog.alert(sc);
}
};
//override makeMenu to add the new menu items
protected void makeMenu( Menu menu, int instance )
{
menu.add(_bombMenu);
// menu.add(_bombSetting);
menu.add(_bombScoreList);
menu.add(_bombAbout);
menu.add(_closeItem);
}
public boolean onClose()
{
System.exit(0);
return true;
}
public class BombScreen extends MainScreen
{
Bomb bomb = new Bomb(bp.enterWidth,bp.enterHeight,bp.enterBombNum,7,7);
private int timeLen=0;
Timer timer;
private int recordBroken = 0;
class timerCounter extends java.util.TimerTask
{
public void run()
{
if(bomb.getProgress()==Bomb._ONGOING)
{
timeLen++;
invalidate();
}
}
}
public void close()// when close the screen, we need to save the score to file.
{
if(bomb.getProgress() == Bomb._ONGOING)
{
if(Dialog.NO ==Dialog.ask(Dialog.D_YES_NO,"Quit?"))
{
return;
}
}
if((bomb.getProgress() == Bomb._SUCCESSFUL)&&(recordBroken == 1))
{
try
{
javax.microedition.io.file.FileConnection fconn = (javax.microedition.io.file.FileConnection)Connector.open("file:///store/home/user/bbMine.txt",Connector.READ_WRITE);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (fconn.exists() && fconn.fileSize()!=0)
{
fconn.truncate(0);
//fconn.create(); // create the file if it doesn't exist
java.io.DataOutputStream dataOut = fconn.openDataOutputStream();
for(int i=0;i<5;i++)
{
dataOut.writeInt(top5.bomb_num[i]);
dataOut.writeInt(top5.score[i]);
}
dataOut.flush();
dataOut.close();
}
else
{
Dialog.alert("Score save failed!");
}
fconn.close();
}
catch(java.io.IOException e)
{
Dialog.alert(e.toString());
}
}
super.close();
}
public BombScreen()
{
timer = new Timer();
timer.schedule(new timerCounter(),0,1*1000);
CustomField1 cf = new CustomField1(50,20);
add(cf);
//addTrackwheelListener(this);
}
/**
* Invoked when a key is pressed.
*/
public boolean keyChar(char key, int status, int time)
{
switch (key)
{
// ENTER, SPACE, x, and X can be used to place the X (in addition to track wheel click)
// On some devices, the CONTROL_SELECT key can be used as well.
case 'd':case 'f':case 'D':case 'F':
if(bomb.cursorX > 0) bomb.cursorX--;
break;
case 'j':case'k':case'J':case'K':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -