我制作了一个有两个类的推箱子,逻辑和推箱子GUI。
基本上,逻辑部分从输入字符串数组 f.ex 构建一个 ArrayList>:
String[] level1 =
{"*###########*",
"# #",
"# ... ... #",
"# *$$ $.$ #",
"# $*+$ $*$$ #",
"# *$$ $.$ #",
"# ... . #",
"# #",
"*###########*"};
其中“+”是玩家位置。
无论如何,随着游戏的进行,ArrayList 会在逻辑类中更新(玩家正在移动,角色正在交互等)
游戏的图形部分是通过给每个角色分配图片并循环遍历arraylist来解决的。然后在一个无限的while循环中更新它,这样当arraylist>在逻辑部分更新时,它会再次打印。
如果这很难理解,我已经打印了下面的代码。
问题 1:我不认为这是输出游戏的最佳方式(使用 while-lopp),因为我一直在以图形方式输出游戏,即使没有进行任何更改。这通常是如何处理的?Swing 可以帮助解决这个问题吗?
问题2:另外,在下面的代码中,我没有使用main方法,我使用了run()。main 方法不喜欢我进行非静态引用,并且 run 有效,所以我继续使用它......我应该如何使用 main 方法来使游戏正常工作?
也许更容易看到我的代码:
逻辑类
package Sokoban2;
import java.util.ArrayList;
public class Logic {
int player_x;
int player_y;
int nextMove_x;
int nextMove_y;
ArrayList<ArrayList<Boolean>> targets;
ArrayList<ArrayList<Character>> grid;
Boolean canMove;
public Logic(){
buildGrid();
buildTargets();
findPlayer();
}
public void move(int dx, int dy){
if(CheckCanMove(dx, dy)){
moveBox(dx, dy);
movePlayer(dx, dy);
}
}
public void movePlayer(int dx, int dy){
boolean boxAhead = (getChar(dx,dy) == '#');
if(!boxAhead){
char pChar = grid.get(player_x ).get(player_y);
char pCharNext = grid.get(player_x +dx).get(player_y +dy);
if(pChar == '@'){
if(pCharNext == ' ' || pCharNext == '$'){
grid.get(player_x + dx).set(player_y + dy, '@');
grid.get(player_x).set(player_y, ' ');
}
if(pCharNext == '.' || pCharNext == '*'){
grid.get(player_x + dx).set(player_y + dy, '+');
if(pChar == '+'){
grid.get(player_x).set(player_y, '.');
}
else{
grid.get(player_x).set(player_y, ' ');
}
}
}
else if(pChar == '+'){
if(pCharNext == ' ' || pCharNext == '$'){
grid.get(player_x + dx).set(player_y + dy, '@');
grid.get(player_x).set(player_y, '.');
}
if(pCharNext == '.' || pCharNext == '*'){
grid.get(player_x + dx).set(player_y + dy, '+');
grid.get(player_x).set(player_y, '.');
}
}
player_x += dx;
player_y += dy;
}
}
public void buildGrid(){
char c = '"';
grid = new ArrayList<ArrayList<Character>>();
String[] level1 =
{"*###########*",
"# #",
"# ... ... #",
"# *$$ $.$ #",
"# $*+$ $*$$ #",
"# *$$ $.$ #",
"# ... . #",
"# #",
"*###########*"};
int length_i = level1.length;
System.out.println(length_i);
int length_i_j = level1[length_i-1].length();
for(int i = 0; length_i>i; i++){
grid.add(i, new ArrayList<Character>());
for(int j = 0; length_i_j>j; j++){
c = level1[i].charAt(j);
grid.get(i).add(c);
}
}
}
public void buildTargets(){
targets = new ArrayList<ArrayList<Boolean>>();
char c = ' ';
int length_i = grid.size();
for(int i = 0; length_i>i; i++){
targets.add(i, new ArrayList<Boolean>());
for(int j = 0; grid.get(i).size()>j; j++){
c = grid.get(i).get(j);
if(c == '.'){
targets.get(i).add(false);
}
else{
targets.get(i).add(true);
}
}
}
}
public void findPlayer(){
char c = ' ';
int length_i = grid.size();
for(int i = 0; length_i>i; i++){
for(int j = 0; grid.get(i).size()>j; j++){
c = grid.get(i).get(j);
if(c == '@' || c == '+'){
player_x = i;
player_y = j;
}
}
}
}
public Character getChar(int dx, int dy){
char c = ' ';
c = grid.get(player_x + dx).get(player_y + dy);
return c;
}
public void moveBox(int dx, int dy){
char c1 = getChar((dx),(dy));
char c2 = getChar((2*dx),(2*dy));
if(c1 == '*' || c1 == '$'){
if(c2 == '.'){
grid.get(player_x +2*dx).set(player_y +2*dy, '*');
}
if(c2 == ' '){
grid.get(player_x +2*dx).set(player_y +2*dy, '$');
}
}
}
public boolean CheckCanMove(int dx, int dy){
canMove = true;
boolean boxAhead = (getChar(dx,dy) == '#');
boolean boxAhead2 = (getChar(2*dx,2*dy) == '#');
boolean movableAhead = (getChar(dx,dy) == '$');
boolean movableAhead2 = (getChar(2*dx,2*dy) == '$');
boolean movableAheadS = (getChar(dx,dy) == '*');
boolean movableAheadS2 = (getChar(2*dx,2*dy) == '*');
if(boxAhead || (movableAhead && boxAhead2) || (movableAhead &&
movableAhead2) || (movableAheadS && movableAheadS2) || (movableAheadS &&
boxAhead2)){
canMove = false;
}
return canMove;
}
void setPlayerX(int player_x){
this.player_x = player_x;
}
void setPlayerY(int player_y){
this.player_y = player_y;
}
}
图形类 包Sokoban2;
import acm.graphics.GImage;
import java.awt.event.KeyEvent;
import acm.graphics.GImage;
import acm.program.GraphicsProgram;
public class SokobanGUI extends GraphicsProgram {
Logic logic = new Logic();
GImage wall;
GImage blank;
@Override
public void init() {
logic = new Logic();
addKeyListeners(); // This is required for listening to key events
}
@Override
public void run() {
System.out.println(logic.player_x);
System.out.println(logic.player_y);
while(true){
paint();
println("hah");
}
// System.out.println("win");
}
public void paint(){
char c =' ';
int length_i = logic.grid.size();
for(int i = 0; length_i>i; i++){
for(int j = 0; logic.grid.get(i).size()>j; j++){
c = logic.grid.get(i).get(j);
switch (c) {
case '#':
GImage wall = new GImage("sokoban/wall16x16.png");
wall.setLocation(16*j,16*i);
add(wall);
break;
case ' ':
GImage blank = new GImage("sokoban/blank16x16.png");
blank.setLocation(16*j,16*i);
add(blank);
break;
case '@':
GImage player = new GImage("sokoban/mover16x16.png");
player.setLocation(16*j,16*i);
add(player);
break;
case '$':
GImage movable = new GImage("sokoban/movable16x16.png");
movable.setLocation(16*j,16*i);
add(movable);
break;
case '.':
GImage target = new GImage("sokoban/target16x16.png");
target.setLocation(16*j,16*i);
add(target);
break;
case '*':
GImage movable_on_target = new
GImage("sokoban/movable_on_target16x16.png");
movable_on_target.setLocation(16*j,16*i);
add(movable_on_target);
break;
case '+':
GImage mover_on_target = new
GImage("sokoban/mover_on_target16x16.png");
mover_on_target.setLocation(16*j,16*i);
add(mover_on_target);
break;
}
}
}
}
public boolean win(){
int length_i = logic.targets.size();
int win = 0;
for(int i = 0; length_i>i; i++){
for(int j = 0; logic.targets.get(i).size()>j; j++){
if(!logic.targets.get(i).get(j)){
win += 1;
}
}
}
return (!(win>0));
}
@Override
public void keyPressed(KeyEvent event) {
int key;
if (Character.isLetter(event.getKeyChar()))
key = event.getKeyChar();
else
key = event.getKeyCode();
switch (key) {
case 'w':
logic.move(-1, 0);
break;
case 's':
logic.move(1, 0);
break;
case 'a':
logic.move(0, -1);
break;
case 'd':
logic.move(0, 1);
break;
}
}
}