📄 player_rpg.java
字号:
package rpg_demo_P;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import java.lang.String;
public class Player_RPG extends Sprite {
Sprite red,blue;
Image yao_Image;
//字体
private static final Font mini=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);
private static final Font medium=Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
StringBuffer buffer=new StringBuffer("");
static public final int HERO_MOVE_FX_UP=0;
static public final int HERO_MOVE_FX_DOWN=1;
static public final int HERO_MOVE_FX_LEFT=2;
static public final int HERO_MOVE_FX_RIGHT=3;
int getfx=0; //获取当前精灵的方向(状态)
// private Graphics g;
public int lev; //等级
public int exp;//经验值
public int exp_max;//最大经验值
public static int hp;//当前生命值
public int mp;//当前魔法值
public int hp_max;//最大生命值
public int mp_max;//最大魔法值
public String name;//角色的名字
public int attack;//攻击力
public static int fy;//防御
public int money;//金钱
public int hp_R;//红药
public int mp_B;//蓝药
public int px,py;//坐标
public int spend_Move;//移动速度
public int spend_Attack;//攻击速度
public int w_Frame,h_Frame;
int[] JustF=null; //npc的帧动画
int[] move_left = {3,4,5}; //向左动作的图片索引
int[] move_right = {9,10,11}; //向右动作的图片索引
int[] move_down={0,1,2}; //向下的动作图片索引
int[] move_up = {6,7,8}; //向上的动作图片索引
int[] action1 = {0,3,6,9}; //动作图片索引
int[] Static = {0}; //静止时的图片索引
/*
* 上、下、左、右、动作、静止的状态值
*/
final int MOVELEFT = 1;
final int MOVERIGHT = 2;
final int MOVEUP=3;
final int MOVEDOWN=4;
final int ACTION = 5;
final int STATIC=0;
int nonkey; //未按键状态 0:未按键 1:按键
int playerState; //精灵的动作状态
public Player_RPG(Image img,int w_Frame,int h_Frame,int px,int py)
{
super(img,w_Frame,h_Frame);
this.w_Frame=w_Frame;
this.h_Frame=h_Frame;
lev=1;
exp=0;
exp_max=100;
hp=hp_max=100;
mp=mp_max=30;
money=100;
attack=6;
fy=2;
hp_R=10;
mp_B=3;
spend_Move=3;
spend_Attack=3;
playerState=STATIC;
this.px=px;
this.py=py;
/* try
{
yao_Image=Image.createImage("/posions.png");
red=new Sprite(yao_Image,24,24);
blue=new Sprite(yao_Image,24,24);
red.setPosition(0, 208-45);
blue.setPosition(55, 208-45);
}catch(Exception exc)
{
System.out.println(exc);
}*/
this.setPosition(px,py);
this.defineCollisionRectangle(6, 6, 16, 16);
}
public void Draw_STATE(Graphics g,Image touxiang,Image red ,Image blue,Image jn,int width,int height)
{
g.setFont(mini);
g.drawImage(touxiang, 0, 0, Graphics.TOP|Graphics.LEFT);
g.setColor(0x00ffff00);
g.drawString("hp ",30,0,20);
g.drawString("mp ",30,10,20);
g.drawString("exp ",30,20,20);
g.setColor(0x000000);
g.drawRoundRect(45, 0, 51, 7,2,2);
g.drawRoundRect(45, 10, 51, 7,2,2);
g.drawRoundRect(45, 20, 51, 7,2,2);
g.setColor(0xff0000);
g.fillRoundRect(46, 1, (hp*100/hp_max)*50/100, 6,2,2);
g.setColor(0x0000ff);
g.fillRoundRect(46, 11, (mp*100/mp_max)*50/100, 6,2,2);
g.setColor(0x00ffff00);
g.fillRoundRect(46, 21, (exp*100/exp_max)*50/100, 6,2,2);
g.setColor(0x00ff0000);
buffer.delete(0, buffer.length());
g.drawString(buffer.append(hp).append(" / ").append(hp_max).toString(), 100, 0, Graphics.TOP|Graphics.LEFT);
buffer.delete(0, buffer.length());
g.drawString(buffer.append(mp).append(" / ").append(mp_max).toString(), 100, 9, Graphics.TOP|Graphics.LEFT);
buffer.delete(0, buffer.length());
g.drawString(buffer.append(exp).append(" /").append(exp_max).toString(), 100, 19, Graphics.TOP|Graphics.LEFT);
buffer.delete(0, buffer.length());
g.drawString(buffer.append("LEV: ").append(lev).toString(),0,32, Graphics.TOP|Graphics.LEFT);
buffer.delete(0,buffer.length());
//画红蓝药
g.setColor(0x00000000);
g.setFont(medium);
//红药
g.drawRoundRect(0,height,17,16,2,2);
g.drawString(buffer.append(hp_R).toString(),10,height-16, Graphics.TOP|Graphics.LEFT);
buffer.delete(0,buffer.length());
g.drawImage(red,0,height, Graphics.TOP|Graphics.LEFT);
//蓝药
g.drawRoundRect(17,height,17,16,2,2);
g.drawString(buffer.append(mp_B).toString(),27,height-16, Graphics.TOP|Graphics.LEFT);
buffer.delete(0,buffer.length());
g.drawImage(blue,17,height, Graphics.TOP|Graphics.LEFT);
//技能框
g.setColor(0x00000000);
g.drawRoundRect(34, height, 17, 16, 2, 2);
g.drawString(buffer.append("L/P").toString(),44,height-16, Graphics.TOP|Graphics.LEFT);
buffer.delete(0,buffer.length());
g.drawImage(jn,34,height, Graphics.TOP|Graphics.LEFT);
// if(Rpg_DEMO.jineng==0)
// g.drawImage(arg0, arg1, arg2, arg3);
// else if(Rpg_DEMO.jineng==1)
// g.drawImage(arg0, arg1, arg2, arg3);
}
public void Add_HP() //使用红药
{ if(hp_R>0)
{
hp_R--;
try
{
Thread.sleep(500);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
hp+=25;
if(hp>hp_max)
hp=hp_max;
}
}
public void Add_MP() //使用蓝药
{
if(mp_B>0)
{
mp_B--;
try
{
Thread.sleep(500);
}catch(Exception e)
{
System.out.println(e.getMessage());
}
mp+=50;
if(mp>mp_max)
mp=mp_max;
}
}
public void Move_UP(int[] Frame)//主角向上移动
{
// System.out.println("test here");
if(this.JustF!=Frame)
{
this.JustF=Frame;
//getfx=HERO_MOVE_FX_UP;
this.setFrameSequence(this.JustF);
}
py-=spend_Move;
this.nextFrame();
if(this.getY() - spend_Move>= 0)
this.move(0, -spend_Move);
else
this.setPosition(this.getX(), 0);
}
public void Move_DOWN(int[] Frame,TiledLayer tl_BG)//主角向下移动
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
//getfx=HERO_MOVE_FX_DOWN;
this.setFrameSequence(this.JustF);
}
this.nextFrame();
if(this.getY() + h_Frame < tl_BG.getHeight())
this.move(0, spend_Move);
else
this.setPosition(this.getX(), tl_BG.getHeight()-h_Frame);
}
public void Move_LEFT(int[] Frame)//主角向左移动
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
//getfx=HERO_MOVE_FX_LEFT;
this.setFrameSequence(this.JustF);
px-=spend_Move;
}
this.nextFrame();
if(this.getX() - spend_Move >= 0)
this.move(-spend_Move, 0);
else
this.setPosition(0, this.getY());
}
public void Move_RIGHT(int[] Frame,TiledLayer tl_BG)//主角向右移动
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
//getfx=HERO_MOVE_FX_RIGHT;
this.setFrameSequence(this.JustF);
px+=spend_Move;
}
this.nextFrame();
if(this.getX() + w_Frame < tl_BG.getWidth())
this.move(spend_Move, 0);
else
this.setPosition(tl_BG.getWidth()-w_Frame, this.getY());
}
public void Dead(int[] Frame)//主角死亡
{
if(hp==0)
{
this.setFrameSequence(Frame);
if(this.getFrame()==2)
{
this.setVisible(false);
}
}
}
public void Attack_UP( int[] Frame)//主角向上攻击
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
nonkey=1;
this.setFrameSequence(this.JustF);
}
//this.nextFrame();
if(this.getFrame()==Frame.length-1)
{
nonkey=0;
this.setFrame(0);
}
}
public void Attack_RIGHT( int[] Frame)//主角向右攻击
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
nonkey=1;
this.setFrameSequence(this.JustF);
}
//this.nextFrame();
if(this.getFrame()==Frame.length-1)
{
nonkey=0;
this.setFrame(0);
}
}
public void Attack_DOWN( int[] Frame)//主角向下攻击
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
nonkey=1;
this.setFrameSequence(this.JustF);
}
//this.nextFrame();
if(this.getFrame()==Frame.length-1)
{
nonkey=0;
this.setFrame(0);
}
}
public void Attack_LEFT( int[] Frame)//主角向左攻击
{
if(this.JustF!=Frame)
{
this.JustF=Frame;
nonkey=1;
this.setFrameSequence(this.JustF);
}
// this.nextFrame();
if(this.getFrame()==Frame.length-1)
{
nonkey=0;
this.setFrame(0);
}
}
public void Hurt(int n)//主角被攻击
{
this.hp=this.hp+this.fy-n;
}
public void Tacking()
{
}
public void Levelup(boolean arg0)
{
lev++;
attack+=2;
fy+=1;
hp_max+=4;
mp_max+=3;
hp=hp_max;
mp=mp_max;
exp-=exp_max;
exp_max+=50;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -