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