📄 npc.java
字号:
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/*
* Created on 2006-6-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author 陈利昌
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class NPC {
protected jCanvas canvas;
protected Image ditu;
protected Random ran,ran1;
protected int suanshu;
protected int suiji=0;
protected int x;
protected int y;
protected int fangxiang;
protected int move1=0;
protected int i=0;
protected int jisuan;
//主角所有的属性
protected String name="安利"; //主角姓名
protected static int dengji=1; //主角等级
protected static int jingyan=0; //主角经验
protected static int maxhp=80,maxmp=50; //主角最大HP、MP
protected static int hp=80,mp=50; //主角HP、MP
protected static int liliang=8; //主角的力量
protected static int fangyu=5; //主角的防御离
protected static int wuqi=20; //主角武器的攻击力
protected static int yifu=10; //主角衣服的防御力
protected static int dun=0; //主角盾牌的防御力
protected static int xiezi=0; //主角鞋子的防御力
protected static int zhili=5; //主角的智力
protected static int minjie=1; //主角的敏捷
protected static int gongjili=liliang+wuqi; //主角的攻击力
protected static int fangyuli=fangyu+yifu+dun+xiezi; //主角的防御力
protected static int jinqian=500; //主角的金钱
protected static String daojulan[]={"铁剑","皮铠","斩龙剑","","","","","",""};
protected static String zhuangbeilan[]={"铁剑","皮铠","",""};
protected static String daojulan1[]={"铁剑","皮铠","斩龙剑","","","","","",""};
protected static String zhuangbeilan1[]={"铁剑","皮铠","",""};
protected int shengjijingyan[]={10,20,50,60,80};
//怪物所有属性
protected String guainame[]={"史莱姆","盗贼","蛇怪"};
protected int guaijingyan[]={3,5,10};
protected int guaijingqian[]={5,10,20};
protected int guaimaxhp[]={40,60,80};
protected int guaihp[]={40,60,80};
protected int guaigong[]={20,25,30};
protected int guaifang[]={0,5,10};
public NPC(String name){
try {
ditu=Image.createImage(name);
} catch (IOException e) {
e.printStackTrace();
}
}
public int getgongjili(){
gongjili=liliang+wuqi;
return gongjili;
}
public int getfangyuli(){
fangyuli=fangyu+yifu+dun+xiezi;
return fangyuli;
}
public int getjinqian(){
return jinqian;
}
//
public boolean RENpengzhuang1(int _x, int _y,int x,int y,int row,int shu) {
return ((_x-x)==0 && (_y-y)==shu &&row==0) || ((_x-x)==0 && (_y-y)==-shu &&row==1)||((_x-x)==shu && (_y-y)==0 &&row==2) || ((_x-x)==-shu && (_y-y)==0 &&row==3);
}
//防止主角与NPC发生碰撞而卡住
public boolean Kapengzhuang(int _x, int _y,int x,int y,int row,int NPCrow,int move) {
return (((_x-x)==0 && (_y-y)<=36 && (_y-y)>0) &&row==0 && NPCrow==1&&move==1&&(y+6)%24!=0) || (((_x-x)==0 && (_y-y)>=-36 &&(_y-y)<0) &&row==1&&NPCrow==0&&move==2&&(y+6)%24!=0)||(((_x-x)<=36 &&(_x-x)>0&& (_y-y)==0) &&row==2&&NPCrow==3&&move==3&&x%24!=0) || (((_x-x)>=-36&& (_x-x)<0 && (_y-y)==0) &&row==3&&NPCrow==2&&move==4&&x%24!=0);
}
public boolean Kapengzhuang1(int _x, int _y,int x,int y,int row,int NPCrow,int move) {
return (((_x-x)==0 && (_y-y)<=60 && (_y-y)>36) &&row==0 && NPCrow==1&&move==1&&(y+6)%24!=0) || (((_x-x)==0 && (_y-y)>=-60 &&(_y-y)<-36) &&row==1&&NPCrow==0&&move==2&&(y+6)%24!=0)||(((_x-x)<=60 &&(_x-x)>36&& (_y-y)==0) &&row==2&&NPCrow==3&&move==3&&x%24!=0) || (((_x-x)>=-60&& (_x-x)<-36 && (_y-y)==0) &&row==3&&NPCrow==2&&move==4&&x%24!=0);
}
//人物与NPC的碰撞
public boolean RENpengzhuang(int _x, int _y) {
return Math.abs(x-_x)<24 && Math.abs(y-_y)<24;
}
//NPC与人物的碰撞
public boolean NPCpengzhuang(int _x, int _y,int x,int y) {
return Math.abs(x-_x)<24 && Math.abs(y-_y)<24;
}
//碰撞判断
public boolean RENpengzhuang1(int _x, int _y) {
return Math.abs(x-_x)<=24 && Math.abs(y-_y)<=24;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getfangxiang(){
return fangxiang;
}
//绘制NPC
protected void drawNPC(Graphics g,int hang,int lian,int col,int row,int _x,int _y){
x=_x;
y=_y;
g.setClip(x-hang,y-lian,24,24);
g.drawImage(ditu,(x-hang)-col*24,(y-lian)-row*24,Graphics.TOP|Graphics.LEFT);
}
protected void drawguaiwu(Image daoguai,Graphics g,int x,int y,int _x,int _y){
g.setClip(x,y,_x,_y);
g.drawImage(daoguai,x,y,Graphics.TOP|Graphics.LEFT);
}
protected boolean yudi(){
rowNPC(100);
return suiji<=2;
}
//随机得到NPC的行走方向
public int rowNPC(int huan){
int move=0;
Random ran= new Random();
suiji=Math.abs(ran.nextInt()%huan);
if(suiji>=0&&suiji<=25){
move=1;
}
if(suiji>=25&&suiji<=50){
move=2;
}
if(suiji>=50&&suiji<=75){
move=3;
}
if(suiji>=75&&suiji<=100){
move=4;
}
return move;
}
//NPC判断
public int NPCmove(int huan,int NPCbuchang,int NPCmove,int NPCrow){
NPCmove=rowNPC(huan);
if(NPCmove==1){
NPCrow=0;
}
if(NPCmove==2){
NPCrow=1;
}
if(NPCmove==3){
NPCrow=2;
}
if(NPCmove==4){
NPCrow=3;
}
return NPCrow;
}
//人物与图片的碰撞
public boolean pengzhuang(short map[][],int hang,int lian,int renx,int reny){
return map[(lian+reny+6)/24][(hang+renx)/24]!=0&&map[(lian+reny+6)/24][(hang+renx)/24]!=1&&map[(lian+reny+6)/24][(hang+renx)/24]!=2&&map[(lian+reny+6)/24][(hang+renx)/24]!=3&&map[(lian+reny+6)/24][(hang+renx)/24]!=11&&map[(lian+reny+6)/24][(hang+renx)/24]!=14&&map[(lian+reny+6)/24][(hang+renx)/24]!=26&&map[(lian+reny+6)/24][(hang+renx)/24]!=37&&map[(lian+reny+6)/24][(hang+renx)/24]!=38&&map[(lian+reny+6)/24][(hang+renx)/24]!=42&&map[(lian+reny+6)/24][(hang+renx)/24]!=44&&map[(lian+reny+6)/24][(hang+renx)/24]!=62&&map[(lian+reny+6)/24][(hang+renx)/24]!=27&&map[(lian+reny+6)/24][(hang+renx)/24]!=43&&map[(lian+reny+6)/24][(hang+renx)/24]!=45;
}
//人物与图片的碰撞
public boolean pengzhuang1(short map[][],int hang,int lian,int renx,int reny){
return map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=0&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=1&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=2&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=3&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=11&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=14&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=26&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=37&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=38&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=42&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=44&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=62&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=27&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=43&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=45;
}
//NPC与图片的碰撞
public boolean pengzhuang3(short map[][],int hang,int lian,int renx,int reny){
return map[(lian+reny+6)/24][(hang+renx)/24]!=12&&map[(lian+reny+6)/24][(hang+renx)/24]!=13&&map[(lian+reny+6)/24][(hang+renx)/24]!=19&&map[(lian+reny+6)/24][(hang+renx)/24]!=21&&map[(lian+reny+6)/24][(hang+renx)/24]!=40&&map[(lian+reny+6)/24][(hang+renx)/24]!=41&&map[(lian+reny+6)/24][(hang+renx)/24]!=47&&map[(lian+reny+6)/24][(hang+renx)/24]!=48;
}
//NPC与图片的碰撞
public boolean pengzhuang4(short map[][],int hang,int lian,int renx,int reny){
return map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=12&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=13&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=19&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=21&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=40&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=41&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=47&&map[(lian+reny+6+18)/24][(hang+renx+18)/24]!=48;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -