`
love19820823
  • 浏览: 933977 次
文章分类
社区版块
存档分类
最新评论

人生如梦游戏间,RPG游戏开源开发讲座(JAVA篇)[6]——走马观花

 
阅读更多

在游戏中通常不可能允许一幅场景贯穿始终,这就提出了场景变更的要求,基于这一要求的实现,我们将进行如下效果的Java代码实现。


下载地址http://www.3adisk.com/?onlinezero[文件名:RpgChair0to6.rar]

或另存以下X(gif)文件为rar


<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:366pt; height:378.75pt'> <v:imagedata src="file:///C:/DOCUME~1/chenpeng/LOCALS~1/Temp/msohtml1/03/clip_image001.gif" o:title="Ex6_01" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

由于代码量已开始增加,由本回开始仅给出部分代码实现,详细请进行下载:

在本例中,本人采取的位图偏移算法如下:

<!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:414.75pt;height:286.5pt'> <v:imagedata src="file:///C:/DOCUME~1/chenpeng/LOCALS~1/Temp/msohtml1/03/clip_image002.gif" o:title="Ex6_02" /> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

MyPanle中实现如下:

// X偏移位置

int offsetX = MyPanel.WIDTH / 2 - role.getX() * CS;

//计算X偏移量

offsetX = Math.min(offsetX, 0);

offsetX = Math.max(offsetX, MyPanel.WIDTH - GameMap.WIDTH);

// Y偏移位置

int offsetY = MyPanel.HEIGHT / 2 - role.getY() * CS;

//计算Y偏移量

offsetY = Math.min(offsetY, 0);

offsetY = Math.max(offsetY, MyPanel.HEIGHT - GameMap.HEIGHT);

//System.out.print(role.getX()+":"+role.getY());

//绘制可偏移位置的地图

map.draw(g, offsetX, offsetY);

//绘制可偏移位置的角色

role.draw(g, offsetX, offsetY);

GameHandleGameMap中,均需注入offsetXoffsetY值,以生成对应图像。

GameHandle实现:

//Example6开始,为了实现背景的移动,所有算法都要加入偏移值

public void draw(Graphics g, int offsetX, int offsetY) {

//count作为图像的偏移数值,并于Example4中添加direction以获取所处图像块位置

g.drawImage(image, x * CS + offsetX, y * CS + offsetY, x * CS + offsetX + CS, y * CS + offsetY + CS,

count * CS, direction * CS, CS + count * CS, direction * CS + CS, panel);

}

GameMap实现:

//修正Example6中绘制方式,引入地图坐标偏移计算

public void draw(Graphics g, int offsetX, int offsetY) {

int firstTileX = pixelsToTiles(-offsetX);

//变更X坐标

int lastTileX = firstTileX + pixelsToTiles(MyPanel.WIDTH) + 1;

//返回最小X偏移值

lastTileX = Math.min(lastTileX, <state w:st="on"><place w:st="on"><em><span lang="EN-US" style='font-size: 10pt; font-family: "Courier New"; color: rgb(0, 0, 192);'>COL</span></em></place></state>);

int firstTileY = pixelsToTiles(-offsetY);

//变更Y坐标

int lastTileY = firstTileY + pixelsToTiles(MyPanel.HEIGHT) + 1;

//返回最小Y偏移值

lastTileY = Math.min(lastTileY, ROW);

//Java或任何游戏开发中,算法都是最重要的一步,本例尽使用

//简单的双层for循环进行地图描绘,

for (int i = firstTileY; i < lastTileY; i++) {

for (int j = firstTileX; j < lastTileX; j++) {

// switch作为java中的转换器,用于执行和()中数值相等

// case操作。请注意,在case操作中如果不以break退出

// 执行;switch函数将持续运算到最后一个case为止。

switch (map[i][j]) {

case 0 : // 地板

g.drawImage(floorImage, tilesToPixels(j) + offsetX, tilesToPixels(i) + offsetY, panel);

break;

case 1 : // 墙壁

g.drawImage(wallImage, tilesToPixels(j) + offsetX, tilesToPixels(i) + offsetY, panel);

break;

}

}

}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics