选择显示字体大小

midp 2.0开发j2me游戏起步之二(1)

用gamecanvas类处理graphics对象

本文,我将介绍graphics对象类在游戏中被应用的主要方面。在tumbleweed游戏中,我要画一个穿越草原的牛仔。在设计中,我将玩家的得分情况显示在屏幕底端,而游戏时间显示在顶端(为使样例不至太过复杂,我仅仅在玩家使用玩规定的时间后才自动终止游戏)。因为牛仔独自在走,我的想法是让他的背景向右或者向左滚动(否则在这个狭小的屏幕上他好象根本没走多远),而时间和分数的显示不动。

为了实现这个设计,我用jumpcanvas类来画屏幕顶端和底端稳定不动的显示条,而动态有趣的图形是用layermanager类来实现的。

jumpcanva类创建时,你必须先分析要使用的显示屏幕。有一些显示屏幕信息来源于graphics对象,一些来自于display对象,还有一些直接来源于gamecanvas类的方法。这些信息用来计算对象应当显示的位置,包括计算显示区域的尺寸,layermanager子类(jumpmanager)将在这些区域重复显示对象。如果你坚持维护java“一处写就,处处运行”的特性,基于动态屏幕尺寸而不是基于常量尺寸设置屏幕显示区域显然更方便合理。

当然,如果从一种目标显示设备转向另一种游戏需要巨大的代码更改量,显然应该根据不同设备显示维护多个游戏版本,运行时根据显示设备信息调用游戏的不同版本,而不是将所有代码置于一个版本中。

在样例游戏中,如果实际显示的设备与游戏中定义的显示设备差别太大,将有一个异常抛出以便jump类捕获并且显示警告给玩家。为了显得更专业,你应当确保给玩家的警告信息明确标明他针对当前的设备应该下载的版本号。

也许有点多余,但我后面仍然会告诉大家我认为顶端和底端区域合适的尺寸,paint(graphics g)方法将描绘出顶端的白色和底端的绿色,g.drawstring()方法用来记录使用的时间和分数。(不用问我为什么游戏的草原中都是颜色相同的绿草和风滚草;唯一的解释是我对java的了解当然远甚于西部荒原。)

layermanager类

一个midp游戏中富有乐趣的图形对象常常是用javax.microedition.lcdui.game.layer类的子类来展示。背景层是javax.microedition.lcdui.game.tiledlayer的实例化,游戏的主人公(和他的敌人)是javax.microedition.lcdui.game.sprite的实例,这两个类都是layer的子类。layermanager类用来组织所有的图形对象层,追加你的图层对象到管理器layermanager的顺序,决定了他们运行时被描绘的次序(后进先出,最先追加的最后显示)。规则是上面的图层将覆盖下面的图层,不过你可以通过创建包含透明区域的图象文件(上层)来部分地显示下层图层中你想要的部分。

layermanager类实际中最有用的也许是你可以创建一个远大于显示屏幕的图形对象,然后选择它将在屏幕中被显示的部分显示出来。你可以想象事先做出一幅巨大的、精心描绘的图画,然后用一张纸去覆盖它,而纸上有一个方孔在图画上自由移动。巨幅的整张图画代表你保存在laybermanager中的显示信息,而方孔是任一给定时刻在屏幕上显示图画的窗口。游戏代码设计中,一个比实际屏幕大得多的虚拟显示屏幕对于通常运行在小屏幕显示设备上的游戏是极其重要的,它将为你节省大量的时间和工作。

举个例子,比如你的游戏中包含主人公在一个复杂的地牢中摸索前进的场景,最麻烦的是你必须处理两个独立坐标系统。gamecanva的图形对象有一个坐标系,但是根据layermanager坐标系的要求,图形中的不同图层又需要被置于layermanager中。所以,一定要牢记layermanager.paint(graphics g, int x, int y)方法根据gamecanvas的坐标系在屏幕上描绘图层,而layermanager.setviewwindow(int x, int y, int width, int height)方法根据layermanager坐标系的要求设置layermanager的可视矩形的显示属性。

在样例中,我设计了一个简单的背景(仅仅是一系列重复显示的草丛),但我想让牛仔从右向左行走的时候总是显示在屏幕中央,所以我需要不断改变layermanager的图形可显示区域。这项工作是通过在layermanager的子类jumpmanager类中方法paint(graphics g)中调用setviewwindow(int x, int y, int width, int height)方法完成的。更准确地说,逻辑是这样的:gamethread中的主循环调用jumpcanvas.checkkeys()来检查按键状态,并且通知jumpmanager类牛仔此时应该向左、向右还是应该跳跃了。jumpcanva类通过调用setleft(boolean left)或者jump()方法将信息传至jumpmanager。如果信息表明牛仔此时应该向左走(向右与之类似),那么当gamethead调用jumpcanvas告诉jumpmanager继续的时候(循环的下一步),jumpmanager就告诉牛仔对象向左移动一个象素,同时通过向左移动视窗一个象素来保持牛仔在屏幕中央。你可以通过增加字段mycurrentleftx的值(这个作为x坐标值传至setviewwindow(int x,int y,int width,int height)),接着调用mycowboy.advance(gameticks, myleft)来实现这两个动作。

当然,我可以不移动牛仔,也不把他追加到layermanager,而只是独立地描绘他,这样也可以确保牛仔在屏幕中央。但显然通过将所有的移动对象置于相对静止的一系列图层中然后将视窗集中在牛仔对象上,借此保持所有对象的运动轨迹的方法要更容易。在通知牛仔前进的同时,风滚草对象sprites和青草对象tiledlayer也也同时移动,然后检测牛仔是否被风滚草撞倒(在以下章节里我将说明实现这个功能的更多细节)。在移动了所有游戏对象后,jumpmanager类调用wrap()方法来检查是否前端窗口到达了背景窗口的边缘,如果是,移动所有的有些对象以便视窗能继续在任一方向无限显示,接着jumpcanvas类重画每一个对象,再次执行游戏主循环。

wrap()方法需要多说几句。很不幸,如果你想让你的简单背景不确定地重复显示,layermanager类并不提供现成的隐藏方法. 当传至方法setviewwindow(int x, int y, int width, int height)的坐标参数值大于integer. max_value 时,layermanager的图形区域将被隐藏,但这似乎对我们的想法没有任何帮助。因此,你必须写自己的函数来避免牛仔sprite对象离开背景区域。

样例中,背景草丛总是在间隔数值grass.tile_width*grass.cycle后被重画,所以任何时候视窗的x坐标值(mycurrentleftx)都是背景图形宽度的整数倍。这样每次重画时,我都可以将视窗移回到显示中心,并且同时在相同的方向上移动sprites对象,这样显然能平滑地阻止牛仔到达背景边界。

listing 4 jumpmanager.java.

package.net.frog_parrot.jump;

import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.*;

/**

* this handles the graphics objects.

*

* @author carol hamer

*/

public class jumpmanager extends

javax.microedition.lcdui.game.layermanager {

//---------------------------------------------------------

// dimension fields

// (constant after initialization)

/**

* the x coordinate of the place on the game canvas where

* the layermanager window should appear, in terms of the

* coordinates of the game canvas.

*/

static int canvas_x;

/**

* the y coordinate of the place on the game canvas where

* the layermanager window should appear, in terms of the

* coordinates of the game canvas.

*/

static int canvas_y;

/**

* the width of the display window.

*/

static int disp_width;

/**

* the height of this object's graphical region. this is

* the same as the height of the visible part because

* in this game the layer manager's visible part scrolls

* only left and right but not up and down.

*/

static int disp_height;

//---------------------------------------------------------

// game object fields

/**

* the player's object.

*/

private cowboy mycowboy;

/**

* the tumbleweeds that enter from the left.

*/

private tumbleweed[] mylefttumbleweeds;

/**

* the tumbleweeds that enter from the right.

*/

private tumbleweed[] myrighttumbleweeds;

/**

* the object representing the grass in the background.

*/

private grass mygrass;

/**

* whether the player is currently going left.

*/

private boolean myleft;

/**

* the leftmost x coordinate that should be visible on the

* screen in terms of this objects internal coordinates.

*/

private int mycurrentleftx;

//-----------------------------------------------------

// gets/sets

/**

* this tells the player to turn left or right.

* @param left whether the turn is toward the left.

*/

void setleft(boolean left) {

myleft = left;

}

//-----------------------------------------------------

// initialization and game state changes

/**

* constructor sets the data and constructs the graphical objects.

* @param x the x coordinate of the place on the game canvas where

* the layermanager window should appear, in terms of the

* coordinates of the game canvas.

* @param y the y coordinate of the place on the game canvas where

* the layermanager window should appear, in terms of the

* coordinates of the game canvas.

* @param width the width of the region that is to be

* occupied by the layoutmanager.

* @param height the height of the region that is to be

* occupied by the layoutmanager.

*/

public jumpmanager(int x, int y, int width, int height)

throws exception {

 canvas_x = x;

 canvas_y = y;

 disp_width = width;

 disp_height = height;

 mycurrentleftx = grass.cycle*grass.tile_width;

 setviewwindow(0, 0, disp_width, disp_height);

 // create the player:

 if(mycowboy == null) {

mycowboy = new cowboy(mycurrentleftx + disp_width/2,

disp_height - cowboy.height - 2);

append(mycowboy);

 }

 // create the tumbleweeds to jump over:

 if(mylefttumbleweeds == null) {

mylefttumbleweeds = new tumbleweed[2];

for(int i = 0; i < mylefttumbleweeds.length; i++) {

 mylefttumbleweeds[i] = new tumbleweed(true);

 append(mylefttumbleweeds[i]);

}

 }

 if(myrighttumbleweeds == null) {

myrighttumbleweeds = new tumbleweed[2];

for(int i = 0; i < myrighttumbleweeds.length; i++) {

 myrighttumbleweeds[i] = new tumbleweed(false);

 append(myrighttumbleweeds[i]);

}

// create the background object:

if(mygrass == null) {

 mygrass = new grass();

 append(mygrass);

}

 }

 /**

* sets all variables back to their initial positions.

 */

 void reset() {

if(mygrass != null) {

 mygrass.reset();

}

if(mycowboy != null) {

 mycowboy.reset();

}

if(mylefttumbleweeds != null) {

 for(int i = 0; i < mylefttumbleweeds.length; i++) {

 mylefttumbleweeds[i].reset();

}

 }

 if(myrighttumbleweeds != null) {

for(int i = 0; i < myrighttumbleweeds.length; i++) {

 myrighttumbleweeds[i].reset();

}

 }

 myleft = false;

 mycurrentleftx = grass.cycle*grass.tile_width;

}

//-------------------------------------------------------

// graphics methods

/**

* paint the game graphic on the screen.

*/

public void paint(graphics g) {

 etviewwindow(mycurrentleftx, 0, disp_width, disp_height);

 paint(g, canvas_x, canvas_y);

}

/**

* if the cowboy gets to the end of the graphical region,

* move all of the pieces so that the screen appears to wrap.

*/

private void wrap() {

 if(mycurrentleftx &#37; (grass.tile_width*grass.cycle) == 0) {

if(myleft) {

 mycowboy.move(grass.tile_width*grass.cycle, 0);

 mycurrentleftx += (grass.tile_width*grass.cycle);

 for(int i = 0; i < mylefttumbleweeds.length; i++) {

mylefttumbleweeds[i].move(grass.tile_width*grass.cycle, 0);

 }

 for(int i = 0; i < myrighttumbleweeds.length; i++) {

myrighttumbleweeds[i].move(grass.tile_width*grass.cycle, 0);

 } else {

mycowboy.move(-(grass.tile_width*grass.cycle), 0);

mycurrentleftx -= (grass.tile_width*grass.cycle);

for(int i = 0; i < mylefttumbleweeds.length; i++) {

 mylefttumbleweeds[i].move(-grass.tile_width*grass.cycle, 0);

}

for(int i = 0; i < myrighttumbleweeds.length; i++) {

 myrighttumbleweeds[i].move(-grass.tile_width*grass.cycle, 0);

}

 }

}

 }

 //-------------------------------------------------------

 // game movements

 /**

 * tell all of the moving components to advance.

 * @param gameticks the remaining number of times that

 * the main loop of the game will be executed

 * before the game ends.

 * @return the change in the score after the pieces

 * have advanced.

 */

 int advance(int gameticks) {

int retval = 0;

// first you move the view window

// (so you are showing a slightly different view of

// the manager's graphical area).

if(myleft) {

 mycurrentleftx--;

} else {

 mycurrentleftx++;

}

// now you tell the game objects to move accordingly.

mygrass.advance(gameticks);

mycowboy.advance(gameticks, myleft);

for(int i = 0; i < mylefttumbleweeds.length; i++) {

 retval += mylefttumbleweeds[i].advance(mycowboy,

gameticks, myleft, mycurrentleftx, mycurrentleftx + disp_width);

 retval -= mycowboy.checkcollision(mylefttumbleweeds[i]);

}

for(int i = 0; i < mylefttumbleweeds.length; i++) {

 retval += myrighttumbleweeds[i].advance(mycowboy,

gameticks, myleft, mycurrentleftx, mycurrentleftx + disp_width);

 retval -= mycowboy.checkcollision(myrighttumbleweeds[i]);

}

// now you check if you have reached an edge of the viewable

// area, and if so, you move the view area and all of the

// game objects so that the game appears to wrap.

wrap();

return(retval);

 }

 /**

 * tell the cowboy to jump.

 */

 void jump() {

mycowboy.jump();

 }

}


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons