?? showmap.java
字號:
package shoot;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: Shoot game from Rex</p>
* <p>Description: This is a shoot game.</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: SkyPlay</p>
* @author Kevin
* @version 1.0
*/
public class ShowMap extends Canvas implements Runnable,CommandListener {
/**Member var declare*/
public final static String DB = "ARMOR";
public final static int PWIDTH = 16;
public final static int PHEIGHT = 24;
public static int maxScore = 0;
public static Effect effect;
public int screenTurn = 0;
public Shoot app;
public Thread thread;
private boolean appRun = true;
public static int X;
public static int Y;
public static int WIDTH;
public static int HEIGHT;
public static boolean keyEnable = true;
//graphics var
private Image splash;
private Image menu,menubar,stage,life;
private int plen = 9;
private int menuitem = 1,stageLevel = 1,perLife = 3 * plen;
final static private int[] mbar = {8,8,32,56};
final static private int stagePos = 66;
final static private int lifeLen = 14;
//upper map var
private Image upMapImg;
private int upMapX,upMapY,upMapWidth;
private boolean divide;
//nether map var
private Image downMapImg[] = new Image[3];
private int current;
private int change = 0;
//buffer screen var
private Image offImg;
private Graphics offScreen;
private int offWidth,offHeight;
private int offScrX,offScrY,upScrHeight;
//person var
private Image personImg;
public int perX,perY;
private int perMaxY,perMinY,perMaxX,perMinX;
public int score;
private boolean perMovable = true;
public int personCurrent;
private int upBlock;
//ogre var
private Image[][] ogreImg = new Image[2][3];
private int ogreLife,ogreCount = 30,currentOgre = 1;
private int ogreX,ogreMinX,ogreMaxX,ogreMoveBlock;
private int[] ogreY;
private long ogreCurTime;
private int oCurrent = 0, yCurrent = 0;
private boolean ogreAlive,ogreDirect;
private int showInterval = 0;
//bullet var
private Random random;
private Vector ogreBulletVector;
private Timer timer;
private Vector bulletVector;
private int bulletMax,moveTime;
private int timeInterval;
private long curTime;
private Image fireImg[] = new Image[2];
public int[][] bulletList = {{0,0,15},{15,1,13},{28,2,11},{39,3,9},{48,4,7},{55,5,5}};
//temporary var
private BulletTrace btTmp = null;
public ShowMap(Shoot application,Effect eff) {
app = application;
effect = eff;
try{
init();
}catch(Exception e){
e.printStackTrace();
}
}
private void init() throws Exception {
X = 0;
Y = 0;
WIDTH = getWidth();
HEIGHT = getHeight();
offWidth = getWidth() < 101 ? getWidth():101;
offHeight = getHeight() < 80 ? getHeight():80;
offImg = Image.createImage(offWidth,offHeight);
offScreen = offImg.getGraphics();
offScrX = (WIDTH - 101)/2;
offScrY = (HEIGHT - 80)/2;
upMapImg = Image.createImage("/shoot/bkg.png");
upBlock = 5;
for(int i = 1 ; i < 4 ; i++){
downMapImg[i - 1] = Image.createImage("/shoot/map"+i+".png");
}
personImg = Image.createImage("/shoot/man.png");
perMinX = 5;
perMaxX = offWidth - PWIDTH - 5;
perMinY = 5;
perMaxY = offHeight - PHEIGHT - 5;
for(int i = 0; i< 2 ; i++){
for(int j = 0; j < 3 ; j++){
ogreImg[i][j] = Image.createImage("/shoot/ogre"+i+j+".png");
}
}
int w,h;
w = ogreImg[ogreImg.length - 1][ogreImg[0].length - 1].getWidth();
h = ogreImg[ogreImg.length - 1][ogreImg[0].length - 1].getHeight();
ogreMaxX = offWidth - w - ogreMoveBlock;
ogreMinX = ogreMoveBlock;
ogreY = new int[3];
ogreY[0] = 15; //small ogre image
ogreY[1] = 20; //medial ogre image
ogreY[2] = 25; //large ogre image
ogreBulletVector = new Vector();
random = new Random();
fireImg[0] = Image.createImage("/shoot/fire0.png");
fireImg[1] = Image.createImage("/shoot/fire1.png");
bulletVector = new Vector();
bulletMax = 20;
timer = new Timer();
curTime = System.currentTimeMillis();
loadData();
this.setCommandListener(this);
this.addCommand(new Command("ExitGame",Command.EXIT,1));
effect.playSound(0);
splashPaint();
repaint2();
}
public void loadData(){
RecordStore rs = null;
try{
rs = RecordStore.openRecordStore(DB,true);
if(rs.getNumRecords() == 0){
resetGame();
saveData(true);
System.out.println("DB not existed!");
}else{
byte[] bData = rs.getRecord(1);
ByteArrayInputStream bais = new ByteArrayInputStream(bData);
DataInputStream dis = new DataInputStream(bais);
upScrHeight = dis.readInt();
divide = dis.readBoolean();
upMapX = dis.readInt();
upMapY = dis.readInt();
current = dis.readInt();
perX = dis.readInt();
perY = dis.readInt();
score = dis.readInt();
maxScore = dis.readInt();
personCurrent = dis.readInt();
ogreMoveBlock = dis.readInt();
ogreCount = dis.readInt();
currentOgre = dis.readInt();
ogreAlive = dis.readBoolean();
ogreDirect = dis.readBoolean();
moveTime = dis.readInt();
timeInterval = dis.readInt();
stageLevel = dis.readInt();
perLife = dis.readInt();
System.out.println("DB existed!");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void resetGame(){
//upper part screen height
upScrHeight = 50;////
//upper part map data
divide = false;////
upMapX = (upMapImg.getWidth() - offWidth)/2;////
upMapY = (upMapImg.getHeight() - upScrHeight)/2;////
//nether part map num
current = 0;////
//person position data
perX = (offWidth - PWIDTH)/2;////
perY = (offHeight + PHEIGHT)/2;////
score = 0;////
//maxScore = 0;////
personCurrent = 0;////
//ogre data
ogreMoveBlock = 10;////
ogreCount = 30 ;////
currentOgre = 1;////
ogreAlive = false;
ogreDirect = true;
//time interval data
moveTime = 100;////
timeInterval = 100;////
stageLevel = 1;
perLife = 3 * plen;
ogreCount = 30;
}
public void saveData(boolean add){
RecordStore rs = null;
try{
rs = RecordStore.openRecordStore(DB,true);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeInt(upScrHeight);
dos.writeBoolean(divide);
dos.writeInt(upMapX);
dos.writeInt(upMapY);
dos.writeInt(current);
dos.writeInt(perX);
dos.writeInt(perY);
dos.writeInt(score);
if(score > maxScore) maxScore = score;
dos.writeInt(maxScore);
dos.writeInt(personCurrent);
dos.writeInt(ogreMoveBlock);
dos.writeInt(ogreCount);
dos.writeInt(currentOgre);
dos.writeBoolean(ogreAlive);
dos.writeBoolean(ogreDirect);
dos.writeInt(moveTime);
dos.writeInt(timeInterval);
dos.writeInt(stageLevel);
dos.writeInt(perLife);
byte[] bData = baos.toByteArray();
if(add){
rs.addRecord(bData,0,bData.length);
}else{
rs.setRecord(1,bData,0,bData.length);
}
//System.out.println("save data length:"+bData.length);
}catch(Exception e){
e.printStackTrace();
}
}
private void moveMap(int direction){
switch (direction) {
case UP:
perMovable = (perY > perMinY);
if(perMovable){
/*if(this.upScrHeight < 50) {
this.upScrHeight += upBlock/2;
upMapY -= upBlock/2;
}*/
perY = (perY - upBlock) < perMinY ? perMinY:(perY - upBlock);
if(perY < 20) {
BulletTrace.ySc = 3;
OgreBullet.ySc = 0;
}
}else{
upMapY = (upMapY >= upBlock) ? (upMapY - upBlock):0;
}
break;
case DOWN:
perMovable = (perY < perMaxY);
if(perMovable){
/*if(this.upScrHeight > 40) {
this.upScrHeight -= upBlock /2;
upMapY += upBlock/2;
}*/
perY = (perY + upBlock) > perMaxY ? perMaxY:(perY + upBlock);
if(perY > 20) {
BulletTrace.ySc = - 4;
OgreBullet.ySc = 6 ;
}
}else{
upMapY = (upMapImg.getHeight() - upMapY) > upScrHeight ? (upMapY + upBlock):
(upMapImg.getHeight() - upScrHeight);
}
break;
case LEFT:
perMovable = perX > perMinX;
if(perMovable){
perX = (perX - upBlock) < perMinX ? perMinX:(perX - upBlock);
if(perX < 40){
BulletTrace.xSc = 2;
}
}else{
moveBullet(upBlock);
ogreX += upBlock;
if(ogreX > upMapImg.getWidth()){
ogreX -= upMapImg.getWidth();
}
if (divide){
upMapX -= upBlock;
upMapWidth += upBlock;
if(upMapWidth >= offWidth){
upMapWidth = offWidth;
divide = false;
}
}else{
if(upMapX >= upBlock){
upMapX -= upBlock;
}else{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -