文件名称:j2me手机游戏代码
文件大小:69KB
文件格式:JAVA
更新时间:2012-09-03 05:14:17
swordCanvas , java,Graphics
import javax.microedition.lcdui.*;
import java.io.*;
import java.lang.*;
import java.util.Timer;
import popup.*;
import java.util.Random;
import java.util.Vector;
import javax.microedition.media.*;
public class SwordCanvas extends Canvas implements PopupListener
{
static int life,energy,skillpoint,x,y,maxlife,maxenergy;
static int level;
static int experience;
static int gold;
static int direction;
static int basicPower;
int totalPower;
int[] skillArray,tn;
int addMagic,addPower;
int stealB;
int addPdP;
int curNPC;
int speed=4;
int count;
int[] weapon;
int curKey;
boolean isUpdated;
static int[] startX,startY;
int curweapon;
int curEl;
boolean isTask,isTask2;
boolean hasBoat;
boolean isHiding;
boolean isGameOver;
boolean tran;
boolean isWalking;
boolean[] dlg;
String[] dlgMenu;
boolean isWin,isDie;
String strTip;
String strDlg;
String strPro;
String[] attackMenu=new String[]{
"落日","乱电", "残风", "召狼", "召熊", "召龙",
"隐身","遁走", "附身", "回血", "补气", "攻击"};
int offY,offX;
int dlgn;
int dlgMenuChoose;
static Image imgHero;
static int w,h;
static int x0,y0,sl=0,i,j;
int killCount;
static int[] W,H;
int[] mapArray;
int[] spriteNum;
static int[] screenStart=new int[]{16,29,33,47,68,77,83};
int sx,sy;
int sn;
Sprite[] SPI;
Sprite[] NPC;
int attackNumber;
int nn;
boolean isProMenu,isAttMenu,isTranMenu;
boolean isFighting;
boolean isTalking;
boolean isChange;
boolean isGameMenu;
int step;
static boolean running;
String[] dlgM;
static Image screenPic;
static Image imgNPC;
static Image imgProBG,imgState;
InputStream is;
Vector winString=new Vector(0,1);
//static Image[] p;
static Image[] screen;
static Image[] spic;
static Image atpic,imgAttackBG;
static Image spic0;
Image imgDlg;
static Image[] sword;
String[] mapName=new String[]{"牛家村","冰火岛 ","大智岛","襄阳城","蓬莱岛","竞技场"};
boolean updateMan;
static Graphics g1;
Random rand;
Player mapPlayer,attPlayer;
boolean isWeather;
int offsetY=36;
Timer timer;
int[][] snow=new int[30][30];
TimerTask moveTask,attackTask,attackControlTask,changeMapTask,weatherTask;
protected Popup popup;
protected String helpStr =
"游戏背景:"+ "\n\n" +
"高手是寂寞的;而磨砺自身成为高手的过程,则是孤独的。"+ "\n\n" +
"操作方法:"+ "\n\n" +
"游戏中用方向键和2、4、6、8键控制主角移动以及进行菜单选择,5键或OK键选定,3键调出/关闭属性菜单,左软键调出操作菜单,可查看帮助信息,保存游戏或回到主菜单。"+ "\n\n" +
"关键提示:"+ "\n\n" +
"玩家每次升级将得到一点技能属性用来学习技能,每项技能所能修炼的最高等级受主角等级限制,技能等级越高,效果越显著。使用“召狼”等技能时,召唤出的宠物将被优先攻击。使用技能会消耗主角的“气力”,“回气”技能将耗费一定数量的金钱。主角战死会损失一定比例的经验及金钱。"+ "\n\n" +
"在战斗过程中菜单选项将不会响应。"+ "\n\n" +
"招式详解:"+ "\n\n" +
"落日:用内力聚成一个落日,对单个敌人造成极大伤害。"+ "\n\n" +
"乱电:在天空中形成流星般的巨石,砸向全部敌人。"+ "\n\n" +
"残风:耗费自身的血和气来形成一股飓风,对全部敌人造成较大伤害。"+ "\n\n" +
"召狼:召唤精灵狼来辅助战斗,生命力较弱但攻击力很强。"+ "\n\n" +
"召熊:召唤精灵熊来辅助战斗,攻击力很弱但生命力较强,可以用作战斗中的护盾。"+ "\n\n" +
"召龙:召唤神龙来辅助战斗,可以反弹敌人部分攻击并反伤敌人。"+ "\n\n" +
"隐身:在数回合内主角将不受攻击,但使用附身或攻击敌人则会失效。"+ "\n\n" +
"遁走:从战斗中直接逃走,技能等级高则逃走成功率大。"+ "\n\n" +
"附身:召唤影子与主角并肩作战,使主角生命力和攻击力按比例上升。"+ "\n\n" +
"回血:恢复主角一定比例的生命力。"+ "\n\n" +
"回气:恢复主角一定比例的气力,但要耗费一定量的金钱。";
public SwordCanvas()
{
setFullScreenMode(true);
running=true;
w = getWidth();
h= getHeight()-offsetY;
}
void init()
{
isGameOver = false;
i=0;
level=1;
experience=0;
skillpoint=0;
basicPower=7;
gold=0;
maxlife=71;
life=71;
energy=40;
maxenergy=40;
direction=6;
addMagic=0;
addPower=0;
stealB=0;
addPdP=0;
curweapon=9;
totalPower=basicPower+addPower+addPdP*basicPower/100;
weapon=new int[]{9,9,9,9,9,9};
skillArray=new int[]{0,0,0,0,0,0,0,0,0,1,0};
spriteNum=new int[]{60,28,153,76,16,2,0};
startX=new int[]{464,144,32,192,32,128,16};
startY=new int[]{448,432,224,320,144,176,112};
dlg=new boolean[]{false,false,false,false,false,false,false};
tn=new int[]{0,0,0,0,0};
W=new int[]{32,32,32,24,24,16,16};
H=new int[]{32,32,32,24,24,18,18};
if(sl==0)
{
if(dlg[0])
{
x=startX[0];
y=startY[0];
message(2);
}
else
{
x=384;
y=304;
}
}
else
{
x=startX[sl];
y=startY[sl];
}
if(x>=w/2)
{
if(x<=W[sl]*16-w/2)
x0=x-w/2;
else
x0=W[sl]*16-w;
}
else
x0=0;
if(y>=h/2)
{
if(y<=H[sl]*16-h/2)
y0=y-h/2;
else
y0=H[sl]*16-h;
}
else
y0=0;
isTask=false;
isTask2=false;
hasBoat=false;
isDie=false;
isWin=false;
isWalking=false;
isHiding=false;
tran=false;
isProMenu=false;
isAttMenu=false;
isFighting=false;
isChange=false;
isGameMenu=false;
isTalking=false;
strTip=null;
strDlg=null;
SPI=new Sprite[8];
timer=new Timer();
rand=new Random(1000);
changeMapTask=null;
is=getClass().getResourceAsStream("/Screen"+toString(sl)+".dat");
readMap(is);
if(spriteNum[sl]!=0)
{
is=getClass().getResourceAsStream("/sprite"+toString(sl)+".dat");
readSprite(is);
}
if(sl==0&&!dlg[0])
message(2);
weatherTask=new TimerTask(this,-2);
timer.schedule(weatherTask,0,400);
}
void readSprite(InputStream s)
{
int i,c,temp;
i=0;
c=0;
temp=0;
NPC=null;
NPC=new Sprite[spriteNum[sl]];
try
{
while((c=s.read())!=-1)
{
switch(c)
{
case ',':
NPC[i++]=new Sprite(
temp/10000,
temp%10000/100,
temp%100);
temp=0;
break;
default:
temp=10*temp+c-48;
break;
}
}
}
catch(java.io.IOException ioe)
{
}
is=null;
}
void readMap(InputStream s)
{
int x,y,c,temp;
x=0;
y=0;
temp=0;
mapArray=null;
mapArray=new int[W[sl]*H[sl]];
int i = 0;
try
{
while((c=s.read())!=-1)
{
switch(c)
{
case '\n':
y++;
x=0;
temp=0;
break;
case ',':
mapArray[y*W[sl]+x++]=temp;
temp=0;
break;
default:
temp=10*temp+c-48;
break;
}
}
}
catch(java.io.IOException ioe)
{
}
if(sl==3)
{
x=getRandom(96)+72;
if(mapArray[x]/100==1)
{
mapArray[x]=mapArray[x]%100+9800;
{
if(mapArray[x+1]/100==1)
mapArray[x+1]=mapArray[x+1]%100+1800;
if(mapArray[x-1]/100==1)
mapArray[x-1]=mapArray[x-1]%100+1900;
if(mapArray[x-24]/100==1)
mapArray[x-24]=mapArray[x-24]%100+1600;
if(mapArray[x+24]/100==1)
mapArray[x+24]=mapArray[x+24]%100+1700;
}
}
x=getRandom(96)+264;
if(mapArray[x]/100==1)
{
mapArray[x]=mapArray[x]%100+9800;
{
if(mapArray[x+1]/100==1)
mapArray[x+1]=mapArray[x+1]%100+1800;
if(mapArray[x-1]/100==1)
mapArray[x-1]=mapArray[x-1]%100+1900;
if(mapArray[x-24]/100==1)
mapArray[x-24]=mapArray[x-24]%100+1600;
if(mapArray[x+24]/100==1)
mapArray[x+24]=mapArray[x+24]%100+1700;
}
}
}
else
if(sl==6)
{
for(int ii=0;ii<256;ii++)
{
if(mapArray[ii]/100==1)
mapArray[ii]=mapArray[ii]%100+(getRandom(100)<70?(getRandom(3)+16)*100:100);
}
}
is=null;
}
public void keyRepeated(int keyCode)
{
int action=getGameAction(keyCode);
if(action==Canvas.UP||action==Canvas.DOWN||action==Canvas.LEFT||action==Canvas.RIGHT)
keyPressed(keyCode);
}
public void keyPressed(int keyCode)
{
int action=getGameAction(keyCode);
if (popup!=null&&popup.isActive())
{
popup.keyPressed(keyCode, action);
repaint();
return;
}
if(keyCode==35)
MenuCanvas.midlet.exit();
curKey=keyCode;
if(!isChange)
{
switch(action)
{
case GAME_B:
if(!isWin&&!isDie&&!isChange&&!isTranMenu&&!isTalking&&!isGameMenu&&moveTask==null)
{
isProMenu=!isProMenu;
offY=0;
offX=0;
i=0;
j=0;
repaint();
}
break;
case UP:
if(isGameMenu)
{
i=i-1>=0?i-1:0;
repaint();
}
else
if(!isProMenu&&!isTranMenu&&!isWin&&!isDie&&!isTalking&&strTip==null&&!isFighting)
{
if(moveTask==null)
{
if(direction/3!=2)
{
direction=6;
repaint();
}
else
setMoveTask();
}
}
else
if(isProMenu)
{
strPro=null;
offY=0;
j=0;
i=i-1>=0?i-1:3;
repaint();
}
else
if(isTranMenu)
{
if(dlg[2])
i=i-1>=0?i-1:5;
else
if(isTask)
i=i-1>=0?i-1:4;
else
i=i-1>=0?i-1:3;
repaint(0,0,128,128);
}
else
if(isAttMenu)
{
i=i-4>=0?i-4:i+8;
repaint();
}
else
if(isTalking&&strTip==null)//对对话菜单的操作
{
if(curNPC==6||curNPC==4||curNPC==5)
dlgMenuChoose=dlgMenuChoose-1>=0?dlgMenuChoose-1:dlgMenu.length-1;
repaint();
}
break;
case DOWN://下键
if(isGameMenu)
{
i=i+1<=4?i+1:4;
repaint();
}
else
if(!isProMenu&&!isFighting&&!isTranMenu&&!isWin&&!isDie&&!isTalking&&strTip==null)
{
if(moveTask==null)
{
if(direction/3!=1)
{
direction=3;
repaint();
}
else
setMoveTask();
}
}
else
if(isProMenu)
{
strPro=null;
offY=0;
i=i+1<4?i+1:0;
j=0;
repaint();
}
else
if(isTranMenu)
{
if(dlg[2])
i=i+1<6?i+1:0;
else
if(isTask)
i=i+1<5?i+1:0;
else
i=i+1<4?i+1:0;
repaint();
}
else
if(isAttMenu)
{
i=i+4<12?i+4:i-8;
repaint();
}
else
if(isTalking&&strTip==null)//对话时候的菜单选项
{
if(curNPC==6||curNPC==4||curNPC==5)
dlgMenuChoose=dlgMenuChoose+1<=dlgMenu.length-1?dlgMenuChoose+1:0;
repaint();
}
break;
case LEFT://左键
if(isProMenu)
{
if(i==2)
j=j-1>=0?j-1:10;
else
if(i==1&&j>0)
for(int ii=j-1;ii>=0;ii--)
{
if(weapon[ii]!=9)
{
offY=0;
j=ii;
break;
}
}
repaint();
}
else
if(!isProMenu&&!isFighting&&!isTranMenu&&!isWin&&!isDie&&!isTalking&&strTip==null&&!isGameMenu)
{
if(moveTask==null)
{
if(direction/3!=0)
{
direction=0;
repaint();
}
else
setMoveTask();
}
}
else
if(isAttMenu)
{
i=i-1>=0?i-1:11;
repaint();
}
else
if(isTalking&&strTip==null)
{
if(curNPC==6||curNPC==4||curNPC==5)
i=i-1>=0?i-1:0;
repaint();
}
break;
case RIGHT://右键
if(isProMenu)
{
if(i==2)
{
j=j+1<11?j+1:0;
repaint();
}
else
if(i==1)
{
for(int ii=j+1;ii<6;ii++)
{
if(weapon[ii]!=9)
{
offY=0;
j=ii;
break;
}
}
repaint();
}
}
else
if(!isProMenu&&!isTranMenu&&!isFighting&&!isWin&&!isDie&&!isTalking&&strTip==null&&!isGameMenu)
{
if(moveTask==null)
{
if(direction/3!=3)
{
direction=9;
repaint();
}
else
setMoveTask();
}
}
else
if(isAttMenu)
{
i=i+1<=11?i+1:0;
repaint();
}
else
if(isTalking&&strTip==null)//对话时候的菜单选项
{
if(curNPC==6||curNPC==4)
i=i+1<=1?i+1:0;
else
if(curNPC==5)
{
switch(sn)
{
case 1:
i=i+1<=2?i+1:0;
break;
case 2:
i=i+1<=1?i+1:0;
break;
default:
break;
}
}
repaint();
}
break;
case FIRE:
FIRE();
break;
default:
switch(keyCode)
{
case KEY_NUM1://1键取消
// if(isAttMenu&&!isProMenu)
// {
// isAttMenu=false;
// attackNumber=4;
// repaint();
// }
// else
if(isProMenu&&i==2)
{
gold+=(weapon[j]%3+1)*(weapon[j]%3+1)*100;
if(curweapon==weapon[j])
curweapon=9;
weapon[j]=9;
addPower=0;
addMagic=0;
addPdP=0;
totalPower=basicPower;
repaint(0,0,240,309);
}
break;
case -6:
FIRE();
break;
default:
break;
}
break;
}
}
}
public void FIRE()
{
if(isGameOver){
MenuCanvas.loadResource();
SwordMIDlet.show(SwordMIDlet.menuCanvas);
MenuCanvas.se.stopSound(mapPlayer);
}
if(isGameMenu)
{
if(i==4)
{
isGameMenu=!isGameMenu;
repaint();
}
else
if(i==3)
{
SwordStore store=new SwordStore();
store.save();
store=null;
isGameMenu=false;
screen=null;
MenuCanvas.loadResource();
SwordMIDlet.show(SwordMIDlet.menuCanvas);
MenuCanvas.se.stopSound(mapPlayer);
}
else
if(i==0)
{if(popup == null)popup = new Popup();
showPopup(helpStr.toCharArray(), Popup.ALT_OK, 0, 0, 0);
}
else
if(i==2)
{
MenuCanvas.isAudioOn=!MenuCanvas.isAudioOn;
if(MenuCanvas.isAudioOn)
MenuCanvas.se.playSound(mapPlayer);
else
MenuCanvas.se.pauseSound(mapPlayer);
isGameMenu=false;
repaint();
}
else
{
SwordStore store=new SwordStore();
store.save();
store=null;
isGameMenu=false;
repaint();
}
}
else
if(isProMenu)
{
if(i==2&&skillpoint>0&&skillArray[j]<10)
{
if(skillArray[j]==0||level>=3*skillArray[j])
{
if(j==9||j==10||j==7)
{
if(skillArray[j]<5)
{
skillArray[j]++;
skillpoint--;
}
}
else
{
skillArray[j]++;
skillpoint--;
}
}
repaint(0,0,160,128);
}
else
if(i==3)
{
if(weapon[j]!=9)
{
curweapon=weapon[j];
addMagic=0;
addPower=0;
addPdP=0;
switch(curweapon)
{
case 0:
addPower=5;
stealB=10;
break;
case 1:
addPower=10;
stealB=25;
break;
case 2:
addPower=15;
stealB=45;
break;
case 3:
addMagic=10;
break;
case 4:
addMagic=20;
break;
case 5:
addMagic=30;
break;
case 6:
addPdP=15;
break;
case 7:
addPdP=25;
break;
case 8:
addPdP=35;
break;
default:
break;
}
totalPower=basicPower+addPower+addPdP*basicPower/100;
if(isFighting)
SPI[0].basicPower=totalPower;
repaint();
}
}
}
else
if(isTranMenu)
{
if(sl!=i)
{
if(sl!=0)
{
if(gold>=100)
{
gold-=100;
changeMap(i);
}
else
{
strTip="银两不够";
isTranMenu=false;
repaint();
}
}
else
changeMap(i);
}
else
{
isTranMenu=false;
repaint();
}
}
else
if(isAttMenu)
{
if((i<11&&skillArray[i]>0)||i==11)
setAttack(i);
}
else
if(strTip!=null)
{
strTip=null;
strDlg=null;
imgDlg=null;
repaint();
}
else
if(isTalking)
{
if(curNPC<2)
{
if(sn==dlgM.length-1)
{
isTalking=false;
dlg[curNPC]=true;
imgDlg=null;
}
else
{
sn++;
createDialogImage(dlgM[sn]);
}
repaint();
}
else
switch(curNPC)
{
case 2:
if(sn==2)
{
dlg[curNPC]=true;
imgDlg=null;
isTalking=false;
changeMap(5);
}
else
{
sn++;
createDialogImage(dlgM[sn]);
repaint();
}
break;
case 3:
if(sn==5)
{
gold+=10000;
isTask2=true;
sn++;
}
else
if(sn==6||sn==7)
{
//removeCommand(con);
isTalking=false;
dlg[curNPC]=true;
imgDlg=null;
}
else
{
sn++;
createDialogImage(dlgM[sn]);
}
repaint();
break;
case 6://船工
if(dlgMenuChoose==0)
{
if(hasBoat)
{
strTip="你已经有船了";
isTalking=false;
}
else
if(gold>=600)
{
gold-=600;
hasBoat=true;
isTalking=false;
strTip="买卖成功";
}
else
{
strTip="银两不够";
isTalking=false;
}
}
else
{
isTalking=false;
imgDlg=null;
}
repaint();
break;
case 5://洪七公
switch(sn)
{
case 0:
if(dlgMenuChoose==0)
{
sn=1;
dlgMenu=new String[2];
dlgMenu[0]="现在分配";
dlgMenu[1]="以后再说";
}
else
if(dlgMenuChoose==1)
{
dlgMenu=null;
isTask=true;
sn=2;
}
else
{
isTalking=false;
imgDlg=null;
return;
}
createDialogImage(dlgM[sn]);
repaint();
break;
case 2:
{
isTalking=false;
imgDlg=null;
//removeCommand(con);
}
repaint();
break;
case 1:
if(dlgMenuChoose==0)
{
if(gold>=10000)
{
gold-=10000;
for(int ii=0;ii<11;ii++)
{
while(skillArray[ii]>0)
{
skillArray[ii]--;
skillpoint++;
}
}
isTalking=false;
imgDlg=null;
//removeCommand(con);
}
else
{
strTip="银两不够";
isTalking=false;
}
}
else
{
isTalking=false;
imgDlg=null;
}
repaint();
break;
default:
break;
}
break;
case 4://竞技官
if(dlgMenuChoose==0)
{
if(gold>=350)
{
gold-=350;
isTalking=false;
imgDlg=null;
changeMap(6);//进入竞技场
killCount=0;
}
else
{
strTip="银两不够";
isTalking=false;
}
}
else
{
isTalking=false;
imgDlg=null;
}
repaint();
break;
default:
break;
}
}
else
if(isWin)
{
if(sl!=6)
{
isWin=false;
updateMan=false;
winString=new Vector(0,1);
repaint();
}
else
{
isWin=false;
if(killCount==10)
{
gold+=10000;
changeMap(3);
}
else
repaint();
}
}
else
if(isDie)//死亡后重生
{
isDie=false;
isFighting=false;
life=maxlife;
energy=maxenergy;
experience-=(experience-getExperience(level))*20/100;
gold=gold*70/100;
changeMap(0);
}
else
if(curKey==-6)
{
isGameMenu=true;
repaint();
}
}
public void keyReleased(int keyCode)
{
int action = getGameAction(keyCode);
if(action==Canvas.UP||action==Canvas.DOWN||action==Canvas.LEFT||action==Canvas.RIGHT)
{
isWalking=false;
}
}
public void drawGameMenu(Graphics g)
{
g.setColor(0,0,100);
g.fillRoundRect(71,73,86,118,30,30);
g.setColor(getRandom(255),getRandom(255),getRandom(255));
g.drawRoundRect(71,73,86,118,30,30);
g.setColor(0);
g.drawRoundRect(70,72,88,120,30,30);
g.setColor(getRandom(255),getRandom(255),getRandom(255));
g.drawRoundRect(69,71,90,122,30,30);
String[] text=new String[]{"帮助说明","保存游戏",MenuCanvas.isAudioOn?"关闭音效":"打开音效","回主菜单","继续游戏"};
for(int ii=0;ii<5;ii++)
{
if(i==ii)
MenuCanvas.ge.drawStringShadow(g,0x00ff00,0x00ff00,0,text[ii],113,ii*20+80,g.HCENTER|g.TOP);
else
MenuCanvas.ge.drawStringShadow(g,0,0,-1,text[ii],113,ii*20+80,g.HCENTER|g.TOP);
}
}
public int getRandom(int n)
{
int r =rand.nextInt();
if(r<0)
r=-r;
r=r%n;
return r;
}
public void drawWeather(Graphics g1)
{
switch(weatherTask.s)
{
case 0://雨天
int energy=-10;
if(isWeather)
energy=-10+getRandom(5);
g1.setColor(-1);
for(int j=0;j<14;j++)
for(int i=energy+j;i<270;i+=30)
{
if(getRandom(100)<80)
g1.drawLine(i,j*20,i+6,j*20+6);
}
break;
case 1:
g1.setColor(-1);
if(isWeather)
{
for(int i=snow.length-1;i>0;i--)
{
for(int j=0;j