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