📄 gamescreen.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import javax.microedition.rms.*;
public class GameScreen extends GameCanvas
implements Runnable,CommandListener
{
public static final int TEXT_COLOR = 0xffc800;//0xD89350;
public static final int BACK_COLOR = 0x000000;
public static final Font SMALL_FONT =
Font.getFont(Font.FACE_SYSTEM/*.FACE_MONOSPACE*/,Font.STYLE_PLAIN,Font.SIZE_SMALL);
public static final Font NORMAL_FONT =
Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
public static final Font LARGE_FONT =
Font.getFont(Font.FACE_SYSTEM/*.FACE_MONOSPACE*/,Font.STYLE_PLAIN,Font.SIZE_LARGE);
public static final int MILLIS_PER_TICK = 300;
//hold the step length of the hero move once
private static final int step = GameMap.TILE_WIDTH;
//hold the direction index
public static final int UP = 0,DOWN = 1,LEFT = 2,RIGHT = 3;
//hold the image type,for create all the image in the same place
public static final int IMAGE_HERO = 0,
IMAGE_MAP = 1,
IMAGE_DIALOG_HERO = 2,
IMAGE_DIALOG_ANGLE = 3,
IMAGE_DIALOG_THIEF = 4,
IMAGE_BORDER = 5,
IMAGE_DIALOGBOX = 6,
IMAGE_MESSAGEBOX = 7,
IMAGE_BORDER2 = 8,
IMAGE_BORDER3 = 9,
IMAGE_BORDER4 = 10,
IMAGE_DIALOG_PRINCESS = 11,
IMAGE_DIALOG_BOSS = 12,
IMAGE_BLUE_GEEZER = 13,
IMAGE_RED_GEEZER = 14,
IMAGE_SPLASH = 15,
IMAGE_GAMEOVER = 16;
private Thread gameThread = null;
private boolean isPlay = false,switchTile = true;
private LayerManager layerManager;
private Command exitCmd = new Command("退 出",Command.BACK,2);
//private Command saveCmd = new Command("保 存",Command.OK,1);
private HeroSprite hero;
private GameMap gameMap;
private FightCalc fightCalc;
private Task task;
//judge that the game is new or not,if the game is new then init all params
//else read the data from the RMS
private boolean isNewGame;
private Display display;
private MainMenu menu;
private RMSUtil rms;
public Graphics g;
public int borderX,borderY;
private Image borderImage;
private Image dialogBoxImage;
private Image messageBoxImage;
private int winWidth,winHeight;
private int scrollX,scrollY;
private int curDialogImg;
private boolean isSaving;
//when dialoging,judge the input in waitKeyDown()
//private boolean downPressed = false;
//private boolean isDialog = false;
public GameScreen(Display display,MainMenu menu,RMSUtil rms,boolean isNewGame)
{
super(false);
this.display = display;
this.isNewGame = isNewGame;
this.menu = menu;
this.rms = rms;
this.setFullScreenMode(true);
isSaving = false;
g = getGraphics();
//winWidth = this.getWidth() - 2 * border;
winWidth = 128;
winHeight = winWidth;
borderX = menu.borderX;//(getWidth() - winWidth ) / 2;
borderY = menu.borderY;//(getHeight() - 160) / 2;
layerManager = new LayerManager();
hero = new HeroSprite(getImage(IMAGE_HERO),
GameMap.TILE_WIDTH,GameMap.TILE_HEIGHT);
hero.defineReferencePixel(GameMap.TILE_WIDTH / 2,GameMap.TILE_HEIGHT / 2);
gameMap = new GameMap(hero);
fightCalc = new FightCalc(hero);
task = new Task(this,hero,gameMap);
hero.setTask(task);
if (isNewGame == false)
{
load();
}
layerManager.append(hero);
layerManager.append(gameMap.getFloorMap());
this.addCommand(exitCmd);
setCommandListener(this);
borderImage = getImage(IMAGE_BORDER);
dialogBoxImage = getImage(IMAGE_DIALOGBOX);
messageBoxImage = getImage(IMAGE_MESSAGEBOX);
//System.out.println("width:"+getWidth());
//System.out.println("height:"+getHeight());
}
public void start()
{
isPlay = true;
gameThread = new Thread(this);
gameThread.start();
}
public void stop()
{
//save();
isPlay = false;
//gameThread = null;
}
//Main game loop
public void run()
{
//Graphics g = getGraphics();
//Thread currentThread = Thread.currentThread();
long startTime = 0;
long timeTake = 0;
while(isPlay == true)//(currentThread == gameThread)
{
startTime = System.currentTimeMillis();
if(isSaving == false)
{
tick();
render(g);
}
timeTake = System.currentTimeMillis() - startTime;
if (timeTake < MILLIS_PER_TICK)
{
//synchronized(this)
try
{
Thread.sleep(MILLIS_PER_TICK - timeTake);
} catch(InterruptedException e){}
}
}
}
public void tick()
{
int type = 0;
int keystate = getKeyStates();
if((keystate & this.UP_PRESSED) != 0)
{
hero.setFrame(9);
if((type = gameMap.canPass(UP)) == 1)
{
hero.move(0,-step);
/*hero.setFrame(10);
hero.move(0,-4);
layerManager.paint(g,0,0);
flushGraphics();
hero.setFrame(11);
hero.move(0,-4);
layerManager.paint(g,0,0);
flushGraphics();
hero.setFrame(10);
hero.move(0,-4);
layerManager.paint(g,0,0);
flushGraphics();
hero.setFrame(11);
hero.move(0,-4);
layerManager.paint(g,0,0);
flushGraphics();
hero.setFrame(9);*/
}
}else if ((keystate & this.DOWN_PRESSED) != 0)
{
hero.setFrame(0);
if((type = gameMap.canPass(DOWN)) == 1)
{
hero.move(0,step);
}
}else if ((keystate & this.LEFT_PRESSED) != 0)
{
hero.setFrame(3);
if((type = gameMap.canPass(LEFT)) == 1)
{
hero.move(-step,0);
}
}else if ((keystate & this.RIGHT_PRESSED) != 0)
{
hero.setFrame(6);
if((type = gameMap.canPass(RIGHT)) == 1)
{
hero.move(step,0);
}
}
/*if want to scroll screen ,please add the following code
*else comment it;
scrollWin();*/
if(type >= GameMap.MAP_LOCKED_BARRIER)dealType(type);
}
private void dealType(int type)
{
/* switch (type)
{
//upstair
case 9: gameMap.upstair();break;
//downstair
case 10: gameMap.downstair();break;
//door:yellow blue red
case 11:
case 12:
case 13:if(hero.useKey(type - 11)){
gameMap.remove();
}break;
//gem
case 15:case 16:case 17:case 18:case 19:case 20:
case 21://hero.takeGem(type);
}*/
if (type == GameMap.MAP_UPSTAIR){ //upstair
gameMap.upstair();hero.setFrame(0);
}else if (type == GameMap.MAP_DOWNSTAIR){ //downstair
gameMap.downstair();hero.setFrame(0);
}else if ((type >= GameMap.MAP_YELLOW_DOOR)&&(type <=GameMap.MAP_RED_DOOR)){ //try to open the door
if(hero.useKey(type)){
gameMap.remove();
}
}else if(type == GameMap.MAP_BARRIER){
gameMap.remove();
}else if((type == GameMap.MAP_SHOP1)||(type == GameMap.MAP_SHOP2)){
if(gameMap.curFloorNum == 3){
shop(ShopScreen.SHOP_3);
}else{
shop(ShopScreen.SHOP_11);
}
}else if ((type >= GameMap.MAP_YELLOW_KEY)&&(type <= GameMap.MAP_SHIELD3)){ //normal gem
//hero.takeGem(type);
//System.out.println("before takegem");
showMessage(this,g,hero.takeGem(type));
gameMap.remove();
//try{Thread.sleep(1000);}catch(Exception e){}
}else if(type >= GameMap.MAP_ANGLE){
if(type > GameMap.MAP_ORGE31)type -= GameMap.SWITCH_OFFSET;
if(((type >= GameMap.MAP_ANGLE)&&(type <= GameMap.MAP_RED_GEEZER))||
(type == GameMap.MAP_ORGE31)){
/*处理任务,注意不同层的老头的任务不同*/
//task.dealTask(type);
dealTask(type);
}else if((type >= GameMap.MAP_ORGE1)&&(type <= GameMap.MAP_ORGE30)){
fight(type);
}
}
/*
FightScreen f = new FightScreen(display,this);
f.start();
display.setCurrent(f);
*/
//fight(type);
//shop();
//jump();
//talk(type);
//task.dealTask(type);
}
public void showMessage(GameCanvas canvas,Graphics g,String msg)
{
int x = borderX;
int y = borderY+32;
int w = 128;
int h = 64;
g.setColor(BACK_COLOR);
g.fillRect(x,y,w,h);
g.drawImage(messageBoxImage,x,y,Graphics.TOP|Graphics.LEFT);
g.setColor(TEXT_COLOR);
if(msg.length() <= 10){
g.drawString(msg,x+10,y+20,Graphics.TOP|Graphics.LEFT);
}else if(msg.length() <= 20){
g.drawString(msg.substring(0,10),x+10,y+15,Graphics.TOP|Graphics.LEFT);
g.drawString(msg.substring(10,msg.length()),x+10,y+30,Graphics.TOP|Graphics.LEFT);
}else{
g.drawString(msg.substring(0,10),x+10,y+10,Graphics.TOP|Graphics.LEFT);
g.drawString(msg.substring(10,20),x+10,y+25,Graphics.TOP|Graphics.LEFT);
g.drawString(msg.substring(20,msg.length()),x+10,y+40,Graphics.TOP|Graphics.LEFT);
}
canvas.flushGraphics();
//try{Thread.sleep(1000);}catch(Exception e){}
boolean isflash = true;
int keystate = canvas.getKeyStates();
long startTime = 0;
long timeTake = 0;
while((keystate & this.FIRE_PRESSED) == 0)
{
startTime = System.currentTimeMillis();
if(isflash == true){
g.setColor(TEXT_COLOR);
}else{
g.setColor(BACK_COLOR);
}
g.drawString("OK",x+105,y+45,Graphics.TOP | Graphics.LEFT);
canvas.flushGraphics();
isflash = !isflash;
keystate = canvas.getKeyStates();
timeTake = System.currentTimeMillis() - startTime;
if (timeTake < MILLIS_PER_TICK){
try{
Thread.sleep(MILLIS_PER_TICK - timeTake);
}catch(Exception e){}
}
}
}
public void render(Graphics g)
{
g.setColor(0x000000);
g.fillRect(0, 0, getWidth(), getHeight());
//g.fillRect(borderX,borderY,128,160);
/* g.setColor(TEXT_COLOR);
//Font f = Font.getFont(Font.FACE_MONOSPACE,Font.STYLE_PLAIN,Font.SIZE_SMALL);
g.setFont(SMALL_FONT);
//g.drawString("hp:"+hero.getHp(),10,getHeight()-20,Graphics.TOP|Graphics.LEFT);
g.drawString(
"级"+hero.getLevel()+
" 血"+hero.getHp()+
" 金"+hero.getMoney(),
borderX+8,borderY+134,Graphics.TOP|Graphics.LEFT);
g.drawString("攻"+hero.getAttack()+
" 防"+hero.getDefend()+
" 经"+hero.getExperience(),
borderX+8,borderY+145,Graphics.TOP|Graphics.LEFT);
g.setColor(0xFECCA9);
g.drawString(Integer.toString(hero.getYellowKey()),borderX+109,borderY+132,
Graphics.TOP|Graphics.LEFT);
g.setColor(0xCCCCFE);
g.drawString(Integer.toString(hero.getBlueKey()),borderX+109,borderY+140,
Graphics.TOP|Graphics.LEFT);
g.setColor(0xFE8888);
g.drawString(Integer.toString(hero.getRedKey()),borderX+109,borderY+148,
Graphics.TOP|Graphics.LEFT);
g.setFont(NORMAL_FONT);*/
drawAttr(g);
gameMap.animateMap();
/*if scroll screen use following code*/
scrollWin();
layerManager.setViewWindow(scrollX,scrollY,winWidth,winHeight);
layerManager.paint(g,borderX,borderY);
/*if don't scroll screen use following code
layerManager.paint(g,0,0); */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -