【问题标题】:Box Push Game Trouble with Slick2dSlick2d 的盒子推送游戏问题
【发布时间】:2013-11-08 18:09:44
【问题描述】:

我正在使用 Slick2d 创建一个游戏,其中角色“Larry”在预定义大小的地图周围推动一个“盒子”。我可以在实体上绘制图片并在实体移动时更新其位置。

我的主要问题是我无法找到一种方法让我的碰撞检测方法停止任何运动。当前,当检测到碰撞时,没有任何反应。当我尝试在我的运动语句中直接使用碰撞检测方法时,eclipse 说没有实体。


public abstract class Entity {

protected int x, y, w, h, dx, dy;

public Entity(int x, int y) {

    this.x = x;
    this.y = y;
    dx = 0;
    dy = 0;
    w = 32;
    h = 32;
}

public void render(GameContainer gc, Graphics g) throws SlickException {

}

public void update(GameContainer gc, int delta) throws SlickException {

    /*x += dx;
    y += dy;*/
}

public boolean isLeftCollision(Entity entity) {
    if (this.x == entity.x && this.y == entity.y) {
        return true;
    } else {
        return false;
    }
}

public boolean isRightCollision(Entity entity) {
    if (this.x + this.w == entity.x && this.y == entity.y) {
        return true;
    } else {
        return false;
    }
}

public boolean isTopCollision(Entity entity) {
    if (this.y == entity.y && this.x == entity.x) {
        return true;
    } else {
        return false;
    }
}

public boolean isBottomCollision(Entity entity) {
    if (this.y + this.h == entity.y && this.x == entity.x) {
        return true;
    } else {
        return false;
    }
}

}


public class Larry extends Entity{
Image player;
float speed = 0.2f;

public Larry(float x, float y) {
    super((int) x, (int) y);

    w = 32;
    h = 32;

}

public void render(GameContainer gc, Graphics g) throws SlickException{

    super.render(gc, g);
    player = new Image("res/char/LarryUP.png");
    g.drawImage(player, x, y);
    g.drawString("Characters X: " + x + "\nCharacters Y: " + y, speed,
            speed);     
}

public void update(GameContainer gc, int delta) throws SlickException{

    super.update(gc, delta);
    Input input = gc.getInput();
    //move right
    if (input.isKeyDown(Input.KEY_RIGHT)) {
        x += speed * delta;
        if (x > 782) {
            x -= speed * delta;
        }
    }
    //move left
    if (input.isKeyDown(Input.KEY_LEFT)) {
        x -= speed * delta;
        if (x < 0) {
            x += speed * delta;
        }
    }
    //move down
    if (input.isKeyDown(Input.KEY_DOWN)) {
        y += speed * delta;
        if (y > 585) {
            y -= speed * delta;
        }
    }
    //move up
    if (input.isKeyDown(Input.KEY_UP)) {
        y -= speed * delta;
        if (y < 0) {
            y += speed * delta;
        }
    } 
}

public class PlayState extends BasicGameState {
int stateID = 3;
Image background;
Larry larry;
Packages box;
float x = 400.0f;
float y = 300.0f;
float speed = 0.2f;
boolean quit = false;

public PlayState(int stateID) {

    this.stateID = stateID;

}

@Override
public void init(GameContainer gc, StateBasedGame sbg1)
        throws SlickException {
    background = new Image("background.jpg");
    larry = new Larry(400,300);
    box = new Packages(150,300);
}

@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
        throws SlickException {
    // TODO Auto-generated method stub


    g.drawImage(background, 0, 0);
    larry.render(gc, g);
    box.render(gc, g);

    if (quit == true) {
        g.drawString("Resume (R)", 250, 100);
        g.drawString("Main Menu (M)", 250, 125);
        g.drawString("Quit Game (Q)", 250, 150);
        if (quit == false) {
            g.clear();
        }
    }
}

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    // TODO Auto-generated method stub
    Input input = gc.getInput();
    // escape
    if (input.isKeyDown(Input.KEY_ESCAPE)) {
        quit = true;
    }
    // when they hit escape
    if (quit == true) {
        if (input.isKeyDown(Input.KEY_R)) {
            quit = false;
        }
        if (input.isKeyDown(Input.KEY_M)) {
            sbg.enterState(0);
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (input.isKeyDown(Input.KEY_Q)) {
            System.exit(0);
        }
    }
    larry.update(gc, delta);
    box.update(gc, delta);
}

@Override
public int getID() {
// TODO Auto-generated method stub
    return 3;
}

}


public class Packages extends Entity{
Image box;
float speed = 0.2f;

public Packages(int x, int y) {
    super(x, y);

    w = 32;
    h = 32; 
}

    public void render(GameContainer gc, Graphics g) throws SlickException{

    super.render(gc, g);
    box = new Image("res/obj/box1.png");
    g.drawImage(box, x, y);
}

public void update(GameContainer gc, int delta) throws SlickException{

    super.update(gc, delta);
}
}

如果您有任何想法或可以提供任何帮助,请告诉我。我已经在网上找了几天,并与其他几个人交谈过,但似乎没有人对出了什么问题有具体的了解。

【问题讨论】:

    标签: java collision-detection slick2d


    【解决方案1】:

    如果你这样做: this.x == entity.x && this.y == entity.y 它只需要图像/碰撞的 1 个点,而不是整个左侧

    你要做的是在它周围画一个形状(矩形、圆、多边形等) 并像这样使用 intersects() 方法:

    Rectangle entityCollision;
    Rectangle boxCollision;
    
    public Entity() {
        //your code here
    }
    
    public void init(arguments){
        entityCollision = new Rectangle(arguments);
        boxCollision = new Rectangle(arguments);
    }
    
    public void render(arguments){
        //your code here
    }
    
    public void update(arguments){
    
        if(entityCollision.intersects(boxCollision)){
            //this is where the computer checks the collision
            //your code here
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      你不使用你的碰撞方法, 在你的 Larry 类的更新方法中,设置一个条件,如果发生碰撞,请不要移动你的播放器

      (尝试向下移动时的示例

      if (input.isKeyDown(Input.KEY_UP)) {
              if(isBottomCollision(this)) {
                   y -= speed * delta;
                   if (y < 0) {
                       y += speed * delta;
                   }
              }
          }
      

      这样,如果玩家尝试向下但发生碰撞,他将无法移动。 您只能使用一种方法来与额外的参数(一个枚举)或两个(x 和 y)发生冲突,这告诉方法玩家试图走哪条路

      Direction {LEFT, RIGHT, UP,DOWN} //global
      public boolean isCollision(Entity entity, direction direction)
      

      public boolean isCollision(Entity entity,int x, int y)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        • 2014-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多