Java来做马里奥[0]—让精灵再次舞动

记得在红白机(FC)年代,还刚刚上小学的我对马里奥、冒险岛、洛克人、魂斗罗等游戏几乎可说是痴迷,每天放学回家就是想去游戏,就是要通关,就是想和关底论个胜负高低。

许多年过去了,沧海桑田,FC曾经的荣耀早已不再,只留下我们对曾经少年时的点点记忆。即使当时在FC上看上去多么复杂,多么高不可攀的游戏,在当今,即使最普通的程序员都可以轻易实现。

本着向经典学习、向经典致敬的心情,我也准备用Java在PC机再现当年马里奥的风采。

下面我在代码中所演示的,是一个简单的ACT游戏动作及地图构成原型。

Map.java
package org.test.mario;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

/***/ /**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:地图绘制及描述用类
*</p>
*<p>
*Copyright:Copyright(c)2008
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*
@authorchenpeng
*@email:[email protected]
*
@version0.1
*/

public class Map ... {

//在以前的blog文章中我介绍过,游戏开发中通常以数组描述地图
//此处1描绘为一个障碍物,0描绘为一个可通行空间
finalstaticprivateint[][]map=...{
...{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},
...{1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
...{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}}
;

//地面瓦片的宽度
finalstaticprivateintTILE_SIZE=32;

//
finalstaticprivateintROW=15;

//
finalstaticprivateintCOL=20;

/***//**
*构造函数
*
*/

publicMap()...{
}


publicvoiddraw(Graphicsg)...{
g.setColor(Color.ORANGE);
for(inti=0;i<ROW;i++)...{
for(intj=0;j<COL;j++)...{
switch(map[i][j])...{
case1:
g.fillRect(tilesToPixels(j),tilesToPixels(i),TILE_SIZE,
TILE_SIZE);
break;
}

}

}

}


/***//**
*换算角色与地板的撞击,并返回Point用以描述新的x,y
*
*
@paramplayer
*
@paramnewX
*
@paramnewY
*
@return
*/

publicPointgetTileHit(Roleplayer,doublenewX,doublenewY)...{
//取最小的整数但不能小于自身,用以换算坐标
newX=Math.ceil(newX);
newY
=Math.ceil(newY);

doublefromX=Math.min(player.getX(),newX);
doublefromY=Math.min(player.getY(),newY);
doubletoX=Math.max(player.getX(),newX);
doubletoY=Math.max(player.getY(),newY);

intfromTileX=pixelsToTiles(fromX);
intfromTileY=pixelsToTiles(fromY);
inttoTileX=pixelsToTiles(toX+Role.WIDTH-1);
inttoTileY=pixelsToTiles(toY+Role.HEIGHT-1);

//返回Point,用以描述x,y坐标点
for(intx=fromTileX;x<=toTileX;x++)...{
for(inty=fromTileY;y<=toTileY;y++)...{
if(x<0||x>=COL)...{
returnnewPoint(x,y);
}

if(y<0||y>=ROW)...{
returnnewPoint(x,y);
}

if(map[y][x]==1)...{
returnnewPoint(x,y);
}

}

}


returnnull;
}


/***//**
*将Tiles转为Pixels
*
*
@parampixels
*
@return
*/

publicstaticintpixelsToTiles(doublepixels)...{
return(int)Math.floor(pixels/TILE_SIZE);
}


/***//**
*将Pixels转为Tiles
*
*
@parampixels
*
@return
*/

publicstaticinttilesToPixels(inttiles)...{
returntiles*TILE_SIZE;
}

}


Role.java

package org.test.mario;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

/***/ /**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:角色描述及绘制用类
*</p>
*<p>
*Copyright:Copyright(c)2008
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*
@authorchenpeng
*@email:[email protected]
*
@version0.1
*/

public class Role ... {

//坐标的x,y
privatedouble_x;

privatedouble_y;

//显示的x,_y
privatedouble_vx;

privatedouble_vy;

//是否在平地
privatebooleanisFlat;

//自定义的地图描述类
privateMap_map;

//角色宽
finalstaticpublicintWIDTH=32;

//角色高
finalstaticpublicintHEIGHT=32;

//移动速度
finalstaticpublicintSPEED=6;

//跳越速度
finalstaticpublicintJUMP_SPEED=25;

/***//**
*构造函数,注入初始的角色x,y及map
*
*
@param_x
*
@param_y
*
@param_map
*/

publicRole(doublex,double_y,Map_map)...{
this._x=x;
this._y=_y;
this._map=_map;
_vx
=0;
_vy
=0;
isFlat
=false;
}


/***//**
*停止动作
*
*/

publicvoidstop()...{
_vx
=0;
}


/***//**
*向左
*
*/

publicvoidleft()...{
_vx
=-SPEED;
}


/***//**
*向右
*
*/

publicvoidright()...{
_vx
=SPEED;
}


/***//**
*跳越动作
*
*/

publicvoidjump()...{
//当角色立于平地时
if(isFlat)...{
_vy
=-JUMP_SPEED;
isFlat
=false;
}

}


/***//**
*变更位置
*
*/

publicvoidupdate()...{
//加入偏差值
_vy+=1.0;

//获得新的newX
doublenewX=_x+_vx;

//获得地板x,_y
Pointtile=_map.getTileHit(this,newX,_y);
//不存在时则默认为newX
if(tile==null)...{
_x
=newX;
}
else...{
if(_vx>0)...{
_x
=Map.tilesToPixels(tile.x)-WIDTH;
}
elseif(_vx<0)...{
_x
=Map.tilesToPixels(tile.x+1);
}

_vx
=0;
}


doublenewY=_y+_vy;

tile
=_map.getTileHit(this,_x,newY);
if(tile==null)...{
_y
=newY;
isFlat
=false;
}
else...{
if(_vy>0)...{
_y
=Map.tilesToPixels(tile.y)-HEIGHT;
_vy
=0;
isFlat
=true;
}
elseif(_vy<0)...{
_y
=Map.tilesToPixels(tile.y+1);
_vy
=0;
}

}

}


/***//**
*将角色绘制于指定Graphics上
*
*
@paramg
*/

publicvoiddraw(Graphicsg)...{
//目前以一个红色方块代替
g.setColor(Color.RED);
g.fillRect((
int)_x,(int)_y,WIDTH,HEIGHT);
}


publicdoublegetX()...{
return_x;
}


publicdoublegetY()...{
return_y;
}


}


启动类:Main.java
package org.test.mario;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.loon.framework.game.image.Bitmap;

/***/ /**
*<p>
*Title:LoonFramework
*</p>
*<p>
*Description:
*</p>
*<p>
*Copyright:Copyright(c)2008
*</p>
*<p>
*Company:LoonFramework
*</p>
*
*
@authorchenpeng
*@email:[email protected]
*
@version0.1
*/

public class Main extends Panel implements Runnable,KeyListener ... {

/***//**
*
*/

privatestaticfinallongserialVersionUID=1L;

publicstaticfinalint_WIDTH=640;

publicstaticfinalint_HEIGHT=480;

privateMap_map;

privateRole_role;

privateThread_sleep;

privateImage_screen=null;

privateGraphics_graphics=null;

//方向控制,由于是自然落体所以没有down
privatebooleanLEFT;

privatebooleanRIGHT;

privatebooleanUP;

publicMain()...{
setSize(_WIDTH,_HEIGHT);
setFocusable(
true);
_screen
=newBitmap(_WIDTH,_HEIGHT).getImage();
_graphics
=_screen.getGraphics();
_map
=newMap();

_role
=newRole(100,32,_map);

//监听窗体
addKeyListener(this);

//启动线程
_sleep=newThread(this);
_sleep.start();
}


/***//**
*运行
*/

publicvoidrun()...{
while(true)...{
//改变方向
if(LEFT)...{
_role.left();
}
elseif(RIGHT)...{
_role.right();
}
else...{
_role.stop();
}

if(UP)...{
_role.jump();
}

_role.update();
repaint();
try...{
Thread.sleep(
20);
}
catch(InterruptedExceptione)...{
e.printStackTrace();
}

}

}


publicvoidupdate(Graphicsg)...{
paint(g);
}


publicvoidpaint(Graphicsg)...{
_graphics.setColor(Color.BLACK);
_graphics.fillRect(
0,0,_WIDTH,_HEIGHT);
_map.draw(_graphics);
_role.draw(_graphics);
g.drawImage(_screen,
0,0,null);
}


publicvoidkeyPressed(KeyEvente)...{
intkey=e.getKeyCode();
if(key==KeyEvent.VK_LEFT)...{
LEFT
=true;
}

if(key==KeyEvent.VK_RIGHT)...{
RIGHT
=true;
}

if(key==KeyEvent.VK_UP)...{
UP
=true;
}

}


publicvoidkeyReleased(KeyEvente)...{
intkey=e.getKeyCode();
if(key==KeyEvent.VK_LEFT)...{
LEFT
=false;
}

if(key==KeyEvent.VK_RIGHT)...{
RIGHT
=false;
}

if(key==KeyEvent.VK_UP)...{
UP
=false;
}

}


publicvoidkeyTyped(KeyEvente)...{
}


publicstaticvoidmain(String[]args)...{
Frameframe
=newFrame();
frame.setTitle(
"Java来做马里奥(1)—让精灵舞动");
frame.setSize(_WIDTH,_HEIGHT);
frame.setResizable(
false);
frame.setLocationRelativeTo(
null);
frame.add(
newMain());
frame.setVisible(
true);
frame.addWindowListener(
newWindowAdapter()...{
publicvoidwindowClosing(WindowEvente)...{
System.exit(
0);
}

}
);
}


}


运行效果如下图:
Java来做马里奥[0]—让精灵再次舞动

现在开始,我会在blog中逐步构建马里奥中的一关,有关心java pc游戏开发者敬请留意。

你可能感兴趣的:(java)