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