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