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