该用户从未签到
|
推(搬)箱子,又名Sokoban,仓库番等,是一款堪称古玩级的电脑游戏。 7 Q7 v. T% \* |$ r5 @
6 ?+ S0 C/ H5 u: T1 U t( X1 f提起它,笔者相信没什么人会感觉到陌生,更没什么生物会连听都没听说过。它的发展历史之久远,甚至超越了俄罗斯方块(1988年电脑游戏化)。
0 j' p: _" U3 u$ Y6 y/ j6 D) K0 T( P3 Z) b2 `8 z, m$ y
这款游戏最初起源于日本,是个很难争辩的事实(我知道有人反对,但笔者确实找不到什么有力的反对证据)。他由日本人(哎……)今川宏行在1981年创立游戏规则,并于1982年经日本软件公司Thinking Rabbit正式发布。比较遗憾的是,早期的推箱子并没有PC版,笔者在网络上搜索到的老版游戏也大多为90年以前的Mac OS下程式。
0 q8 f4 g: d( ~. g3 X4 J
/ p; }" Z9 W9 E- m$ @- g7 ?但说起真正令推箱子风靡于PC机的,却该感谢我们的台湾同胞李果兆先生。是他在1994年开发的仓库世家,才真正令推箱子游戏在世界各地大受推崇;仔细说来,推箱子这款小游戏之所以能有今时今日的声望与地位,固然有今川宏行的开创之功,但若说到贡献最大,承前启后的,则非中国台湾的李果兆先生莫属。
. ~0 i* d ]: F8 }8 q G' { U* F4 R3 Z5 w8 N. F
推箱子游戏的规则非常简单,就是用尽量少的推动或移动把所有箱子都推到目标点上。箱子只能推动而不能拉动;一次只能推动一个箱子。然而,尽管它的规则是很简单的,但对于不同难度的关卡,所需要的脑力却是截然不同的,有些关卡可能会花费您几个小时、几天甚至几个月的时间,也正是这种简单性和复杂性的结合,最终令推箱子类游戏风靡全球!
8 V1 Y+ F/ x( m5 W- [) K( J$ o- K% N) G7 g
本回笔者在Blog中提供的,就是一款java版推箱子游戏的简单实现。! v, Z/ W5 w2 M- y4 K% A# W% j
' y1 t2 ?; p. b: I4 O$ w笔者设定[上、下、左、右]为方向控制 ,[S]键为后退到上一步操作,[ESC]为重新开始当前关卡,点击键盘上对应关卡的数字键可以直接选关,需要注意的是笔者以HP限制了角色的移动次数,HP归0则挑战失败。8 y: h$ @7 ?5 P4 W
3 {. e$ N7 ^& ]/ o0 |: R; i; |目前版本仅提供有5关,有需要者可参考同类游戏自行扩充,游戏源码在jar内。/ A; H5 @: a. e j6 I, m5 h
游戏截图: 2 d1 g7 i- u4 H% k/ S2 ]7 l/ w
/ b7 T. x# Y" j& m I0 M
& I2 Z# p; t, I) g; a5 `: r2 x
. l6 w) I5 f+ t; |9 l, K
# k7 `9 c! M) K
核心代码:
9 N- ?( L- p# w6 V8 \: x1 TSokoban.java - package org.loon.game.simple.sokoban.control;
6 E& h5 k/ F* [# F/ B# ]& @ - import java.awt.Color; / [7 G9 E+ n# l, ~; U6 N1 e7 e
- import java.awt.Font;
& j y+ r+ j1 f" O( B/ F% f* K - import java.awt.Graphics; 0 v+ ^. ~! _. ?& v, {0 x2 x& Z
- import java.awt.Image;
$ J/ y8 J. o6 `& P - import java.awt.event.KeyEvent; $ K9 d3 ~% b `8 v. u& R& G9 m
- import org.loon.game.simple.sokoban.GraphicsUtils; ) Y* Z- S% Q- A$ c# C
- import org.loon.game.simple.sokoban.LSystem; ) t# Q# U2 U2 I( F; }, x0 H
- import org.loon.game.simple.sokoban.SimpleControl; ; E W+ g7 T; z( n
- /**
6 {( H+ i/ G9 n - * Copyright 2008 - 2009 + {& }& F6 I+ ~, ?4 V' G' h) g2 x
- * P+ t+ P, K6 m9 W9 X2 x' `; x7 Q
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 m& N; I L. T9 i3 F: v
- * use this file except in compliance with the License. You may obtain a copy of 9 R# U0 M5 z3 I' n
- * the License at 2 Y. Q" H @# A7 F
- *
, y E# v2 w6 o8 H* I% E! m5 V - * [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
7 A9 R- f% F; p7 d& T2 @ - *
! S" P) }, o0 H8 }9 @) q( C - * Unless required by applicable law or agreed to in writing, software
% z: P: q- F, ] [( S; x - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- z3 l. [ u z, _ - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
) E0 n2 D+ F: C, r - * License for the specific language governing permissions and limitations under
) h" g) f( G6 E2 x2 C$ ~ - * the License. 9 q9 D: ~: k F6 ^4 @! M! l
- * # f* ]; ^- l, a3 y) c
- * @project loonframework ; [' b- P. z6 p" {" c. Z$ d
- * @author chenpeng ' B/ Q6 P4 i$ N- F1 m; T- G
- * @email <a title="" href="http://hi.baidu.com/ceponline" mce_href="http://hi.baidu.com/ceponline" target="_blank">ceponline</a>@yahoo.com.cn
( } P% q) F" q5 b; [5 L( l% ~) B8 ^ - * @version 0.1
9 E/ ]* i9 M9 P3 h2 k$ ], u - */ * ?0 p# {( m9 u# J4 c
- public class Sokoban extends SimpleControl {
; }' W9 ?1 G. k - /** 3 b5 K# C3 K) T3 m3 O
- *
7 O/ {7 S* H1 t& |' O! s - */ - c( a- F! p/ [9 a- f
- private static final long serialVersionUID = 1L;
! ?0 ]% t1 O `- o - private Image backImage = GraphicsUtils.loadImage("image/back1.jpg");
H; {& M" ^; Q! G* s7 p j3 W - private Image screenImage; 0 T$ z, x+ x& I6 R7 O
- // 墙壁图
N) G2 e7 C; M& o! L& e4 A - private Image floorImage[];
" K. ^. q5 Q8 l+ g. S% O% M/ K - // 角色精灵
9 v" ]7 G7 k( S# M - private RpgSprite sprite; 3 \7 f1 Z* H2 N8 o
- // 窗体显示图
2 `* j& t( P/ C" {6 U, k - private Graphics screen;
4 o: N. a$ h* g% p/ S5 c - private String message = "按下 [Enter] 开始进行游戏";
K2 z+ N' P/ O; q' S" h - private String m_stageName[] = { "关卡1", "关卡2", "关卡3", "关卡4", "关卡5", "关卡6", # L8 S: [9 B! d6 _" B; D
- "关卡7" }; 1 m+ K* v5 t5 {5 l6 G; S/ N
- private int CS = 32;
* f* |/ \, {# ]. ? W+ r - private int maxX, maxY, comps; - n' S7 M8 {2 O1 T( k! T
- private int stageNo;
; {; \' a" b1 j0 l2 T - private boolean complete[];
: Y6 c6 R8 o) u+ @7 B+ j: X9 A - private boolean boxMove[][];
0 K- y3 G$ d5 J" U - private int hp[];
' V* g, j5 D; H& f; s6 n - private int stageStates[][][]; 8 n; _# u0 F% ?& D- c, _0 t
- private int ons[][]; ' g" s9 E8 r& x; l
- private int role[];
4 x& P6 X( N3 I% J4 T - private int rolex[]; $ w0 V: |3 D1 h; l- V0 V
- private int roley[]; " _* c- D7 N. j, Q
- private int mapx[];
2 P! d% D0 m5 _, U$ ^: R% \ - private int mapy[]; j }/ g) p' L9 Y* `" g! }
- private int sleep = 0; # c7 Z- c5 M8 ]; X" e. k5 i
- private boolean succeed = false;
* M' v; z, W5 G/ `5 H - private Stage stage;
8 L* ~. |9 v0 p7 e - public Sokoban(Stage stage) {
5 b, q( Q9 ~) Y7 Y - this.stage = stage; + X7 q( k, Y7 F! L& g
- this.floorImage = new Image[4]; 0 k$ s9 ?6 O9 [6 I2 {, O" o
- this.sprite = new RpgSprite("image/role1.gif"); * W) p9 Q( }0 x" W7 \
- this.maxX = 16; 5 b |1 Z7 |5 e5 W
- this.maxY = 13; ! q: |5 ~# r/ v2 l+ Q+ g
- this.comps = 0; / ?9 a `: U( s+ I& b7 M% V
- this.complete = new boolean[stage.getMaxStageNo()];
4 M! y. j" I5 b8 r) k6 G) C) \ - this.boxMove = new boolean[stage.getMaxStageNo()][1500]; + |2 X$ |$ b$ P$ B( |
- this.ons = new int[stage.getMaxStageNo()][1500];
: e2 ^4 d) m" v1 Y" @ - this.hp = new int[stage.getMaxStageNo()];
; Q4 j( d, f0 f$ }$ p3 e. d; x - this.stageStates = new int[stage.getMaxStageNo()][maxY][maxX];
! m/ |$ y' ] I" O! c% B - this.role = new int[stage.getMaxStageNo()]; / |) F) P( [ @. `6 ^- A
- this.rolex = new int[stage.getMaxStageNo()];
* M/ c! Q' b. y# r - this.roley = new int[stage.getMaxStageNo()];
1 Y. P+ u6 C; ?4 q5 t8 s* T - this.mapx = new int[stage.getMaxStageNo()];
# q# y: ?4 x- C9 j0 E1 c1 G - this.mapy = new int[stage.getMaxStageNo()]; 3 h- W. Q. f8 C# T3 d1 b$ j
- for (int j = 0; j < 4; j++) {
3 O+ X5 X5 }9 q) p" o - floorImage[j] = GraphicsUtils.loadImage("image/back" + (j + 1)
9 x! ]* U0 \, ?& K7 H! x$ n8 p - + ".gif");
% w/ |$ H: i2 l/ y# g - }
( u* J& e( I8 p' ? - this.screenImage = GraphicsUtils.createImage(CS * maxX + CS, CS # u4 }7 w, E) K5 d4 g6 B" V
- * (maxY + 1) + 32, true); 5 ~: h6 `; u6 ^, I
- this.screen = screenImage.getGraphics();
: v8 B/ O* s* j7 |! B, N$ O6 F - for (stageNo = 0; stageNo < stage.getMaxStageNo(); stageNo++) { 4 l2 ^, Z0 c" [4 S; c7 P
- this.setupStage(); $ {! B3 C9 o' d3 v7 {" f) K; w
- }
% ]0 c, [8 R* f. S - this.stageNo = -1;
! Z2 T& O( j& G/ u* b - // 开场界面
! ]+ Y0 W6 ^) T4 ^% f; }$ a - this.openDraw(); . y* |! \! [. [+ t2 S, G4 R
- } $ Z, M; m, L- `
- /** . S5 b9 t6 _. ]( |& e1 u
- * 刷新关卡
3 k5 r2 s! z7 K/ b - *
9 h" s. n% h- W# S - */ # N, n% j; @0 i6 z w7 l
- public synchronized void reset(int stageNo) {
& r: j! e, o7 H - this.stageNo = stageNo;
8 a: ~5 ?/ b7 P5 T( G& t! o - this.stage.getMode()[stageNo] = 1; . _) O# {' Y3 |' `
- this.message = null;
4 ~& B/ p' r/ B7 ]5 Q8 O - // 设定关卡 ! r7 A# ^9 H0 q; g
- this.setupStage(); : {, @# M; \( K1 p% J2 y
- // 重绘背景 ! R% }2 B& J0 N) _9 k
- this.background(); ~( o4 \/ g+ ?6 V {1 g
- // 重绘游戏屏幕 4 a/ [- L. n4 q( V, m9 G! K/ i$ g
- this.drawScreen(); , A8 d: C* C7 ?+ ]5 p4 L
- }
j+ E) v9 l; e# q5 q( W# T - /** 9 }- p, b% K5 ^. B
- * 绘制屏幕背景 , e' e- r% o/ L9 c% r" ~+ i
- * % \% V- \& n K. l
- */
' ^3 J" v* m6 y1 { - public synchronized void background() {
) z/ \# O' w7 o! z$ A% _0 q - screen.drawImage(backImage, 0, 0, null);
+ m2 f/ j' v, B9 e0 Z - screen.setColor(Color.black);
* K& w8 B7 b7 B! ^4 s* L7 s4 W - screen.fillRect(0, LSystem.HEIGHT - 40, LSystem.WIDTH, 40); ; V' @6 } m( H4 y
- } 3 G# M \- G' F$ ^
- /** + H7 v& A% v' I+ W% }) b- ]1 {
- * 绘制屏幕图像
; H* P/ G1 Q, d6 X8 I+ f - * 0 M! x5 x9 A, r0 ^
- */
; ^- M7 p J/ M) w# D - public synchronized void drawScreen() { 8 W* ~' \* R2 U
- if (stageNo >= stage.getMaxStageNo()) {
' M: d0 y" c; Y6 Z" G - stageNo = 0;
$ s/ g1 M! ]+ t! t" R7 g - }
8 i$ E9 n m" X6 R - for (int i = 0; i < mapy[stageNo]; i++) { - \$ p. n+ T; {! u5 n3 d& M
- for (int j = 0; j < mapx[stageNo];) { 4 {# i& x: d, h! U5 u2 X
- switch (stageStates[stageNo][i][j]) { 8 T4 [5 e' s% J! t3 U9 m' A2 s
- case 2: 5 k+ C* }; E8 ~; X6 R* X
- case 3: 6 _' d& g- L. B5 F: q( ~- |
- case 4:
y# q: M9 i+ Z/ o5 n2 m$ H - case 5: ' a. w5 w/ d; H2 Q3 y, @# `. J: z
- screen.drawImage(floorImage[1], moveX(j), moveY(i), null);
0 A; @7 s% N. E b" J8 h# c0 R - default:
/ X. y1 q2 g' z/ x - j++;
$ w& e& o/ b5 T' { - break;
# F* _) f5 T& u3 H% n - }
o0 ?8 S$ n0 Y6 v" E* r. K - } $ @! R7 T4 F7 t( d
- } 0 E5 ]$ a4 o( W3 \, E
- for (int n = 0; n < mapy[stageNo]; n++) { 1 Q( y& D+ I& x$ b; |1 S+ F
- for (int l = 0; l < mapx[stageNo]; l++) , M; O4 I/ L/ ]' K
- switch (stageStates[stageNo][n][l]) {
3 M/ V; n' [1 x5 X - case 1: 1 [1 ?: R [& r; e2 L
- screen.drawImage(floorImage[0], moveX(l), moveY(n), null); / U0 S5 U4 m' K$ d4 v; U7 y
- screen.setColor(new Color(32, 0, 0)); ( b+ Q }+ W2 D9 ~) h6 k0 b" O
- screen.drawLine(moveX(l), moveY(n + 1), moveX(l) + CS - 1, 8 k! r" j0 c, k5 D! n# r3 E
- moveY(n + 1)); 0 ~+ v8 I5 I8 c
- if (l == 0 || l > 0 && stageStates[stageNo][n][l - 1] != 1) {
/ g X9 F2 y( z# E. M - screen.setColor(new Color(160, 128, 96));
% [, @8 A! g3 @% j6 e# J - screen.drawLine(moveX(l), moveY(n) + 1, moveX(l), * @ Z5 s$ O1 f% H0 ^( H
- moveY(n) + CS - 2); 3 T- w0 K" e% R5 s, ~* M/ W
- } 1 {& i) l' w. }- C! b. K5 k, c
- if (l == mapx[stageNo] - 1 || l < mapx[stageNo] - 1 7 Z2 I4 J, C' y0 a
- && stageStates[stageNo][n][l + 1] != 1) {
8 h/ I- G9 i6 [1 o! P0 | - screen.setColor(new Color(72, 64, 64)); 3 i0 `$ q6 T) o0 i& a
- screen.drawLine(moveX(l) + CS - 1, moveY(n), moveX(l)
. @0 T- n4 }9 ~) z4 d% N7 m6 Y - + CS - 1, moveY(n) + CS - 2); 5 K5 ?2 V7 C9 \" S' j7 ]8 F
- } " U0 k; v, D, j6 c% `
- break;
5 _9 {$ N4 a* S6 @& B - case 2:
* M. ]% M/ y( g7 C4 W. w4 C - case 3:
# q( n* u$ {$ o; t) B% e: Z - case 4: 0 k6 T2 Q- e/ l, f
- switch (stageStates[stageNo][n][l]) { # ]3 S: t, R) O! C# j- H7 v
- default:
5 f: D- ~ w6 a5 G9 A, C; h2 h8 n - break;
4 d6 G* \8 p3 ?$ s - case 2:
t8 m: K( L- H - case 3:
) i8 M% |. v+ w! z - screen.drawImage(floorImage[3], moveX(l), moveY(n), 2 L. B1 q$ U3 {; M7 U5 W7 |
- null); 8 l. B" c, ]; x$ q) ?
- break; 3 c/ Q+ r5 [1 B" q! k( E8 O1 U
- case 4:
4 P% z9 E/ j) S: J# ^$ C - break; 1 o6 |/ y/ F5 A# P$ B- }3 @2 ?
- }
5 g0 M! I* Z/ }0 n - if (stageStates[stageNo][n][l] != 3) ) [+ R" @- y# `: W; P2 s
- screen.drawImage(floorImage[2], moveX(l), moveY(n),
4 P3 |9 ?1 E0 T' ^7 `+ J2 c - null);
, O; ]# c% Q" F& ` - break;
W; k; Z' Q8 o4 ]' o5 v3 J - default:
7 h, b7 l& O6 L# x. m) w s B/ b - break; 8 j f$ W/ [' A4 r' v
- }
) L, g9 W9 l7 I: d2 ] - } ; i K# G4 |& U) l, S) ^
- // System.out.println(role[stageNo]);
8 S& C* I. h- a; `7 b4 J - screen.drawImage(sprite.getOnlyMove(role[stageNo]),
" w1 d, u7 s1 X; i3 N+ x; i - moveX(rolex[stageNo]), moveY(roley[stageNo]), null); 4 `8 N+ H6 M- i8 s) _( N
- if (stageStates[stageNo][roley[stageNo]][rolex[stageNo]] == 4) { . K6 g& s( x5 ?% C% L) T: D4 h
- screen.drawImage(floorImage[2], moveX(rolex[stageNo]),
) e% g/ W8 z$ J' a) c) L - moveY(roley[stageNo]), null); F+ u( i- P7 c7 v/ d# w5 b
- } 5 d1 J# l; Q: d$ N
- setupDisplay();
, z/ g+ y% V9 }% c! e - }
. N6 i0 ~" t8 F Y* P& h8 U - /**
. } B5 K. t* Q; p1 q B - * 定位到实际的X座标
2 p5 G# S8 \- Z! C" O1 z2 K - * 3 ~ N6 a% M+ i
- * @param i ; x7 e; |0 J$ t8 F9 i
- * @return
7 W1 ^7 i7 \; P% Q/ g# l- }" ] - */ 7 |5 }9 S6 O4 d3 `, G2 p
- public int moveX(int i) { + t0 \/ N+ X) L* b/ f
- return (CS * ((maxX - mapx[stageNo]) + 1)) / 2 + CS * i; ) e2 i# V' x7 ?: @* \
- }
& w& @' H( ^% v" J% h - /** . ~1 Z' H7 I _5 l
- * 定位到实际的Y座标 1 H1 C {# |8 j V: b" W2 K
- *
) |/ p3 l5 n8 t, U. X; y - * @param i
* e) q1 k' P' v/ Z" E - * @return
$ m$ y/ U: k2 E2 a+ \ - */ ! g0 o. D) U: E! _9 ]
- public int moveY(int i) {
. C% _: t! [2 A- P4 C - return (CS * ((maxY - mapy[stageNo]) + 1)) / 2 + CS * i;
7 t# b$ ~: e' U M - }
7 T! y0 @0 g: G" r {6 k - /**
8 q. ]( v1 j$ @ l: c - * 执行操作
! e& _! ^) ^8 V/ W y0 J: n - * / j- S+ V X" l* C' l' |$ S7 _& Z+ D
- */
* T4 v0 M* Q8 D' T - public synchronized void execute() { 2 Q# e5 M# R4 O9 [ \ b5 n
- boolean flag = true; 7 Y& d- _% k) r8 y# I* G" h: t
- for (int i = 0; i < mapy[stageNo]; i++) { $ N/ g. |, F, d% C8 N: u
- for (int j = 0; j < mapx[stageNo]; j++) { u; @; ~/ L- Q- H; R- C
- if (stageStates[stageNo][i][j] == 4) {
7 c9 ]. Q; o @ - flag = false;
1 I9 a- T) K6 x. { R2 ]8 o# ^ - } 6 c5 C, v5 L+ Y. a; f7 D3 P
- }
3 ]" T; M' z& g* A8 ^3 w - }
. `+ Q+ _) f. E1 t; \: t - if (flag) { 7 C& P/ a! Q R1 u7 b1 E
- stage.getMode()[stageNo] = 3;
- p9 n# s" D% e$ ~( s - complete[stageNo] = true;
: `" Z' r h" Z - } else if (hp[stageNo] == 0) {
" e; Q8 e2 _+ C - stage.getMode()[stageNo] = 2;
0 u* x5 h7 p: V! ^ - } 3 f8 Y, v6 y4 f
- comps = 0;
; B& V9 S) H, V2 n - for (int n = 0; n < stage.getMaxStageNo(); n++) {
- i' g2 {9 L+ X! k/ e$ ~7 k1 [( w$ }/ T - if (complete[n]) { & o- i0 F! G* r
- comps++;
0 x I# @+ t: c2 @ - } ) {3 b. o0 H2 F
- } / Y+ m! C8 _& g4 Y' \$ t8 Y
- }
( R- v) r) U0 d- E9 `3 O: ? - /** + X' Y9 P( Q* y; v W+ S, w
- * 键盘事件处理 7 N% M: H. R! @1 k
- */
: d5 p) L/ O# u {) ]9 n - public void keyPressed(KeyEvent e) {
6 H* U+ S+ q9 P6 E5 ? { - if (e.getKeyCode() == 10 && stageNo == -1) {
: k, R" f V7 ^9 Z - // 开始关卡1
/ F9 H6 |2 j& W$ r# ]' X5 J - reset(0);
! }0 U$ W+ _0 C" z& K' `3 u - } else if (stageNo < 0) { $ s; m K3 o6 y! {
- return; 9 v* f$ R) y( {6 G1 w) w: Y5 W3 s
- } # R6 ~/ s# w8 ^' t: [6 {$ K: t
- // 选关(默认为最大支持7关,更多请自行设定)
( _2 i1 v( O6 i' r8 U - if (e.getKeyCode() == 97 || e.getKeyCode() == 49) { # _* S) E9 J( i# R, Z1 @! L
- stageNo = 0;
; M7 j2 M8 q5 R# l8 K - } else if (e.getKeyCode() == 98 || e.getKeyCode() == 50) { ) ^* i; E+ r% l8 {' B
- stageNo = 1; + m' `' T8 N' |% k' |% Z2 Z0 Q
- } else if (e.getKeyCode() == 99 || e.getKeyCode() == 51) {
2 V, r, T3 T0 H, ]; o0 y# O - stageNo = 2; - p j( Z; X% b+ x x
- } else if (e.getKeyCode() == 100 || e.getKeyCode() == 52) { $ b( f( K3 W; |! E( `
- stageNo = 3; ) p) A/ e, O0 a. I6 C
- } else if (e.getKeyCode() == 101 || e.getKeyCode() == 53) { 4 w8 I8 J1 L {& n6 `
- stageNo = 4;
) q7 U+ s: @( G8 y+ K6 s" Y - } else if (e.getKeyCode() == 102 || e.getKeyCode() == 54) {
4 b! K& { q' k% v5 F# ] - stageNo = 5;
; {' H7 m- e' b3 H) G' Q3 x - } else if (e.getKeyCode() == 103 || e.getKeyCode() == 55) {
6 t2 M7 m; i) a, _+ q5 M - stageNo = 6;
9 l* S0 J# V G; L - // ESC,重新开始 & _' q7 k' X( D
- } else if (e.getKeyCode() == 27) { ' d# h+ N* d% c: d
- reset(stageNo);
+ h7 G4 z, @# Y - } else if (stage.getMode()[stageNo] == 1) { , p- d+ y* P3 L- n
- // 退步 " A6 w V2 P) Y2 s
- if (e.getKeyCode() == 83) {
. C5 N1 x/ X5 O - nextStep(-1);
f6 z5 N1 H" r - }
0 w5 D' T5 Z' t2 k0 R+ X* h' m7 I - // 移动角色 $ P! s7 C2 M$ X( A4 K) B9 f5 \( M! j( f4 E
- else if (e.getKeyCode() == 40) { 6 W. l2 ?% z. r
- nextStep(0); , ^0 L# g; \$ G0 {
- } else if (e.getKeyCode() == 37) {
e/ b0 y% w8 w5 Y+ T' m - nextStep(1); , u3 {( m2 D6 X* W( }+ _8 E
- } else if (e.getKeyCode() == 39) {
' c1 u0 N8 B& }* r4 I1 n" @ - nextStep(2);
% E( x E0 s/ g0 t - } else if (e.getKeyCode() == 38) { 5 @) [/ G4 _6 A& R
- nextStep(3);
/ x5 |9 _( R/ L4 {0 E0 T3 [ - } else { ! P# |; ^, r- Z+ [, q w9 y, ~
- return; 0 Q6 S) j& R* Y: X/ @: V }
- }
s' h3 j( g$ l6 @4 ^7 s0 M - } else {
' {; X, F U+ P/ V) v - return;
4 F3 y$ L+ }& g v# O; k) L$ R2 V - } t9 e& L: M! k8 @( R3 N; c4 w
- // 绘制背景
6 J$ ~/ i" ~& R! K; r- K+ O - background(); : y+ \( v6 c1 W4 U
- // 绘制游戏画面
0 |8 [! Z/ ~+ u5 ?+ O - drawScreen(); 9 z2 I9 C. T( z, N
- } ! b$ w* F' P' F- p" C/ P
- /** 4 w2 [ v+ x3 J6 m0 a
- * 切换动作 : W3 \, t1 x+ c, E
- * , @& R* z! _4 w7 o
- * @param i - A" V, W$ y# {
- */
1 m# v' [! o2 }& U" Q - public synchronized void nextStep(final int i) { & M# D# [) _# z
- boxMove[stageNo][hp[stageNo] - 1] = false;
& F% e2 V1 A- [0 r - switch (i) { 1 C" H7 G$ a! R" b9 n8 |6 e1 P
- case 0: $ i2 O( t( m/ a7 s- h' I
- if (stageStates[stageNo][roley[stageNo] + 1][rolex[stageNo]] < 2) { 7 B& z' C- p4 @# ~' n+ q
- return;
* n: q3 G! y# o; Z - } ( u2 z* \1 X# b8 a9 m* d! Y' G
- if (stageStates[stageNo][roley[stageNo] + 1][rolex[stageNo]] < 4) {
% y* d! T2 q. E. k/ b - if (stageStates[stageNo][roley[stageNo] + 2][rolex[stageNo]] < 4) {
# g2 Q# O' s( ?0 B) z - return;
' M# F& }6 Q+ s- C - }
5 m! I2 R7 J' H( O, t4 r - stageStates[stageNo][roley[stageNo] + 1][rolex[stageNo]] += 2; " J& o5 I4 o n; b V) G9 F4 ]+ P
- stageStates[stageNo][roley[stageNo] + 2][rolex[stageNo]] -= 2; ; w, {, i; ^$ D7 U8 ^- _
- boxMove[stageNo][hp[stageNo] - 1] = true;
5 c4 {* j4 g3 z& s6 c# i- Y4 t - }
0 D) @+ U8 k0 g9 h" H* q - roley[stageNo]++;
- t/ q. B2 c* P2 \% z3 J9 F% B3 Z - break;
0 Q" M- O8 E) n% a( B - case 1:
/ W _# [1 g% }8 C9 Q3 b) i3 c - if (stageStates[stageNo][roley[stageNo]][rolex[stageNo] - 1] < 2) { 0 ?3 v& K! N7 M/ A& r) Q K
- return;
9 V8 Q1 z; \+ N6 G - }
: Q/ m `6 ?" d _ - if (stageStates[stageNo][roley[stageNo]][rolex[stageNo] - 1] < 4) { 8 ~$ t) O7 p" \1 W j0 m4 R8 O7 B6 z
- if (stageStates[stageNo][roley[stageNo]][rolex[stageNo] - 2] < 4) { 9 v4 ^5 P3 ~: h9 w. k2 _- k
- return; 8 ~# l. r: a$ h/ `
- }
: Z9 ^( ~0 f1 W - stageStates[stageNo][roley[stageNo]][rolex[stageNo] - 1] += 2; 6 ]' A+ M) z3 H# z
- stageStates[stageNo][roley[stageNo]][rolex[stageNo] - 2] -= 2;
9 n5 N0 f4 x) `" S - boxMove[stageNo][hp[stageNo] - 1] = true; 9 @) y* d8 P; _2 d# v7 f
- }
) E! e [0 M) n1 n; ] - rolex[stageNo]--;
. O9 h: r# w' b) _% P' U7 z - break;
8 e& J/ c' O. x( t - case 2: 0 b8 f, Y# [! _/ J6 d
- if (stageStates[stageNo][roley[stageNo]][rolex[stageNo] + 1] < 2) {
, @% F( M: c& | - return;
2 b$ c! L$ r/ B - } 8 a0 z. s) y$ X( ~& N
- if (stageStates[stageNo][roley[stageNo]][rolex[stageNo] + 1] < 4) { 2 |* C5 x9 K# B
- if (stageStates[stageNo][roley[stageNo]][rolex[stageNo] + 2] < 4) { e2 Y f) l! k& X
- return; . h$ R2 n2 b3 m8 c" \
- }
2 j9 \1 _1 x4 ?2 Y( B! b - stageStates[stageNo][roley[stageNo]][rolex[stageNo] + 1] += 2; " e j9 ]; d( O9 x$ E
- stageStates[stageNo][roley[stageNo]][rolex[stageNo] + 2] -= 2; # C) X. ?: z2 E# `8 _
- boxMove[stageNo][hp[stageNo] - 1] = true; 5 J& k, t! O7 G0 `" p
- } : y% D; O; A; _* A3 W
- rolex[stageNo]++; + h3 H# {. d: u
- break;
5 z/ v: [* b2 w6 ]' l2 { - case 3:
! r6 u2 ?/ ^% [8 `: x6 A% R8 U - if (stageStates[stageNo][roley[stageNo] - 1][rolex[stageNo]] < 2) { 2 k- c9 E( M" p7 @2 v. g
- return;
* B4 S) ~+ Q4 f- l - } ; X! ^, D- X! }+ S7 F( Y
- if (stageStates[stageNo][roley[stageNo] - 1][rolex[stageNo]] < 4) { - F& {; f6 [8 j! N, S' y
- if (stageStates[stageNo][roley[stageNo] - 2][rolex[stageNo]] < 4) {
& m% _$ F$ _" u5 ^ - return; 4 j1 m2 r1 p8 s$ c- r% D1 T
- }
8 r' \" K' Q% j* I - stageStates[stageNo][roley[stageNo] - 1][rolex[stageNo]] += 2;
P; `1 I* w, c1 _ - stageStates[stageNo][roley[stageNo] - 2][rolex[stageNo]] -= 2; , S- V" X) p: N% c9 Y( G, v9 w
- boxMove[stageNo][hp[stageNo] - 1] = true; . V; ]' m' P, F, r
- }
5 m* o8 ]8 p- X9 R; U: ? - roley[stageNo]--; 0 \% i0 \/ m$ H! m, @: r
- break; ( x0 Q" t6 K y0 X& w" z Z2 z
- default: ' `3 b R1 J% w% I' b8 R
- if (hp[stageNo] == stage.getMaxHp()[stageNo]) { 0 h4 H7 ?6 r' a! K ]
- return; 0 t& I! n7 \9 \! r8 [: v! N
- }
. h! Q: n2 `- O( R8 Z+ L* S - switch (ons[stageNo][hp[stageNo]]) {
' y9 J7 X2 N/ Y9 B: Q& J; B - case 0: . y% d) I2 K7 g8 G# v6 i7 Q
- if (boxMove[stageNo][hp[stageNo]]) { & F, K s; `' U1 x: I& b- J v
- stageStates[stageNo][roley[stageNo] + 1][rolex[stageNo]] += 2; / C: T3 ?8 x& f8 n% m
- stageStates[stageNo][roley[stageNo]][rolex[stageNo]] -= 2;
1 W/ u, C: s$ Y) A/ ]; z$ a7 g' M - }
) Z: q+ M3 f. @: X6 V+ j - roley[stageNo]--; - O/ v8 [9 j/ y9 u J
- break; ! S2 p# s" e; t
- case 1: 6 [( R! T( l C2 p7 o
- if (boxMove[stageNo][hp[stageNo]]) { * P. [. b7 e2 d6 m6 W9 t
- stageStates[stageNo][roley[stageNo]][rolex[stageNo] - 1] += 2;
+ }6 t/ D; w5 s/ {2 l: C- | - stageStates[stageNo][roley[stageNo]][rolex[stageNo]] -= 2;
% `8 [- h9 ?6 U; ?4 G( X9 ^, C - } $ E: U4 d1 R/ }, e# K6 ?
- rolex[stageNo]++;
w! }# s0 ?6 r" K3 V! S - break;
7 y& E5 ]) {$ r' o* a - case 2:
# f" _4 e9 }& {( q. C - if (boxMove[stageNo][hp[stageNo]]) { 7 c$ M3 f4 [3 [1 E/ k/ r+ v! K
- stageStates[stageNo][roley[stageNo]][rolex[stageNo] + 1] += 2; & n. i% v, r1 R2 z, s
- stageStates[stageNo][roley[stageNo]][rolex[stageNo]] -= 2; 9 Z5 J4 \* M8 G/ V3 @/ g
- }
: l. n/ s% K8 M: W - rolex[stageNo]--;
/ z( S" e5 ?' f& c$ m3 G - break;
1 Q7 i) N$ n1 T( j3 a( e' Q - default: ; Z) X5 D% J' o; ?$ k
- if (boxMove[stageNo][hp[stageNo]]) { & C: D0 H! P) B# V
- stageStates[stageNo][roley[stageNo] - 1][rolex[stageNo]] += 2;
9 l1 J( U) |+ v0 W - stageStates[stageNo][roley[stageNo]][rolex[stageNo]] -= 2;
$ y+ c" C( }; u - } % e- }+ l# L9 `2 L5 ]' A% I9 F- A
- roley[stageNo]++; , y" U7 m* I" ? H9 N4 D
- break; 7 m& [9 a) ~! |/ x" X% \- I
- } 8 J; ]" U* ?2 i6 J
- break;
: r; _( |+ C7 o) {; U - }
$ C/ c, B4 S2 Q, }; Q9 a - if (i != -1) { * D( O! e% O) |
- hp[stageNo]--; + `0 [) j3 T7 O8 j% C9 ~4 b
- ons[stageNo][hp[stageNo]] = i;
1 D* d. e' ~+ E! y4 I9 F - } 8 c j* S$ q9 G" O% D/ H
- role[stageNo] = ons[stageNo][hp[stageNo]]; 9 T- P$ q5 ^% L4 T. }6 F
- if (i == -1) { * f# w) C% k( J* k* O
- hp[stageNo]++; % q4 d% ] ]. G
- } , g' \7 u% B9 `* }. `
- execute();
2 h- K" R6 w: t+ c+ j' v$ B9 Y3 y - } # I9 j) N( v9 g! D+ r( O8 l
- /**
$ h8 Q8 t. y$ \1 m# ~5 b6 U+ x' x: D0 J - * 开场画面 & a4 _6 ]& U' {
- * 4 D6 q6 d9 m; G
- */
/ Y+ m+ j; W- Z! j - public synchronized void openDraw() {
1 d2 N! ~; z' I8 J5 t& ]% p: B7 w0 K5 s6 M" S - background();
0 e9 y7 H( u* A9 P7 g I - for (int i = 0; i < 5; i++) {
" `1 i6 i* t5 J# [0 p) t. B - for (int j = 0; j < maxX + 1; j++) {
+ ]2 k0 [. Q0 m! W" y5 V R - screen.drawImage(floorImage[0], CS * j,
0 {# S/ [/ p7 _! [4 a/ ? - (CS * i + CS / 2 * maxY) - CS * 2, null); ( W8 x$ g2 @( O
- }
5 w* T, @+ J5 p, l) \: j# i2 { - } # @8 f" j/ a! ^ r
- GraphicsUtils.setAntialias(screen, true);
$ G' k1 {6 \$ x- r5 U: n - String mes = "怪蜀黍传说 - 勇者推魔王"; 1 K& T: H$ s4 b/ K+ ~) x
- screen.setFont(new Font("华文新魏", 1, 35));
; M/ d; B& Y* B% u - GraphicsUtils.drawStyleString(screen, mes, (CS * maxX - screen 2 E" Y" ]* g8 i$ `' g
- .getFontMetrics().stringWidth(mes)) / 2 + 13, 6 O% z: f* w. H H
- (CS * maxY) / 2 + 25, Color.black, Color.orange); 4 G. u, E7 u# N; V& T: w
- mes = "Java版搬箱子游戏开发入门示例 - 0.1.0";
% `5 O; N; Q" X } - screen.setFont(new Font("华文新魏", 0, 30)); , [% e& n: u2 Y, N( |2 { H i8 y
- GraphicsUtils.drawStyleString(screen, mes, (CS * maxX - screen % y$ \5 h5 I3 R4 r2 ?6 V; |
- .getFontMetrics().stringWidth(mes)) / 2 + 13, , y. p2 F4 K9 ^: j8 `4 _
- (CS * (maxY + 2)) / 2 - 55, Color.black, Color.white);
, [3 h6 i& ^( X% M+ p - GraphicsUtils.setAntialias(screen, false); 9 E% o1 o! a' B, L d7 ~
- setupDisplay(); . a0 {: }. w- y
- } ; ^5 [" x6 K9 K$ K' C
- /** 0 p* [; m' [ \4 u/ h1 _8 b
- * 绘图接口实现 , c3 L5 H H2 P2 _' M3 @, W
- */ ! s3 l: P& I% x
- public synchronized void draw(Graphics g) { 4 e8 D9 |7 w7 \* e# k
- g.drawImage(screenImage, 0, 0, null); . T" _2 y+ L$ P0 ?2 T- g+ V4 n" g/ d
- if (succeed) {
7 F! ~* m6 h+ `+ X/ q - if (sleep == 100) { 8 \5 [* @9 w; k4 f9 J
- // 进入下一关
1 L1 l. _% F0 _! @ - reset(++stageNo);
. d, W1 k/ R5 V2 ^ - succeed = false; ; y9 f/ i& Q( A: [. B
- sleep = 0;
' Y# ], \- u1 C' }, n4 |' n- s - } * m( T1 d% } j! T1 J
- sleep++; % X }5 L1 D/ A& X
- } 3 h9 ^3 D# l5 O. T4 J
- } ( i: Z+ R$ M! a# `9 y1 t$ `4 b
- /** - ?/ }* c1 T4 ^1 b4 N5 H
- * 设置基本数据 6 `+ w9 a6 g# l8 [4 d1 f
- * 9 r! O( }+ Q$ k j' w
- */ 4 @# r( Y' f) a3 O! J* i; |' t E
- public void setupStage() { * r* w4 ~% k/ ]6 N+ W" V+ c7 C( S
- for (int i = 0; i < maxY; i++) {
( U" n( \6 \' } - for (int j = 0; j < maxX; j++) {
3 e# @" }, g' Z! l - stageStates[stageNo][i][j] = 0;
. }' ~; N" j0 z& s3 E) O7 A - } & V% q% ?* M& B L3 i' l9 G
- }
1 }6 p q4 E; n% `" W - mapx[stageNo] = stage.getConf()[stageNo][0];
" E2 t. X( d2 |' t" j( T8 r - mapy[stageNo] = stage.getConf()[stageNo][1]; ) R, H4 o, s- ?7 j3 D/ \
- rolex[stageNo] = stage.getConf()[stageNo][2]; 2 E, g1 d, }8 N+ N4 P K1 G( E
- roley[stageNo] = stage.getConf()[stageNo][3]; " y/ V3 k/ s1 p# {( z7 Y* W
- for (int n = 0; n < mapy[stageNo]; n++) {
( ]- V- J# P2 ~3 a! {' m - for (int l = 0; l < mapx[stageNo]; l++) {
. J, T% A% s z+ F3 j4 k w - stageStates[stageNo][n][l] = stage.getMapData()[stageNo][n][l];
; r$ d- M5 `0 Q1 y - } 1 t+ a& l0 N: F0 d; S% N
- } 8 }, r7 f2 g9 p) ?% T
- hp[stageNo] = stage.getMaxHp()[stageNo];
# |4 @) I: K/ a" _8 e - role[stageNo] = 0;
9 ^. ?- B# t! k$ Q' i( x3 o - for (int i1 = 0; i1 < stage.getMaxHp()[stageNo]; i1++) { ) G% [8 y1 `) p) ?5 j( P" b7 x
- ons[stageNo][i1] = 0;
" M( A/ G/ S6 M) C% o - }
2 s; J' L( S( i, T - for (int j1 = 0; j1 < stage.getMaxHp()[stageNo]; j1++) {
`4 |2 q8 X, D" o5 A( K - boxMove[stageNo][j1] = false; 1 k# Y( I& r% m6 Z7 q7 X& M
- }
7 Y0 O/ m) C! Z2 G7 L* o q5 S8 i2 p - } 5 x6 a! \% ]1 ]! i+ Q
- /** , D' r2 y8 [! Q, C/ O
- * 设置显示 & Q8 o6 n) U& c$ @. k) W
- *
( J! g4 Z' l! j8 D4 u- l4 O% J - */
7 [( Q; P+ H1 c* I - public void setupDisplay() {
; a5 a* D) x- P4 x/ s - if (message == null) {
T0 }3 x# N4 |- O$ i, f( g - screen.setFont(new Font(LSystem.FONT, 0, 12));
6 r5 m" b. u; D - int size = 0; 9 a+ p n' s* m+ x
- for (int j = 0; j < stage.getMaxStageNo(); j++) {
% y! Y( \+ _, D2 A - if (complete[j]) { 8 M7 K2 q* x- D1 | M( {
- screen.setColor(Color.gray);
7 w: W* P, j0 m! Z) q - } else {
, x5 f# K% x- B: S y6 C. H( Y2 J- U( h* \ - screen.setColor(Color.white); 7 y1 @* s$ ^0 X1 y( G" T, l
- } ) n* c4 b( T- t! h
- if (j == stageNo) {
/ T& Z" U" O+ j- J! A - if (complete[j]) {
: E- w3 b$ o' V1 `7 C9 F* o$ y, r/ n - screen.setColor(Color.magenta);
. q4 ~+ m! R7 f S# h- ^4 C z - } else {
+ Y+ j: g1 `8 ~% w - screen.setColor(Color.cyan); ) S, R8 `8 Z6 \ j2 ]$ P
- }
5 y9 A# v* _& ]' b" x7 x - } % e: T" i' o, \: l0 L
- screen.drawString(m_stageName[j], size = (10 + (screen ! [( m: o3 H# B N3 @; y
- .getFontMetrics().stringWidth("关卡0") + 20)
$ f5 j Z* g# a - * j), LSystem.HEIGHT - 50);
: Z( T% [& }$ @! V. g - } # N8 Y8 F2 Z/ w3 l( x9 e2 z" ^
- screen.setColor(Color.white); ) D1 I0 d0 R' F4 ~) [
- if (stageNo == -1) { 9 j6 I7 V# u- N
- // 初始画面,无数据 / b6 M& j# Z+ [9 m
- } else {
5 H' u6 Q# M( m0 \* f* s3 e - // 显示状态 ! P `0 K5 o2 `
- if (hp[stageNo] == 0) {
8 A9 U) a8 v6 K U: }& I( m; E - screen.setColor(Color.red); 1 t4 u, k" f8 a) b! E% Q- ]+ {$ C
- } else if (hp[stageNo] <= stage.getMaxHp()[stageNo] / 4) {
6 Q+ X# a6 n3 S' b9 ^# H - screen.setColor(Color.orange); 6 p7 R7 P3 x6 d* \) G
- } else if (hp[stageNo] <= stage.getMaxHp()[stageNo] / 2) {
G) k+ V2 r. F - screen.setColor(Color.yellow); 7 ~4 p- I. R3 c; Z5 z5 \
- } - m" Y; R k5 J9 o, w& N
- screen.drawString("主角 HP " + hp[stageNo] + "/" 0 O3 Q. u3 Z2 L- u) x/ x1 }
- + stage.getMaxHp()[stageNo], size + 50,
; ?# M3 Y6 W) \8 D( \ R - LSystem.HEIGHT - 50); 4 p: G+ P4 v! q1 h) Q
- switch (stage.getMode()[stageNo]) { ! c. V; G2 W, J& v) |. H5 l9 u
- // 生命耗尽
. u% e+ V# I, b1 b6 U& v5 r - case 2: $ C4 Q1 b" C' \ s& b0 N1 l
- screen.setFont(new Font(LSystem.FONT, 1, 30)); ' a2 A2 B% H( E1 T/ D$ d
- screen.setColor(Color.red);
# E9 Q/ r% ], N" ]! `" A+ d# r - GraphicsUtils.setAntialias(screen, true); ) o F. n4 C' L& k P' C5 u+ w1 E
- screen.drawString("The End!",
6 {' z0 l7 O# A6 S; b6 I& S - (CS * maxX - screen.getFontMetrics().stringWidth( 4 x" n$ Z$ L- M* D* c8 `4 \# o' `; H2 m
- "The End!")) / 2 + 14,
, R3 n3 v2 H: v2 K% K4 X$ ~ - ((CS * (maxY + 1) + screen.getFontMetrics() " i; Y& f3 x/ g0 S; F
- .getAscent()) - screen.getFontMetrics()
% g9 b+ \* v; [8 B# n2 b - .getDescent()) / 2);
' P" k+ R- J2 {9 @) ]2 G8 F - GraphicsUtils.setAntialias(screen, false);
) D* Y( ?! \9 ?2 b - break; ( l% u* g$ W# Q" }
- // 胜利 : {. }% @+ h0 C+ P. v
- case 3:
' p% x: E2 z' v" p3 H {5 b - screen.setFont(new Font(LSystem.FONT, 2, 40)); 4 ]: \# A% d5 z7 Q$ g+ \
- screen.setColor(Color.darkGray); ! w2 @) S# W0 v1 q
- GraphicsUtils.drawStyleString(screen, "Good job!", (CS
% }8 ]. b) _, x - * maxX - screen.getFontMetrics().stringWidth(
7 c1 B8 Z& N4 J - "Good job!")) / 2 + 14, ((CS * (maxY + 1) + screen & J5 E7 c# K% R. u0 _. s+ d3 c
- .getFontMetrics().getAscent()) - screen
* @& w5 O# y/ ^/ o2 ~! z - .getFontMetrics().getDescent()) / 2, Color.black,
* R7 ?9 t+ g. S9 d7 h6 K, W - Color.white); / B4 D/ J2 y% J0 r4 J, ?( B
- // 胜利标记 8 F- Y/ B5 g0 T! B
- succeed = true; ! G- w9 q# T" R
- break; % I% S1 K$ P9 V6 H9 y, }2 l5 E0 F
- }
8 Z9 a6 O" [0 S& A# C' ?7 n - }
, K( t! p! O. `4 B - }
. M! @5 Y4 O O3 T, B3 R8 ]. n - // 存在信息时
) a* K$ U' H* m+ R - else {
; J8 b S5 L' Q3 u) n - screen.setFont(new Font(LSystem.FONT, 0, 12));
7 J$ C/ g# l0 E, ], t; r/ Q) b- [; Y - int n = screen.getFontMetrics().stringWidth(message) + 16;
2 V3 x% d1 L% k7 j2 j, Z - int l = screen.getFontMetrics().getAscent() " q: Z; D% e" \, }! o
- + screen.getFontMetrics().getDescent() + 8;
# R, T. V+ m8 Y+ S: d. m; U - int i1 = (CS * (maxX + 1) - n) / 2;
0 C+ A- ?! l6 E9 |* P2 C9 ~ - int j1 = (CS * maxY + 1) - l / 2; - E7 r8 b+ w( G# G
- int k1 = i1 + 8; ) u) K$ v; ?. e7 H7 ~9 |+ I4 o
- int l1 = CS 4 Q/ j9 X; L, r3 a4 z. \
- * maxY - c0 z u' I6 b4 v9 @! n' Z8 U5 z
- + 1
# u6 a+ V" I& m1 e7 Y - + (screen.getFontMetrics().getAscent() - screen ; A- ~& @+ h, g
- .getFontMetrics().getDescent()) / 2;
# {6 x' s; o) |/ c: R7 N! | - screen.setColor(Color.black); \2 u. M( E. b; [
- screen.fillRect(i1, j1, n, l);
# E. l) I; x+ j0 A% o) ^ - screen.setColor(Color.white);
* w8 g9 V) j+ _9 I" x) T8 T - screen.drawString(message, k1, l1); x) h7 B9 }& ?0 x
- } % j* C- Z' e" n( v# m
- }
}9 Q$ T+ F w6 | - }
复制代码
/ L. m5 T& _7 u7 Z6 y9 y: r( W+ {启动类:
& q% E. B6 X6 x. L& n/ l
; A0 X( }- a' b9 g2 C4 Z
* o( E, j& C( y' H8 w) f
Main.java - package org.loon.game.simple.sokoban.main; 9 c1 _4 L" R/ F( x& @! Q' s9 r8 Y
- import org.loon.game.simple.sokoban.GameCursor; 0 V3 [ R+ \# G
- import org.loon.game.simple.sokoban.GameFrame; 9 s& c( z. ^# g R5 O0 U5 s
- import org.loon.game.simple.sokoban.control.Sokoban; . n- R4 r/ w3 ?: N6 [6 }
- import org.loon.game.simple.sokoban.control.Stage;
, N3 n ?/ q( S5 A0 A - import org.loon.game.simple.sokoban.control.Stage1;
. s7 A0 K; O- _: F1 y, v - /** s2 T$ d0 \8 l( w" T8 H# ~
- * Copyright 2008 - 2009 3 t R+ a' S0 Y6 w ~, n3 y' a
- * 5 o% b9 o- D+ ^* _, q
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
( }) h8 A7 Y+ M3 {4 V! h9 D6 f# o9 u3 z - * use this file except in compliance with the License. You may obtain a copy of 5 r3 L8 h; O, \9 Z5 [0 b6 d
- * the License at ' Z; B( o% d* I5 E, W9 ?7 E$ c& _8 P
- *
6 a6 S8 L A! X5 O - * http://www.apache.org/licenses/LICENSE-2.0
/ \1 Q( T# X/ e9 T* A - * 7 U7 i$ l; D& p9 \. s9 S$ W
- * Unless required by applicable law or agreed to in writing, software . r+ B: j, f2 i1 w* I
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT ) g$ p7 W, ~5 b' ^
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the : W% I, U/ l$ i8 r2 c3 G
- * License for the specific language governing permissions and limitations under " y2 u1 \. |+ M% z' Y5 a5 u! y
- * the License.
$ d6 k! N' l5 m1 ^) B! b& ^8 T& C - * 8 W& i5 G1 B. D8 g/ T! d) n' @/ [
- * @project loonframework ) R/ Z9 B' @& B
- * @author chenpeng 2 ]+ W$ s8 Y" p* T* T Y
- * @email <a title="" href="http://hi.baidu.com/ceponline" mce_href="http://hi.baidu.com/ceponline" target="_blank">ceponline</a>@yahoo.com.cn
! d7 ^2 F6 P {4 p1 A; x+ H0 K7 b - * @version 0.1 $ r9 r0 i3 C2 {# \" s+ j
- */
# } y6 t- Q5 J- J& {7 |) a4 p/ j7 x - public class Main {
- L/ I3 T1 e* M - public static void main(String[] args) { ) Z% ~$ B$ J& Q# H: e/ T5 O0 E
- java.awt.EventQueue.invokeLater(new Runnable() { % V9 O) t; Q r3 q3 A( ^
- public void run() {
- H) i5 M5 x: d w; U3 P7 F - GameFrame frame = new GameFrame("Java版搬箱子游戏入门示例及源码-0.1.0", 540, 480);
8 X* t% F* i7 L - frame.setCursor(GameCursor.getCursor("image/cursor.png")); # `& x S8 J( u; a* I' n
- // 获得搬箱子关卡接口 . |$ x% d7 m3 t& J/ Z1 s) `
- Stage stage = new Stage1(); 5 \, z3 L% e0 J
- // 设定游戏控制器 6 l) [3 h {7 Q. W! {' i
- frame.getGame().setControl(new Sokoban(stage));
. B$ e2 o. \9 S0 X: A - // 游戏全屏
* {3 a5 w7 J& k8 y - // frame.updateFullScreen();
7 l* Z4 U) g- T' r# h+ Y l, ` - // 是否显示fps
! D' t- j$ W, \; `+ R - frame.setFPS(true);
$ X) V& u$ f- q- L - // 允许的最大刷新率
9 c+ ?) I/ e* Y+ y5 r7 Z1 S* S - frame.setMaxFrames(60); 6 x: U7 \ n, U
- frame.mainLoop(); / l4 t0 {& G: K( v( y2 W! q
- frame.showFrame(); 5 E( ~0 w1 s4 f- @# T) ^
- }
6 r+ N+ L/ s' ]% J- s: U% p& N - });
: H6 X) `6 D, B+ D - }
6 k. W+ {" q: n6 Q" X - }
复制代码
5 t0 g9 s( J- Z; M+ } _- g$ q4 Y3 I- c: J. i
JavaSwing实现的推箱子项目源码.zip
(139.15 KB, 下载次数: 4, 售价: 1 IT币)
4 z# |6 j4 f4 }. h D0 ]* o$ l
|
|