📄 main.java
字号:
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextBox;
public class Main extends Canvas implements Runnable
{
Image offBufferImage = Image.createImage(176,208);
Graphics bg = offBufferImage.getGraphics();
//-------------------State-----------------------//
private final byte StateLogo = 0;
static final byte Loading = 1; //所有LOAD状态
private final byte LoadLogo = 16;
static final byte LoadMenuData = 11; //读取菜单
static final byte LoadMainData = 3; //读取MAIN
private final byte FreeLogo = 17;
private final byte FreeMenuData = 14;
private final byte FreeMainData = 15;
private final byte LoadEnd = 13;
public static final byte StateMenu = 4;
private final byte StateHelp = 5;
public static final byte StateSet = 6;
private final byte StateMain = 7;
private final byte StateWin = 8;
private final byte StateOver = 9;
public final byte StatePause = 10;
public static final byte StateTop = 12;
public static byte FlowState;
public static byte LoadState; //当前读取状态
private static byte FreeState; //当前释放状态
public static byte NpcState;
//-----------------------Role--------------------------//
int RoleState;
private final byte RoleL = 0;
private final byte RoleLU = 1;
private final byte Roleforward = 2;
private final byte RoleRU = 3;
private final byte RoleR = 4;
private final byte RoleFall = 5;
private final byte RoleFall2 = 6;
private final byte RoleJump = 7;
private final byte RoleReady = 8;
//-------------------------Key---------------------------//
private final byte UP = -1;
private final byte DOWN = -2;
private final byte LEFT = -3;
private final byte RIGHT = -4;
private final byte Enter = -5;
private final byte LeftCom = -6;
private final byte RightCom = -7;
private final byte Num_0 = 48;//数字键0
private final byte Num_1 = 49;//数字键1
private final byte Num_2 = 50;//数字键2
private final byte Num_3 = 51;//数字键3
private final byte Num_4 = 52;//数字键4
private final byte Num_5 = 53;//数字键5
private final byte Num_6 = 54;//数字键6
private final byte Num_7 = 55;//数字键7
private final byte Num_8 = 56;//数字键8
private final byte Num_9 = 57;//数字键9
private int Key = 1;
private boolean keyLock;
//----------------------Res-----------------------------//
private Image img_Logo1;
private Image img_Logo2;
private Image img_Menu,img_MenuStr;
private Image img_temp;
private Image img_jiemian;
private Image img_GameJM;
private Image img_GameOver;
private Image img_distance;
private Image img_Pause,img_choose;
private Image img_Npc;
private Image img_run;
private Image img_speed;
private Image img_Jump;
private Image img_RGB;
//----------------------GameData------------------------//
private Ripple ripple;
public ScnInputName scnInputName;
Random random;
//----------------------游戏运行变量-------------------------//
public static int ScrWidth = 176;
public static int ScrHeight = 208;
private int LogoTime;
private int SystemTime;
private int GameTime;
private boolean Runing;
private int MenuSdlcd; //菜单选项
private byte Menustate1; //菜单状态
private boolean LoadOver;
////////////////////////////////
private byte Lv ; //关数
private int distanceX; //距离
public int Speed; //人物速度
private int MaxSpeed; //最大人物速度
private int SpeedStat; //速度条
private int WindowsX,WindowsY,tempWindowsX; //可视范围
boolean fall; //跌到状态
public boolean Hit; //碰撞状态
public boolean Fly; //跳起状态
public boolean clarity; //无敌状态
private boolean powerSpeed; //速度条暴气
public static int score; //积分牌
boolean InputName; //能否输入名字
public int npcInfo[][]; //NPC数组
private int Npc_num; //第几个 NPC
private int JumpNum5; //被缠计算器
private int clarityTime; //无敌计算器
private int muscleTime; //muscle计算器
private int shakeNum; //震动计算器
//----------------------游戏对象或相关----------------------------//
private MID mid;
private Thread thread;
private Font ft;
private int FontWidth;
private Random r;
public sprite sp_role;
public sprite sp_MenuStr; //开始菜单选项
private sprite sp_state; //左上角状态
private sprite sp_Speed; //速度POW
private sprite sp_jump; //狂按5
private sprite sp_muscle; //肌肉
private sprite sp_end; //过关结局
// private int endFrame[]= ;
private MapInfo mapinfo;
public tiledLayer tl_way;
//0:左 1 :左上 2:前进 3:右上 4:右 5:跌倒 6跌到2: 7:跳 8 9: 起跑
private int RoleFrame[][] = {{0,1},{2,3},{4,5},{6,7},{8,9},
{10,10,10,11,11,11,11,11,11,11,11,11},{12,12,12,13,13,13,13,13,13,13,13,13},{14,14,14,15,15,15,15,15,15},
{16,18,16,18,16,18,20,21,19,17,19,17,19}};
private int roleW = 34,roleH = 48;
public layerManager lm;
public Vector npcVector;
private NumImgTools NumTools;
private String strName = "";
public Main(MID mid)
{
setFullScreenMode(true);
this.mid = mid;
r = new Random();
ft = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_MEDIUM);
FontWidth = ft.stringWidth("一");
FlowState = Loading;
LoadState = LoadLogo;
Start();
}
public void Start()
{
if(thread==null)
{
thread = new Thread(this);
Runing = true;
thread.start();
}
}
private void DrawMain(Graphics g)
{
// Runtime rt = Runtime.getRuntime();
SetState();
//-----------------设置窗口-------------------------//
lm.setViewWindow(WindowsX, WindowsY, ScrWidth,ScrHeight);
lm.paint(g, 0, 0);
//-----------------速度条---------------------------//
int tempSpeed = 40/MaxSpeed*Speed;
if(SpeedStat<tempSpeed)
{
SpeedStat++;
sp_Speed.setFrame(0);
powerSpeed = false;
}
else if(SpeedStat>tempSpeed)
{
SpeedStat=tempSpeed;
powerSpeed = false;
}
else if(SpeedStat >= sp_Speed.getWidth())
{
sp_Speed.setFrame(SystemTime%3+1);
powerSpeed = true;
}
if(!powerSpeed)
{
img_RGB = Image.createImage(39, 35);
Graphics rgbG = img_RGB.getGraphics();
int rgb[] = new int[39*35];
rgbG.setColor(0,0,0);
rgbG.fillRect(0, 0, 39, 35);
rgbG.setColor(255,255,0);
rgbG.fillRect(0, 0, SpeedStat-2, sp_Speed.getHeight()-6);
sp_Speed.setPosition(0, -20);
sp_Speed.paint(rgbG);
img_RGB.getRGB(rgb, 0, 39, 0, 0, 39, 35);
img_RGB =null;
for(int i=0;i<39*35;i++)
{
if(rgb[i]==0xff000000)
{
rgb[i]=0x00000000;
}
}
g.drawRGB(rgb, 0, 39, 2, 193, 39, 35, true);
}
else if(powerSpeed)
{
sp_Speed.setPosition(2, 173);
sp_Speed.paint(g);
}
g.drawImage(img_GameJM, 0, 0, 0);
g.drawImage(img_distance,90+(-distanceX),0,0);
g.setColor(0,0,0);
g.drawString(""+score, sp_muscle.x+20, sp_muscle.y-2, 0);
NumTools.drawNums(g, GameTime, 50, 0, 2);
sp_state.paint(g);
sp_muscle.paint(g);
if(fall && NpcState == NPC.tree)
{
sp_jump.nextFrame();
sp_jump.paint(g);
}
}
private void SetState()
{
switch (RoleState)
{
case RoleL:
break;
case RoleLU:
break;
case Roleforward:
break;
case RoleRU:
break;
case RoleR:
break;
case RoleFall:
case RoleFall2:
shake();
if(Speed>0) //速度递减
{
Speed--;
}
if(SystemTime>RoleFrame[RoleState].length)
{
Hit = false;
fall= false;
RoleState = Roleforward;
SystemTime = 0;
WindowsX = tempWindowsX;
clarity = true;
}
break;
case RoleJump:
if(sp_role.y>50)
{
sp_role.move(0, -2);
}
else
sp_role.setPosition(sp_role.x, 50);
if(SystemTime>RoleFrame[RoleState].length)
{
RoleState = Roleforward;
SystemTime = 0;
Fly = false;
keyLock = false;
Speed /=2;
}
break;
case RoleReady:
if(SystemTime >RoleFrame[RoleState].length-1)
{
RoleState = Roleforward;
SystemTime = 0;
keyLock = false;
}
break;
}
sp_role.setFrame(RoleFrame[RoleState][SystemTime%RoleFrame[RoleState].length]);
}
private void GameLogic()//游戏逻辑
{
SystemTime++;
if(RoleState!=RoleReady)
{
//--------------游戏时间---------------------//
if(SystemTime%10==0)
GameTime--;
//-------------游戏加速----------------------//
if(Speed<MaxSpeed && !Hit && !fall && !Fly && SystemTime%5==0)
{
Speed++;
}
//------------------状态条-------------------//
if(!fall)
{
sp_state.setFrame(SystemTime%2);
}
else if(fall)
sp_state.setFrame(SystemTime%3+2);
//---------------------logic ---------------------//
if(Npc_num<npcInfo.length)
{
if(tl_way.getY()<npcInfo[Npc_num][3])
{
NPC tmp = new NPC(img_Npc,npcInfo[Npc_num][0],npcInfo[Npc_num][1],npcInfo[Npc_num][2],npcInfo[Npc_num][4],0,this);
npcVector.addElement(tmp);
lm.insert(tmp,lm.getSize()-1);
Npc_num++;
tmp = null;
}
}//end ////////////////
if(!npcVector.isEmpty())
{
for(int i=0;i<npcVector.size();i++)
{
NPC tmp=(NPC)npcVector.elementAt(i);
tmp.move(Speed);
tmp.isCollision(tmp.boundX,tmp.boundY);
tmp=null;
}
}//end NPC移动
}
//---------------------------无敌状态------------------------------//
if(clarity )
{
clarityTime++;
if(clarityTime%3==0)
{
sp_role.setVisible(true);
}
else
sp_role.setVisible(false);
if(clarityTime>31)
{
sp_role.setVisible(true);
clarity = false;
clarityTime = 0;
}
}
//-------------------------muscle状态-------------------------------//
if(muscleTime>0)
{
muscleTime--;
sp_muscle.setFrame(muscleTime%2);
}
else
{
muscleTime = 0;
sp_muscle.setFrame(0);
}
//----------------------------碰撞后设置状态------------------------//
if(Hit)
{
switch (NpcState)
{
case NPC.Fish:
score+=30;
Hit = false;
muscleTime = 10;
break;
case NPC.drug:
case NPC.fruit:
score+=40;
Hit = false;
muscleTime = 10;
break;
// /-------缠住角色无法移动---------//
case NPC.tree:
fall = true;
powerSpeed = false;
SpeedStat--;
Speed = 0;
if(score>10)
score-=1;
else
score=0;
if(JumpNum5<=0)
{
JumpNum5 = 10;
RoleState = Roleforward;
NpcState = 30;
Hit = false;
fall= false;
}
break;
//-------------------------初始化移动速度----------------//
case NPC.conch :
case NPC.block:
case NPC.crad:
SystemTime = 0;
powerSpeed = false;
tempWindowsX = WindowsX;
if(SpeedStat>0)
SpeedStat-=5;
if(!fall)
{
if((r.nextInt()>>>1)%2==0)
{
RoleState = RoleFall;
}
else
{
RoleState = RoleFall2;
}
}
fall = true;
Hit = false;
break;
//---------------------------速度递减-------------------//
case NPC.tussock:
if(NPC.NpcState == NPC.tussock && Speed>1)
{
Speed--;
}
else
{
Hit = false;
NPC.NpcState = 30;
}
break;
}
}
//-------------------判断输赢---------------------//
if(distanceX<-70)
{
if(Lv<2)
{
NextLv();
}
else
{
FlowState = Loading;
LoadState = LoadEnd;
}
}
else if(GameTime == 0)
{
FlowState = StateOver;
}
//---------------------地图滚动-------------------//
tl_way.move(0, -Speed);
distanceX = ((-tl_way.getHeight()+tl_way.getY()))*100/tl_way.getHeight();
}
private void InitGame()
{
switch (Lv)
{
case 0:
GameTime = 100;
break;
case 1:
GameTime = 80;
break;
case 2:
GameTime = 60;
break;
}
RoleState = RoleReady;
MenuSdlcd = 0;
Speed = 0;
JumpNum5 = 10;
fall = false;
LoadOver = false;
powerSpeed = false;
InputName = false;
Fly = false;
clarity = false;
keyLock = true;
Npc_num = 0;
MaxSpeed = 10;
mapinfo = new MapInfo(this,Lv);
mapinfo = null;
npcVector.removeAllElements();
NcpInfo npc = new NcpInfo(this,Lv);
npc = null;
SystemTime = 0;
WindowsX = 20;
distanceX = (tl_way.getHeight()+tl_way.getY())*100/tl_way.getHeight();
sp_role.setPosition(90, 80);
sp_role.setFrame(16);
tl_way.setPosition(0,(tl_way.getHeight()-ScrHeight));
}
private void Load()
{
switch (LoadState)
{
case LoadLogo:
if(!LoadOver)
{
try {
img_Logo1 = Image.createImage("/pic/logo.png");
} catch (IOException e) {
e.printStackTrace();
}
if(ripple == null)
{
ripple = new Ripple();
}
LoadOver = true;
}
break;
case LoadMenuData:
if(!LoadOver)
{
try {
img_Menu = Image.createImage("/pic/cover.png");
img_MenuStr = Image.createImage("/pic/MenuStr.png");
} catch (IOException e) {
e.printStackTrace();
}
sp_MenuStr = new sprite(img_MenuStr,45,16,0);
sp_MenuStr.setFrame(0);
sp_MenuStr.setPosition(80, 160);
MenuSdlcd = 0;
if(scnInputName==null)
scnInputName = new ScnInputName(mid);
Lv = 0;
LoadOver = true;
}
break;
case LoadMainData:
if(!LoadOver)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -