分享
 
 
 

使用GameCanvas制作星空效果

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

文章来源:J2ME开发网

MIDP2.0中提供了游戏开发专用的API,比如GameCanvas等类。他们位于javax.microedition.lcdui.game包内。本文介绍GameCanvas的基本使用方法并实现一种滚动星空的效果。您可以参考Game Canvas Basic获得更详细的信息。

GameCanvas是Canvas的子类,因此他同样继承了Canvas类的一些特性,比如showNotify()方法会在Canvas被显示在屏幕的时候调用,而hideNotify()会在Canvas离开屏幕的时候被调用。我们可以把他们当作监听器来使用,用于初始化和销毁资源。比如

// When the canvas is shown, start a thread to

// run the game loop.

protected void showNotify()

{

random = new Random();

thread = new Thread(this);

thread.start();

}

// When the game canvas is hidden, stop the thread.

protected void hideNotify()

{

thread = null;

}

在游戏开发中最重要的就是接受用户触发的事件然后重新绘制屏幕,通常我们使用getKeyStates()方法判断哪个键被按下了,然后绘制屏幕,调用flushGraphics()。在GameCanvas中,系统事实上已经为我们实现了双缓冲技术,因此每次我们绘制的时候就是在off-screen上绘制的。结束后通过flushGraphics把它复制到屏幕上去。下面是典型的接受事件、处理逻辑、绘制屏幕的代码。

// Get the Graphics object for the off-screen buffer

Graphics g = getGraphics();

while (true) {

// Check user input and update positions if necessary

int keyState = getKeyStates();

if ((keyState & LEFT_PRESSED) != 0) {

sprite.move(-1, 0);

}

else if ((keyState & RIGHT_PRESSED) != 0) {

sprite.move(1, 0);

}

// Clear the background to white

g.setColor(0xFFFFFF);

g.fillRect(0,0,getWidth(), getHeight());

// Draw the Sprite

sprite.paint(g);

// Flush the off-screen buffer

flushGraphics();

}

下面开始实现我们滚动星空的效果,其实设计的思想非常简单。我们启动一个线程,使用copyArea()方法把屏幕的内容往下复制一个像素的距离。然后绘画第一个空白的直线,随机的在直线上绘画点儿,这样看起来就像星空一样了。逻辑代码如下:

// The game loop.

public void run()

{

int w = getWidth();

int h = getHeight() - 1;

while (thread == Thread.currentThread())

{

// Increment or decrement the scrolling interval

// based on key presses

int state = getKeyStates();

if ((state & DOWN_PRESSED) != 0)

{

sleepTime += SLEEP_INCREMENT;

if (sleepTime > SLEEP_MAX)

sleepTime = SLEEP_MAX;

} else if ((state & UP_PRESSED) != 0)

{

sleepTime -= SLEEP_INCREMENT;

if (sleepTime < 0)

sleepTime = 0;

}

// Repaint the screen by first scrolling the

// existing starfield down one and painting in

// new stars...

graphics.copyArea(0, 0, w, h, 0, 1, Graphics.TOP | Graphics.LEFT);

graphics.setColor(0, 0, 0);

graphics.drawLine(0, 0, w, 0);

graphics.setColor(255, 255, 255);

for (int i = 0; i < w; ++i)

{

int test = Math.abs(random.nextInt()) % 100;

if (test < 5)

{

graphics.drawLine(i, 0, i, 0);

}

}

flushGraphics();

// Now wait...

try

{

Thread.currentThread().sleep(sleepTime);

} catch (InterruptedException e)

{

}

}

}

下面给出源代码

/*

* License

*

* Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.

*

*/

import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.*;

import javax.microedition.midlet.*;

public class GameCanvasTest extends MIDlet implements CommandListener

{

private Display display;

public static final Command exitCommand = new Command("Exit", Command.EXIT,

1);

public GameCanvasTest()

{

}

public void commandAction(Command c, Displayable d)

{

if (c == exitCommand)

{

exitMIDlet();

}

}

protected void destroyApp(boolean unconditional)

throws MIDletStateChangeException

{

exitMIDlet();

}

public void exitMIDlet()

{

notifyDestroyed();

}

public Display getDisplay()

{

return display;

}

protected void initMIDlet()

{

GameCanvas c = new StarField();

c.addCommand(exitCommand);

c.setCommandListener(this);

getDisplay().setCurrent(c);

}

protected void pauseApp()

{

}

protected void startApp() throws MIDletStateChangeException

{

if (display == null)

{

display = Display.getDisplay(this);

initMIDlet();

}

}

}

/*

* License

*

* Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.

*/

import java.util.Random;

import javax.microedition.lcdui.*;

import javax.microedition.lcdui.game.GameCanvas;

// A simple example of a game canvas that displays

// a scrolling star field. Use the UP and DOWN keys

// to speed up or slow down the rate of scrolling.

public class StarField extends GameCanvas implements Runnable

{

private static final int SLEEP_INCREMENT = 10;

private static final int SLEEP_INITIAL = 150;

private static final int SLEEP_MAX = 300;

private Graphics graphics;

private Random random;

private int sleepTime = SLEEP_INITIAL;

private volatile Thread thread;

public StarField()

{

super(true);

graphics = getGraphics();

graphics.setColor(0, 0, 0);

graphics.fillRect(0, 0, getWidth(), getHeight());

}

// The game loop.

public void run()

{

int w = getWidth();

int h = getHeight() - 1;

while (thread == Thread.currentThread())

{

// Increment or decrement the scrolling interval

// based on key presses

int state = getKeyStates();

if ((state & DOWN_PRESSED) != 0)

{

sleepTime += SLEEP_INCREMENT;

if (sleepTime > SLEEP_MAX)

sleepTime = SLEEP_MAX;

} else if ((state & UP_PRESSED) != 0)

{

sleepTime -= SLEEP_INCREMENT;

if (sleepTime < 0)

sleepTime = 0;

}

// Repaint the screen by first scrolling the

// existing starfield down one and painting in

// new stars...

graphics.copyArea(0, 0, w, h, 0, 1, Graphics.TOP | Graphics.LEFT);

graphics.setColor(0, 0, 0);

graphics.drawLine(0, 0, w, 0);

graphics.setColor(255, 255, 255);

for (int i = 0; i < w; ++i)

{

int test = Math.abs(random.nextInt()) % 100;

if (test < 5)

{

graphics.drawLine(i, 0, i, 0);

}

}

flushGraphics();

// Now wait...

try

{

Thread.sleep(sleepTime);

} catch (InterruptedException e)

{

}

}

}

// When the canvas is shown, start a thread to

// run the game loop.

protected void showNotify()

{

random = new Random();

thread = new Thread(this);

thread.start();

}

// When the game canvas is hidden, stop the thread.

protected void hideNotify()

{

thread = null;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有