📄 gameplay.java~201~
字号:
package GoGoGo;
import java.util.Vector;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0.0
*/
public class GamePlay extends GameFrameBase
{
//Game State Defining
final static byte PLAYS_NEXT_LEVEL = 20;
final static byte PLAYS_COUNT_DOWN = 21;
final static byte PLAYS_PLAYING = 22;
final static byte PLAYS_STAGE_CLEARED = 23;
final static byte PLAYS_BOOM = 24;
final static byte PLAYS_GAME_OVER = 25;
String npcword1="ni hao";
int animation= 0; //主角状态
int NPCanimation[]= new int [2]; //NPC状态
AnimationX animationx;//主角动画管理对象
AnimationX NPCanimationx;//NPC管理对象
CMap m_Map=null;
npcROL npc[]=new npcROL[2];
int HeroACT=30;//主角攻击力
//Game map Defining
/* final static byte STAGE_DATA[][][] =
{
{
{2, 0, 0, 4, 0},
{0, 0, 3, 0, 4},
{1, 0, 2, 0, 0},
{0, 0, 0, 0, 0}
}
};*/
//Gaming Consts
/* final static byte STAGE_COUNT = 1;
final static byte BLOCK_SIZE = 24;
final static byte SMALL_BLOCK_SIZE = 16;
final static byte MAP_X_LENGTH = 5;
final static byte MAP_Y_LENGTH = 4;
final static byte START_POS_X = 4;
final static byte START_POS_Y = 1;*/
//=变量定义=================
//byte m_Curr_StageData[][];
int m_i_CurrStage;
//==================================END=
//=图片资源=================
private Surface m_Surface_BigRoad = null;
// private Surface m_Surface_BigCar = null;
// private Surface m_Surface_SmallRoad = null;
// private Surface m_Surface_SmallCar = null;
// private Surface m_Surface_Icons = null;
// private Surface m_Surface_Digits = null;
// private Surface m_Surface_Boom = null;
// private Surface m_Surface_TitleNext = null;
// private Surface m_Surface_TitleGameOver = null;
private Surface m_Surface_right = null;
private Surface m_Surface_up = null;
private Surface m_Surface_down = null;
private Surface m_Surface_left = null;
private Surface m_Surface_ACT_left = null;
private Surface m_Surface_ACT_right= null;
private Surface m_Surface_NPC_up = null;
private Surface m_Surface_NPC_down = null;
private Surface m_Surface_NPC_right = null;
private Surface m_Surface_NPC_left = null;
public GamePlay()
{
m_vect_SurfacePath = new Vector();
m_vect_SurfacePath.addElement("/map1.png");
// m_vect_SurfacePath.addElement("/man1.png");
// m_vect_SurfacePath.addElement("/smallroad.png");
// m_vect_SurfacePath.addElement("/smallcar.png");
// m_vect_SurfacePath.addElement("/icons.png");
// m_vect_SurfacePath.addElement("/digits.png");
//m_vect_SurfacePath.addElement("/boom.png");
// m_vect_SurfacePath.addElement("/title_next.png");
// m_vect_SurfacePath.addElement("/title_gameover.png");
m_vect_SurfacePath.addElement("/RIGHT(28-42).png");
m_vect_SurfacePath.addElement("/UP(22,42).png");
m_vect_SurfacePath.addElement("/DOWN(34,38).png");
m_vect_SurfacePath.addElement("/LEFT.png");
m_vect_SurfacePath.addElement("/ACT_left.png");
m_vect_SurfacePath.addElement("/ACT_right.png");
m_vect_SurfacePath.addElement("/NPC_right.png");
m_vect_SurfacePath.addElement("/NPC_left.png");
m_vect_SurfacePath.addElement("/NPC_up.png");
m_vect_SurfacePath.addElement("/NPC_down.png");
// m_Curr_StageData = new byte[MAP_Y_LENGTH][MAP_X_LENGTH];
}
public void Create(GameViewBase view)
{
m_View = view;
m_BackGraphy = m_View.m_g_BackGraphy;
m_Surface_BigRoad = m_View.nextSurface();
// m_Surface_BigCar = m_View.nextSurface();
// m_Surface_SmallRoad = m_View.nextSurface();
// m_Surface_SmallCar = m_View.nextSurface();
// m_Surface_Icons = m_View.nextSurface();
// m_Surface_Digits = m_View.nextSurface();
// m_Surface_Boom = m_View.nextSurface();
//
// m_Surface_TitleNext = m_View.nextSurface();
// m_Surface_TitleGameOver = m_View.nextSurface();
m_Surface_right = m_View.nextSurface();
m_Surface_up = m_View.nextSurface();
m_Surface_down = m_View.nextSurface();
m_Surface_left = m_View.nextSurface();
m_Surface_ACT_left = m_View.nextSurface();
m_Surface_ACT_right = m_View.nextSurface();
m_Surface_NPC_right = m_View.nextSurface();
m_Surface_NPC_left = m_View.nextSurface();
m_Surface_NPC_up = m_View.nextSurface();
m_Surface_NPC_down = m_View.nextSurface();
if (m_Map == null)
{
m_Map = new CMap(m_View);
m_Map.Create(m_Surface_BigRoad);
}
m_View.m_pge_gameplay.NewGame(1);
m_i_FrameState = PLAYS_PLAYING;
npc[0] = new npcROL(100, 2, 0, 0, 60, 60);
npc[1] = new npcROL(100, 2, 1, 0, 100, 100);
animationx = new AnimationX();
animation = animationx.down_stand;
}
public void Release()
{
if(m_View != null)
m_View.ReleaseAllSurfaces();
}
public void NewGame(int iStage)
{
m_i_CurrStage = iStage;
m_Map.LoadMap(m_i_CurrStage);
//加载资源
}
public void OnKeyDown(int iScanCode)
{
switch (m_i_FrameState)
{
case PLAYS_PLAYING:
MAPOnKeyDown( iScanCode);
break;
}
}
public void OnKeyUp(int iScanCode)
{
switch (m_i_FrameState)
{
case PLAYS_PLAYING:
MAPOnKeyUp( iScanCode);
break;
}
}
public void MAPOnKeyDown(int iScanCode)
{
// System.out.println(iScanCode);
switch (iScanCode)
{
case -1:
case '2':
m_Map.MoveUp();
animation=animationx.up_run;
break;
case -2:
case '8':
m_Map.MoveDown();
animation=animationx.down_run;
break;
case -3:
case '4':
m_Map.MoveLeft();
animation=animationx.Left_run;
break;
case -4:
case '6':
m_Map.MoveRight();
animation=animationx.right_run;
break;
case '5':
m_Map.StopMoveUp();
m_Map.StopMoveLeft();
m_Map.StopMoveRight();
m_Map.StopMoveDown();
animationx.Current_station=animation;
animation=animationx.ACT;
break;
}
}
public void MAPOnKeyUp(int iScanCode)
{
switch (iScanCode)
{
case -1:
case '2':
m_Map.StopMoveUp();
animation=animationx.up_stand;
break;
case -2:
case '8':
m_Map.StopMoveDown();
animation=animationx.down_stand;
break;
case -3:
case '4':
m_Map.StopMoveLeft();
animation=animationx.Left_stand;
break;
case -4:
case '6':
m_Map.StopMoveRight();
animation=animationx.right_stand;
break;
case '5':
if(animationx.Current_station<4)
animation=animationx.Current_station+4;
else if(animationx.Current_station>3&& animationx.Current_station<8)
animation=animationx.Current_station;
animationx.Current_station=-1;
break;
}
}
public void Show()
{
switch (m_i_FrameState)
{
}
m_Map.DRAWMAP();//地图绘制
// Drawman();//绘制主角
DrawNPC();//绘制NPC
npcbrock();//主角和NPC碰撞方法
ACTbrock();//攻击碰撞
// System.out.print(m_i_FrameState);
}
//
public void npcbrock()//身体碰撞
{
for(int i=0;i<npc.length;i++)
{
if(npc[i].life<1)
{
npc[i].NPC_X = -100;
npc[i].NPC_Y = -100;
break;
}
if (checkRectCollision(X_intMan,Y_intMan,animationx.getwidth(animation),animationx.getheight(animation),npc[i].NPC_X, npc[i].NPC_Y,npc[i].getwidth(npc[i].NPCStatic),npc[i].getheight(npc[i].NPCStatic)))
{
m_View.m_g_BackGraphy.setColor(0);
m_View.m_g_BackGraphy.drawString("ni hao",npc[i].NPC_X- m_Map.MapX, npc[i].NPC_Y- m_Map.MapY,0);
}
}
}
public void ACTbrock()//攻击碰撞
{
for(int i=0;i<npc.length;i++)
{
if (npc[i].life < 1)
{
npc[i].NPC_X = -100;
npc[i].NPC_Y = -100;
break;
}
switch (animationx.Current_station)
{
case 0:
case 4:
if (checkRectCollision(X_intMan-20,Y_intMan+15,16,21,npc[i].NPC_X, npc[i].NPC_Y,npc[i].getwidth(npc[i].NPCStatic),npc[i].getheight(npc[i].NPCStatic)))
{
m_View.m_g_BackGraphy.setColor(255,0,0);
m_View.m_g_BackGraphy.drawString("别打我!!",npc[i].NPC_X- m_Map.MapX, npc[i].NPC_Y- m_Map.MapY,0);
npc[i].Hit=true;
}
// m_View.m_g_BackGraphy.setColor(255);
// m_View.m_g_BackGraphy.fillRect(X_intMan-m_Map.MapX-30,Y_intMan-m_Map.MapY+20,16,16);
break;
case 1:
case 5:
if (checkRectCollision(X_intMan+30,Y_intMan+10,16,21,npc[i].NPC_X, npc[i].NPC_Y,npc[i].getwidth(npc[i].NPCStatic),npc[i].getheight(npc[i].NPCStatic)))
{
m_View.m_g_BackGraphy.setColor(255,0,0);
m_View.m_g_BackGraphy.drawString("别打我!!",npc[i].NPC_X- m_Map.MapX, npc[i].NPC_Y- m_Map.MapY,0);
npc[i].Hit=true;
}
// m_View.m_g_BackGraphy.setColor(255);
// m_View.m_g_BackGraphy.fillRect(X_intMan-m_Map.MapX+65,Y_intMan-m_Map.MapY+20,16,16);
break;
case 2:
case 6:
if (checkRectCollision(X_intMan-20,Y_intMan+15,16,21,npc[i].NPC_X, npc[i].NPC_Y,npc[i].getwidth(npc[i].NPCStatic),npc[i].getheight(npc[i].NPCStatic)))
{
m_View.m_g_BackGraphy.setColor(255,0,0);
m_View.m_g_BackGraphy.drawString("别打我!!",npc[i].NPC_X- m_Map.MapX, npc[i].NPC_Y- m_Map.MapY,0);
npc[i].Hit=true;
}
// m_View.m_g_BackGraphy.setColor(255);
// m_View.m_g_BackGraphy.fillRect(X_intMan-m_Map.MapX-30,Y_intMan-m_Map.MapY+20,16,16);
break;
case 3:
case 7:
if (checkRectCollision(X_intMan+30,Y_intMan+10,16,21,npc[i].NPC_X, npc[i].NPC_Y,npc[i].getwidth(npc[i].NPCStatic),npc[i].getheight(npc[i].NPCStatic)))
{
m_View.m_g_BackGraphy.setColor(255,0,0);
m_View.m_g_BackGraphy.drawString("别打我!!",npc[i].NPC_X- m_Map.MapX, npc[i].NPC_Y- m_Map.MapY,0);
npc[i].Hit=true;
}
// m_View.m_g_BackGraphy.setColor(255);
// m_View.m_g_BackGraphy.fillRect(X_intMan-m_Map.MapX+65,Y_intMan-m_Map.MapY+20,16,16);
break;
}
}
}
//物品碰撞检测
protected final boolean checkRoundCollision(int x1,int y1,int r1,int x2,int y2,int r2){
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) <= (r1 + r2)*(r1 + r2) ;
}
protected final boolean distanceLessThan(int x0,int y0,int newX,int newY,int oldX,int oldY){
return (newX-x0)*(newX-x0) + (newY-y0)*(newY-y0) < (oldX-x0)*(oldX-x0) + (oldY-y0)*(oldY-y0);
}
protected final boolean checkRectCollision(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
if(x1>=x2+w2 || x1+w1<=x2 || y1>=y2+h2 || y1+h1<=y2 ){
return false;
}
return true;
}
boolean Check=false;
public boolean Brock_check(int speedX, int speedY)
{
int CheckBlockY = m_Map.MapY / m_Map.MapblockSize; //16
int CheckBlockX = m_Map.MapX / m_Map.MapblockSize; //30
for (int i = CheckBlockY; i < m_Map.ShowMapArrowY + CheckBlockY; i++)
{
for (int j = CheckBlockX; j < m_Map.ShowMapArrowX + CheckBlockX;
j++)
{
if ( (m_Map.MAP_DATE[i][j] > 12 && m_Map.MAP_DATE[i][j] < 17) ||
(m_Map.MAP_DATE[i][j] > 42 && m_Map.MAP_DATE[i][j] < 47) ||
(m_Map.MAP_DATE[i][j] > 72 && m_Map.MAP_DATE[i][j] < 77) ||
(m_Map.MAP_DATE[i][j] > 102 && m_Map.MAP_DATE[i][j] < 117))
if (checkRectCollision(j * m_Map.MapblockSize,
i * m_Map.MapblockSize,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -