http://tech.ccidnet.com/art/1077/20050826/319351_1.html
筆者以前是做J2EE的,一個月前由于興趣所致開始利用業余時間學習J2ME游戲開發。在網上看了一通教程后,便準備動手一邊學一邊做一個簡單的rpg游戲。雖然起點比較高,但感覺游戲難度越大,遇到的問題也就越多。
這樣在問題全部解決后,也就學的差不多了。另外只有做這種游戲才有學下去的興趣。以后我會每次把寫成的源碼及思路整理成文章的形式供大家參考,互相學習進步。
下面是游戲中所需要的部分圖片,還有的圖片下次寫到了再上傳:
hero_left.png:

hero_right.png:

hero_up.png:

hero_down.png:

整個程序的主體在BraveCanvas類中完成,該類繼承于GameCanvas實現Runnable接口,由BraveMIDlet類啟動他,BraveMIDlet繼承MIDlet實現CommandListener接口。BraveMIDlet類代碼如下:(由于只是一個簡單的rpg,開頭畫面和菜單全部省略了,改為由一個按扭啟動游戲)
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("開始", Command.SCREEN, 1);
17
}
18
19
public void startApp() {
20
// 創建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("開始")) {
39
// 運行BraveCanvas中的線程(啟動游戲)
40
braveCanvas.startup();
41
} else if (str_co.equals("退出")) {
42
destroyApp(false);
43
notifyDestroyed();
44
}
45
}
46
}
47

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

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
// 設置@符號的當前位置
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
// 啟動線程
24
thread.start();
25
}
26
27
public void run() {
28
g = getGraphics();
29
while (sign) {
30
try {
31
// @符號的移動
32
input(g);
33
// @符號的顯示
34
paint(g);
35
// 這里應該有詳細的計算,方便為上,置為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
// 設置畫布的背景色
63
g.setColor(0x000000);
64
// 把背景色畫滿畫布
65
g.fillRect(0, 0, getWidth(), getHeight());
66
// 設置畫布的前景色
67
g.setColor(0xffffff);
68
// 在畫布上寫上@
69
g.drawString("@", x, y, Graphics.TOP | Graphics.LEFT);
70
// 刷新畫布
71
flushGraphics();
72
}
73
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

上面代碼運行后,畫面上會出現一個"@"字符隨著鍵盤的左右而移動。雖然這離rpg游戲相差很遠,但這是一
個游戲的基本框架。下面我們要做的就是把"@"換成我們的英雄。為了程序清晰,我們還要設計一個英雄類Hero,該類繼承于Sprite,這個類里面要用到上面的圖片。
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
// 設置英雄的當前位置
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
// 英雄上移,并改為相應的圖片
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
// 英雄下移,并改為相應的圖片
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
// 英雄左移,并改為相應的圖片
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
// 英雄右移,并改為相應的圖片
55
setImage(Image.createImage("/hero_right.png"), 17, 26);
56
nextFrame();
57
this.x = Math.min(braveCanvas.getWidth(), x + 1);
58
}
59
60
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

然后把BraveCanvas類稍做修改,把"@"換成英雄
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
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

這樣,我們的英雄就可以在畫面上移動了,不過英雄比較簡單,沒有hp,mp等等,只是一個圖片。而且如果大家感覺英雄的腳步過快,可以在BraveCanvas類中的Hero初始化時設置:
1
hero.setFrameSequence(new int[]{0, 0, 1, 1, 0, 0, 2, 2 });
