我的2D游戏Sokoban正在工作,但我想知道我是否改进了设计

时间:2013-05-01 13:58:09

标签: java

我做了一个带有两个类的Sokoban,逻辑和SokobanGUI。

基本上,logicpart构建一个ArrayList>来自输入字符串数组,f.ex:

String[] level1 = 
              {"*###########*",
               "#           #",
               "#  ... ...  #",
               "#  *$$ $.$  #",
               "# $*+$ $*$$ #",
               "#  *$$ $.$  #",
               "#  ...  .   #",
               "#           #",
               "*###########*"};

其中'+'是玩家的位置。

无论如何,当玩游戏时,ArrayList会在逻辑类中更新(玩家正在移动,角色正在进行交互等)。

通过为每个角色分配图片并循环遍历arraylist>来解决游戏的图形部分。然后,这是在无限循环中更新的,这样当arraylist>在逻辑部分更新,再次打印。

如果这很难理解,我打印下面的代码。

问题1:我不认为这是输出游戏的最佳方式(使用while-lopp)因为我一直以图形方式输出游戏,即使没有任何变化制作。这通常如何处理? Swing可以帮助解决这个问题吗?

问题2:另外,在下面的代码中,我不使用main方法,我使用run()。主要方法不喜欢我制作非静态引用,并运行工作,所以我去了...我怎么应该使用主要方法使游戏工作?

也许更容易看到我的代码:

逻辑课

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;

    }
}

}

1 个答案:

答案 0 :(得分:2)

问题1 :为了防止不得不连续画画,即使没有输入,只需检查是否有任何变化(怪物移动,玩家移动等)。我认为这是一个Roguelike,所以你可能只需要检查一下玩家是否移动了。

if( playerMoved() ) {
    paint();
}

问题2 :我不知道GraphicsProgram包含哪些内容,但我认为其中的main()正在使用中。使用run()方法运行游戏循环并没有什么本质上的错误,事实上,这就是大多数游戏的工作方式。最多,您的main()只会像现在一样开始使用run()方法。所以你很好!

HTH