📄 mygame.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
//////////////////////////////////////////////
//游戏类
// 游戏场景,游戏过程;
///////////////////////////////////////////////
public class MyGame extends GameCanvas
implements Runnable
{
static boolean musicON = true;
public static int stage,score,GetXue,GetShengming;
public static LayerManager lm;
public static GameSpringe
sprock,spDownRock, //石头
spjiguan,spjiguan1,spjiguan2, //机关
spzhangai, //障碍
splouti,splouti1,splouti2, //楼梯
spmen, //门
spjian,spjian1,spjian2,spjian3,//箭
spgunR1,spgunR2,spgunD1, //棍
spxin, //心
spbaozhang,spbaozhang1; //宝藏
public static GameOgre
spguai,spguai1,spguai2,spguai3,spguai4; //怪
public static GamePlayer player;
public static PlayMusic pp;
public static TiledLayer tlqiang;
private TiledLayer tlbeijing;
private Image map,caidan,beijing;
private Image tou,xue,shengming;
public static Sprite spxue,sptou,spshengming;
public static boolean isL,isR,isD,isU,isOK,isDown,isJump,isUandD;
//游戏画面
public static Displayable instance;
private boolean isKey,isEndGame,islose,flag,isMOV;
public static boolean ispause,isTime;
private int i;
private int mapqiang2[][]= {
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0,
2, 2 },
{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0 },
{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0 },
{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0 },
{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2,
2, 2 },
{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0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 },
{ 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0,
0, 4 },
{ 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4,
4, 4 }
};
private int mapbeijing2[][] = {
{ 1, 1, 1, 1, 1, 1 },
{ 3, 3, 3, 3, 3, 3 },
{ 3, 4, 3, 3, 3, 3 },
{ 3, 3, 3, 3, 3, 3 },
{ 3, 3, 3, 3, 3, 3 },
{ 3, 3, 3, 3, 4, 3 },
{ 3, 3, 3, 3, 3, 3 }
};
private int mapbeijing3 [][] = {
{ 1, 1, 1, 1, 1, 1 },
{ 3, 2, 3, 3, 3, 3 },
{ 3, 3, 3, 3, 3, 3 },
{ 4, 4, 4, 4, 4, 4 },
{ 3, 3, 3, 3, 3, 3 },
{ 3, 3, 3, 3, 3, 3 },
{ 3, 3, 3, 3, 3, 3 }
};
private int mapbeijing4[][]= {
{ 1, 2, 1, 2, 1, 2 },
{ 1, 2, 1, 2, 1, 2 },
{ 1, 2, 1, 2, 1, 2 },
{ 1, 2, 1, 2, 1, 2 },
{ 1, 2, 1, 2, 1, 2 },
{ 1, 2, 1, 2, 1, 2 },
{ 1, 2, 1, 2, 1, 2 }
};
long StartGameTime = 0;
long EedGameTime = 0;
long em = 0,mm = 0;
//创建并得到一个游戏画面的对象
synchronized public static Displayable getInstance()
{
instance = null;
if(instance == null)
instance = new MyGame();
return instance;
}
protected MyGame()
{
super(false);
setFullScreenMode(true);
stage = 2;
score = 0;
GetXue = 2;
i = 1;
GetShengming = 3;
isUandD = false;
isL = true;
isR = true;
isU = false;
isD = false;
isDown = true;
isJump = false;
isOK = false;
isKey = false;
islose = false;
isEndGame = false;
flag = true;
ispause = false;
isTime = true;
isMOV = true;
try
{
caidan = Image.createImage("/res/caidan.png");
}
catch(Exception e)
{}
Thread t = new Thread(this);
t.start();
}
protected void initGame()
{
switch (stage)
{
case 2:
isOK = false;
tlbeijing = null;
lm = null;
tlqiang = null;
pp = null;
splouti = null;
spjian = null;
spjian1 = null;
spjian2 = null;
beijing = null;
spguai = null;
spDownRock = null;
spmen = null;
spjiguan = null;
spgunR1 = null;
spgunR2 = null;
spgunD1 = null;
player = null;
sprock = null;
spguai1 = null;
spjiguan1 = null;
spbaozhang = null;
spxin = null;
spxue = null;
spshengming = null;
sptou = null;
tou = null;
xue = null;
shengming = null;
map = null;
break;
case 3:
isOK = false;
tlbeijing = null;
lm = null;
beijing = null;
tlqiang = null;
pp = null;
player = null;
spDownRock = null;
spjiguan = null;
spguai = null;
spbaozhang = null;
spgunD1 = null;
spjian = null;
splouti = null;
sprock = null;//代替splouti2
spguai1 = null;
spjiguan1 = null;//代替spbaozhang1
spgunR1 = null;//代替spyaosi1
spgunR2 = null;//代替spyaosi2
spmen = null;
spjian1 = null;
spjian2 = null;
spzhangai = null;
spxue = null;
spshengming = null;
sptou = null;
tou = null;
xue = null;
shengming = null;
map = null;
break;
case 4:
isOK = false;
tlbeijing = null;
lm = null;
tlqiang = null;
pp = null;
player = null;
beijing = null;
spjiguan = null;
spguai = null;
spjian3 = null;
splouti1 = null;
splouti2 = null;
spbaozhang1 = null;
spbaozhang = null;
spgunD1 = null;
spjiguan2 = null;
spjian = null;
spguai2 = null;
splouti = null;
sprock = null;//代替spDownRock2
spguai1 = null;
spjiguan1 = null;
spgunR1 = null;
spmen = null;
spjian1 = null;
spjian2 = null;
spzhangai = null;
spxue = null;
spshengming = null;
sptou = null;
tou = null;
xue = null;
shengming = null;
map = null;
break;
}
}
public void changeXue()
{
// isStatr = true;
GamePlayer.isOK = true;
if (spxue.getFrame() == 2)
{
spxue.setFrame(1);
}
else if (spxue.getFrame() == 1)
{
spxue.setFrame(0);
}
else if (spxue.getFrame() == 0)
{
spxue.setFrame(2);
changeShengming();
switch(stage)
{
case 2:
splouti.isOK = false;
isDown = true;
isU = false;
isD = false;
isL = true;
isR = true;
break;
case 3:
splouti.isOK = false;
sprock.isOK = false;
isDown = true;
isU = false;
isD = false;
isL = true;
isR = true;
break;
case 4:
splouti.isOK = false;
splouti1.isOK = false;
splouti2.isOK = false;
isDown = true;
isU = false;
isD = false;
isL = true;
isR = true;
break;
}
}
}
public void changeShengming()
{
GamePlayer.isDie = true;
if (spshengming.getFrame() == 9)
{
spshengming.setFrame(8);
spxue.setFrame(2);
}
else if (spshengming.getFrame() == 8)
{
spshengming.setFrame(7);
spxue.setFrame(2);
}
else if (spshengming.getFrame() == 7)
{
spshengming.setFrame(6);
spxue.setFrame(2);
}
else if (spshengming.getFrame() == 6)
{
spshengming.setFrame(5);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -