Posted on 2009-03-05 09:37
love1563 閱讀(361)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
j2me學(xué)習(xí)筆記
http://tech.ccidnet.com/art/1077/20050826/319351_1.html
筆者以前是做J2EE的,一個(gè)月前由于興趣所致開(kāi)始利用業(yè)余時(shí)間學(xué)習(xí)J2ME游戲開(kāi)發(fā)。在網(wǎng)上看了一通教程后,便準(zhǔn)備動(dòng)手一邊學(xué)一邊做一個(gè)簡(jiǎn)單的rpg游戲。雖然起點(diǎn)比較高,但感覺(jué)游戲難度越大,遇到的問(wèn)題也就越多。
這樣在問(wèn)題全部解決后,也就學(xué)的差不多了。另外只有做這種游戲才有學(xué)下去的興趣。以后我會(huì)每次把寫(xiě)成的源碼及思路整理成文章的形式供大家參考,互相學(xué)習(xí)進(jìn)步。
下面是游戲中所需要的部分圖片,還有的圖片下次寫(xiě)到了再上傳:
hero_left.png:
hero_right.png:
hero_up.png:
hero_down.png:

整個(gè)程序的主體在BraveCanvas類(lèi)中完成,該類(lèi)繼承于GameCanvas實(shí)現(xiàn)Runnable接口,由BraveMIDlet類(lèi)啟動(dòng)他,BraveMIDlet繼承MIDlet實(shí)現(xiàn)CommandListener接口。BraveMIDlet類(lèi)代碼如下:(由于只是一個(gè)簡(jiǎn)單的rpg,開(kāi)頭畫(huà)面和菜單全部省略了,改為由一個(gè)按扭啟動(dòng)游戲)
1
package brave;
2
3
import javax.microedition.lcdui.*;
4
import javax.microedition.midlet.*;
5
6
public class BraveMIDlet extends MIDlet implements CommandListener
{
7
private Display d;
8
private Command exitCommand;
9
private Command startCommand;
10
11
private BraveCanvas braveCanvas;
12
13
public BraveMIDlet()
{
14
d = Display.getDisplay(this);
15
exitCommand = new Command("退出", Command.EXIT, 1);
16
startCommand = new Command("開(kāi)始", Command.SCREEN, 1);
17
}
18
19
public void startApp()
{
20
// 創(chuàng)建BraveCanvas
21
braveCanvas = new BraveCanvas();
22
braveCanvas.addCommand(exitCommand);
23
braveCanvas.addCommand(startCommand);
24
braveCanvas.setCommandListener(this);
25
// 裝載BraveCanvas
26
d.setCurrent(braveCanvas);
27
}
28
29
public void pauseApp()
{
30
}
31
32
public void destroyApp(boolean unconditional)
{
33
}
34
35
public void commandAction(Command c, Displayable dpa)
{
36
37
String str_co = c.getLabel();
38
if (str_co.equals("開(kāi)始"))
{
39
// 運(yùn)行BraveCanvas中的線程(啟動(dòng)游戲)
40
braveCanvas.startup();
41
} else if (str_co.equals("退出"))
{
42
destroyApp(false);
43
notifyDestroyed();
44
}
45
}
46
}
47
1
package brave;
2
3
import javax.microedition.lcdui.game.GameCanvas;
4
import javax.microedition.lcdui.Graphics;
5
import java.io.IOException;
6
7
public class BraveCanvas extends GameCanvas implements Runnable
{
8
private boolean sign;
9
private Graphics g;
10
// 設(shè)置@符號(hào)的當(dāng)前位置
11
private int x, y;
12
13
public BraveCanvas()
{
14
super(true);
15
// 初始化@位置
16
x = getWidth() / 2;
17
y = getHeight() / 2;
18
}
19
20
public void startup()
{
21
this.sign = true;
22
Thread thread = new Thread(this);
23
// 啟動(dòng)線程
24
thread.start();
25
}
26
27
public void run()
{
28
g = getGraphics();
29
while (sign)
{
30
try
{
31
// @符號(hào)的移動(dòng)
32
input(g);
33
// @符號(hào)的顯示
34
paint(g);
35
// 這里應(yīng)該有詳細(xì)的計(jì)算,方便為上,置為15
36
Thread.sleep(15);
37
} catch (Exception e)
{
38
System.out.println("2:" + e);
39
}
40
}
41
}
42
43
public void input(Graphics g) throws IOException
{
44
int keystates = getKeyStates();
45
switch (keystates)
{
46
case UP_PRESSED:
47
y = Math.max(0, y - 1);
48
break;
49
case DOWN_PRESSED:
50
y = Math.min(getHeight(), y + 1);
51
break;
52
case LEFT_PRESSED:
53
x = Math.max(0, x - 1);
54
break;
55
case RIGHT_PRESSED:
56
x = Math.min(getWidth(), x + 1);
57
break;
58
}
59
}
60
61
public void paint(Graphics g)
{
62
// 設(shè)置畫(huà)布的背景色
63
g.setColor(0x000000);
64
// 把背景色畫(huà)滿(mǎn)畫(huà)布
65
g.fillRect(0, 0, getWidth(), getHeight());
66
// 設(shè)置畫(huà)布的前景色
67
g.setColor(0xffffff);
68
// 在畫(huà)布上寫(xiě)上@
69
g.drawString("@", x, y, Graphics.TOP | Graphics.LEFT);
70
// 刷新畫(huà)布
71
flushGraphics();
72
}
73
}
上面代碼運(yùn)行后,畫(huà)面上會(huì)出現(xiàn)一個(gè)"@"字符隨著鍵盤(pán)的左右而移動(dòng)。雖然這離rpg游戲相差很遠(yuǎn),但這是一
個(gè)游戲的基本框架。下面我們要做的就是把"@"換成我們的英雄。為了程序清晰,我們還要設(shè)計(jì)一個(gè)英雄類(lèi)Hero,該類(lèi)繼承于Sprite,這個(gè)類(lèi)里面要用到上面的圖片。
1
package brave;
2
3
import javax.microedition.lcdui.game.Sprite;
4
import javax.microedition.lcdui.Image;
5
import java.io.IOException;
6
7
public class Hero extends Sprite
{
8
// 設(shè)置英雄的當(dāng)前位置
9
private int x, y;
10
private BraveCanvas braveCanvas;
11
12
public Hero(Image image, int frameWidth, int frameHeight)
{
13
// 英雄的初始化
14
super(image, frameWidth, frameHeight);
15
}
16
17
public void setBraveCanvas(BraveCanvas braveCanvas)
{
18
this.braveCanvas = braveCanvas;
19
}
20
21
public void init(int x, int y)
{
22
// 英雄位置的初始化
23
this.x = x;
24
this.y = y;
25
}
26
27
public void afresh()
{
28
// 刷新英雄的位置
29
setPosition(this.x, this.y);
30
}
31
32
public void moveUp() throws IOException
{
33
// 英雄上移,并改為相應(yīng)的圖片
34
setImage(Image.createImage("/hero_up.png"), 17, 26);
35
nextFrame();
36
this.y = Math.max(0, y - 1);
37
}
38
39
public void moveDown() throws IOException
{
40
// 英雄下移,并改為相應(yīng)的圖片
41
setImage(Image.createImage("/hero_down.png"), 17, 26);
42
nextFrame();
43
this.y = Math.min(braveCanvas.getHeight(), y + 1);
44
}
45
46
public void moveLeft() throws IOException
{
47
// 英雄左移,并改為相應(yīng)的圖片
48
setImage(Image.createImage("/hero_left.png"), 17, 26);
49
nextFrame();
50
this.x = Math.max(0, x - 1);
51
}
52
53
public void moveRight() throws IOException
{
54
// 英雄右移,并改為相應(yīng)的圖片
55
setImage(Image.createImage("/hero_right.png"), 17, 26);
56
nextFrame();
57
this.x = Math.min(braveCanvas.getWidth(), x + 1);
58
}
59
60
}
然后把BraveCanvas類(lèi)稍做修改,把"@"換成英雄
1
package brave;
2
3
import javax.microedition.lcdui.game.GameCanvas;
4
import javax.microedition.lcdui.Graphics;
5
import java.io.IOException;
6
import javax.microedition.lcdui.Image;
7
8
public class BraveCanvas extends GameCanvas implements Runnable
{
9
private boolean sign;
10
private Graphics g;
11
private Hero hero;
12
13
public BraveCanvas()
{
14
super(true);
15
}
16
17
public void startup()
{
18
this.sign = true;
19
try
{
20
Image heroimage = Image.createImage("/hero_up.png");
21
hero = new Hero(heroimage, 17, 26);
22
hero.setBraveCanvas(this);
23
hero.init(40, 40);
24
} catch (Exception e)
{
25
e.printStackTrace();
26
}
27
Thread thread = new Thread(this);
28
thread.start();
29
}
30
31
public void run()
{
32
g = getGraphics();
33
while (sign)
{
34
try
{
35
input(g);
36
paint(g);
37
Thread.sleep(15);
38
} catch (Exception e)
{
39
System.out.println("2:" + e);
40
}
41
}
42
}
43
44
public void input(Graphics g) throws IOException
{
45
int keystates = getKeyStates();
46
switch (keystates)
{
47
case UP_PRESSED:
48
hero.moveUp();
49
break;
50
case DOWN_PRESSED:
51
hero.moveDown();
52
break;
53
case LEFT_PRESSED:
54
hero.moveLeft();
55
break;
56
case RIGHT_PRESSED:
57
hero.moveRight();
58
break;
59
}
60
hero.afresh();
61
}
62
63
public void paint(Graphics g)
{
64
g.setColor(0x000000);
65
g.fillRect(0, 0, getWidth(), getHeight());
66
g.setColor(0xffffff);
67
hero.paint(g);
68
flushGraphics();
69
}
70
}
這樣,我們的英雄就可以在畫(huà)面上移動(dòng)了,不過(guò)英雄比較簡(jiǎn)單,沒(méi)有hp,mp等等,只是一個(gè)圖片。而且如果大家感覺(jué)英雄的腳步過(guò)快,可以在BraveCanvas類(lèi)中的Hero初始化時(shí)設(shè)置:
1
hero.setFrameSequence(new int[]
{0, 0, 1, 1, 0, 0, 2, 2 });