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