📄 myplayer.java
字号:
package obj;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.*;
import main.*;
import java.util.*;
public class MyPlayer extends Sprite{
private int w,h;//屏幕的宽高
private int viewx=0, viewy=0;//屏幕的坐标。
private int mapw,maph;//底图的宽高。
private int player_x=0;//玩家的位置
private int player_y=0;
private String playerName;
public int playerLevel;//等级
public int playerHp;
public int playerMp;
public int playerEx;//经验
public int playerHit;//攻
public int playerRecovery;//防
public boolean playerBooty=false;//获得战利品。
private int playerMoney;
private int playerMaxHp;
private int playerMaxMp;
private int playerNextEx;//经验
private int imgw=0;//玩家图片大小
private int imgh=0;
private Image speakImg;//说话的图片
private int speakx;
private int speaky;
int temp_a=0;//这三个数记录按1键的次数。
int temp_b=0;
int pressA_Num=1;
private boolean speakSure=false;//按1确认说话。
private boolean hitNpcOk=false;
private boolean showBuyGrid=false;//显示买卖的窗口。
private int BuyGridxPos=0,BuyGridyPos=15;//交易窗口的坐标
private Cur cur; ///显示买卖的窗口时的图标。
private boolean curSwap=false;
private boolean hitDrugstore=false;//是否与药店的人相碰
private boolean hitSmith=false;//是否与铁匠相碰。
Random r=new Random();
int speak_Num=-1;//npc选取那个话说。
public boolean isWarfare=false;//战斗状态。
private int speed;//速度
private boolean isShowGrid=false;//是否显示物品栏。
int sx,sy;//玩家移动时的坐标变化量。在图片移动时:因为要总用,所以放在外面,不用每次都创建
private int step=0;//运动图片的变化计数器
/////////////////////////////////////////////////////////////////////////////////////////
public MyPlayer(Image img,int imagew,int imageh,int Map_w,int Map_h,int boardw,int boardh) {
super(img,imagew,imageh);
imgw=imagew;//图的宽高
imgh=imageh;
w=boardw;//屏幕的宽高
h=boardh;
mapw=Map_w;
maph=Map_h;
player_x=mapw/2;//玩家的初始位置
player_y=maph/2;
speakImg=MyControl.load.speakingImg;//说话的底图
speed=MyControl.load.playerSpeed;
this.setPosition(player_x,player_y);
this.playerLoadFile();
this.playerNewMen();
cur=new Cur(BuyGridxPos,BuyGridyPos,w,h,playerMoney);//交易和使用物品时的光标。
}
////////////
public void playerInit(){//玩家的初始位置
player_x=mapw/2;
player_y=maph/2;
this.setPosition(player_x,player_y);
}
public void playerNeedSaves(){//须要保存的数据。//以后要做个存文件的来保存这些数据.
playerName="圣骑士";
playerLevel=0;//等级
playerHp=30;
playerMp=20;
playerEx=10;//经验
playerMoney=1000;
cur.getVector();//玩家的物品。
//长级比
//卖回东西的价格比
//怪掉钱比。
}
/////////////////////???????????????????????????????????????????????????????????????????
private void playerLoadFile(){
playerName="圣骑士";
playerLevel=0;//等级
playerHp=30;
playerMp=20;
playerEx=0;//经验
playerMoney=1000;
}
//////////////////////////??????????????????????????????????/
private int count(int NN,int level){//升级计算公式。
for(int i=0;i<level;i++){ NN=NN+NN*10/100;}
return NN;}
//////////////////
private void playerNewMen(){//人物的属性。读取时计算
playerMaxHp=count(50,playerLevel);
playerMaxMp=count(30,playerLevel);
playerHit=count(20,playerLevel);//攻
playerRecovery=count(10,playerLevel);//防
playerNextEx=2*count(30,playerLevel);//经验
playerLevel+=1;//等级
playerHp=playerMaxHp;
playerMp=playerMaxMp;
playerEx=0;
}
public void playerHitMap(TiledLayer backMap,Sprite point_0,Sprite point_1){
this.hitMap(backMap);//与Map层的碰撞检测.
this.hitCarryPoint(point_1);//与传送点碰撞
palyerhitAimPoint(point_0);
}
////////////////
private void npcSpeak(Graphics g,Npc npc,String s1,String s2){//npc说话的
speakx=npc.getXpos()-speakImg.getWidth()/2-viewx;
speaky=npc.getYpos()-speakImg.getHeight()-viewy;
g.drawImage(speakImg,speakx,speaky,Graphics.LEFT|Graphics.TOP);
g.setColor(0xffffff);
g.drawString(s1,speakx+5,speaky+5,0);
g.drawString(s2,speakx+5,speaky+20,0);
}
//////////////
private boolean playerHitNpc(Npc npc){//与村中的npc相碰撞,npc停下,不碰时,npc继续动
if(this.collidesWith(npc,true))
{ npc.isSpeaking=true;
hitNpcOk=true;//控制按1时看是否碰撞。
return true;}//相碰撞返回真
else {
if(npc.isSpeaking){
speak_Num=-1;//为说话的选取给初值。
pressA_Num=1;//为按1键的次数给初值。
hitNpcOk=false;
speakSure=false;//关闭与NPC对话的窗口。
npc.isSpeaking=false;
hitDrugstore=false;
hitSmith=false;
}
return false;}
}
/////////////////////
private void hitCarryPoint(Sprite point_1){//与传送点相碰撞
if( this.collidesWith(point_1,true))
{int mapid=MyControl.load.backMapId;
switch(mapid){
case 0:MyControl.load.backMapId=1;break;
case 1:MyControl.load.backMapId=2;break;
case 2:MyControl.load.backMapId=0;break;
}
MyControl.gameBoard.boardInit();
}
}
/////////////////////
public void palyerhitAimPoint(Sprite po){//与传送点相碰撞
if( this.collidesWith(po,true))
{int mapid=MyControl.load.backMapId;
switch(mapid){
case 0:MyControl.load.backMapId=2;break;
case 1:MyControl.load.backMapId=0;break;
case 2:MyControl.load.backMapId=1;break;
}
MyControl.gameBoard.boardInit();
}
}
/////////////
private void hitMap(TiledLayer backMap){//与地图上的房子碰撞
player_x+=sx; //这样做的碰撞检测,一个方向碰撞后,另一个方向不停.
setPosition(player_x,player_y);
if(collidesWith(backMap,true)){
player_x-=sx;setPosition(player_x,player_y);}
player_y+=sy;
setPosition(player_x,player_y);
if(collidesWith(backMap,true)){
player_y-=sy;setPosition(player_x,player_y);}
// player_x+=sx; //这样做的一个方向碰撞后,另一个方向也停了.
// player_y+=sy;
// setPosition(player_x,player_y);//设定玩家的当前位置
// if(collidesWith(backMap,true)){
// player_x-=sx; player_y-=sy;
// setPosition(player_x,player_y);//设定玩家的当前位置
// }
}
////////////////
private int pressA(int st){// 计录按1键的次数。得到不连续输入按 1 键
if(st==0 || ((st&GameCanvas.GAME_A_PRESSED)!=0))
if(temp_a!=st)
{ temp_b++; temp_a=st;
if(temp_b%2==0)
{
pressA_Num++;
if(pressA_Num>3){}
temp_b=0;}
}
return pressA_Num;
}
/////////////这个函数应写在npc类中。//npc说的话。
public void showNpcSpeak(Graphics g,LayerManager layerManager,
Npc npcDrugstore,Npc npcSmith,Npc npcGranger_2,Npc npcGranger_3,
int view_x,int view_y){
viewx=view_x; viewy=view_y;//修正说话图片的坐标的。
if(playerHitNpc(npcDrugstore) && speakSure)
{ if(speak_Num==-1) {speak_Num=r.nextInt(6);}
switch(speak_Num){
case 0:npcSpeak(g,npcDrugstore,"嘿嘿,我是最好的大夫!","不过现在暂时缺货……");break;
case 1:npcSpeak(g,npcDrugstore,"最近村外有妖怪,", "来买药的人也多了……");break;
case 2:npcSpeak(g,npcDrugstore,"这几天我的生意特别好,","你知道这是为什么吗?");break;
case 3:npcSpeak(g,npcDrugstore,"啊,大侠你又来了,", "常来这里是会短命的。");break;
case 4:npcSpeak(g,npcDrugstore,"新到的药材,", "快买,快买…………") ;break;
default:npcSpeak(g,npcDrugstore,"来人买点药吧……", "啊,又一个大头来了!") ;break;
}
hitDrugstore=true;
return;}
if(playerHitNpc(npcSmith) && speakSure)
{ if(speak_Num==-1) {speak_Num=r.nextInt(6);}
switch(speak_Num){
case 0:npcSpeak(g,npcSmith,"做莫邪的就是俺师傅!","现在装备只修不卖。");break;
case 1:npcSpeak(g,npcSmith,"听说最近村外有妖怪,","来买武器的人也多了……");break;
case 2:npcSpeak(g,npcSmith,"这几天我的生意特别好,","你知道这是为什么吗?");break;
case 3:npcSpeak(g,npcSmith,"啊,大侠你又来了,", "上回买的武器还趁手吗?");break;
case 4:npcSpeak(g,npcSmith,"新到的武器!", "快买,快买…………") ;break;
default:npcSpeak(g,npcSmith,"我的武器,呵呵……", "天下…啊…第一……") ;break;
}
hitSmith=true;
return;}
if(playerHitNpc(npcGranger_2) && speakSure)
{ if(speak_Num==-1) {speak_Num=r.nextInt(6);}
switch(speak_Num){
case 0:npcSpeak(g,npcGranger_2,"听说村外有妖怪,","我好怕怕哦……");;break;
case 1:npcSpeak(g,npcGranger_2,"人们都在家里打麻将,","我今天又赚了不少。");break;
case 2:npcSpeak(g,npcGranger_2,"听说药店生意特别好,","你知道这是为什么吗?");break;
case 3:npcSpeak(g,npcGranger_2,"我也想做药材生意,", "可我不敢出村子进货。");break;
case 4:npcSpeak(g,npcGranger_2,"你看村子东北的那人,","在村中都快呆傻了。") ;break;
default:npcSpeak(g,npcGranger_2,"看你是外地人吧,", "来时看到妖怪了吗?") ;break;
}return;
}
if(playerHitNpc(npcGranger_3) && speakSure)
{ if(speak_Num==-1) {speak_Num=r.nextInt(6);}
switch(speak_Num){
case 0:npcSpeak(g,npcGranger_3,"总呆在村里","真是太郁闷了!");break;
case 1:npcSpeak(g,npcGranger_3,"打麻将把底裤都输了,","手气实在是太差了。");break;
case 2:npcSpeak(g,npcGranger_3,"村子西南的那小子,","赢钱还说我坏话&*^%");break;
case 3:npcSpeak(g,npcGranger_3,"我要是铁匠,就……","做把刀砍死那小子。");break;
case 4:npcSpeak(g,npcGranger_3,"啊,大侠,你带我出","村打妖怪吧!") ;break;
default:npcSpeak(g,npcGranger_3,"看你是外地人吧,", "来时看到妖怪了吗?") ;break;
}return;
}
}//end
/////////////////////////////////
int action=-1;
private void playerMove(int st,int mapw,int maph)//玩家移动,边界检测是用底图的宽高做边界
{int xn=16;
sx=0;sy=0;
if((st&GameCanvas.RIGHT_PRESSED)!=0){
if(player_x+1<=mapw-imgw) { sx=speed;}//边界检测//????????????????????????????????????????????????????????
this.setFrame(0+xn*(step++%4)); }//右 //这里要修正:视窗没到边界时,这里可不用检测.
if((st&GameCanvas.LEFT_PRESSED)!=0){
if(player_x-1>=2) { sx=-speed;}
this.setFrame(2+xn*(step++%4)); }//左
if((st&GameCanvas.UP_PRESSED)!=0){
if(player_y-1>=0) {sy=-speed;}
this.setFrame(1+xn*(step++%4)); }//上
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -