如何使用java在png图像上创建网格地图



问题是,我正在帮助一位朋友为University做一个java项目,我们必须创建一个MSX的Knightmare的重制版。游戏已经很发达了,但我们不知道如何与场景中的东西发生碰撞,比如柱子和河上的桥。为了做到这一点,我认为我们可以在png上创建一个逻辑"网格映射"(整个级别是这个图像,在游戏中像普通图像一样调用),并在上面执行逻辑,只是一个布尔逻辑。

因此,我建议的基本方法是,在我们的脑海中计算一个二维阵列。有没有什么工具可以让我们更容易或自动化地做到这一点?

Knightmare原版视频

这是我们在游戏中使用的图像

我将尝试回答标题问题,这似乎是一个重要的问题。

不久前,我用Java写了一个小平台游戏,在那里我写了一种名为comprobarColisions()(检查冲突)的方法,每次都会调用它

// Check collisions
comprobarColisiones();
h.mover(pasadas); //Move hero
// Moving enemies
if (h.x > en.getX()){
    en.mover(h.getdx(), h.getIzq(),pasadas);
}
if (h.x> en2.getX()){
    en2.mover(h.getdx(), h.getIzq(),pasadas);
}
// Moving static objects (rocks)
roca1.mover(h.getdx(),h.getIzq());
roca2.mover(h.getdx(),h.getIzq());
roca3.mover(h.getdx(),h.getIzq());
// REPAINTING
repaint();
// A time calculation that I use to control the game speed somewhere
tiempoAnteriorTipito = System.currentTimeMillis();

当我指的是"静态对象"时,它只是将具有自我移动的对象与只要英雄移动就滚动的对象区分开来,在你的游戏中(就像我的游戏中一样)可能不会有任何静态对象(嗯,可能是分数之类的),甚至岩石也会滚动。

public void comprobarColisiones(){
    // Getting bounds
    Rectangle r1 = en.getBounds();
    Rectangle r2 = en2.getBounds();
    Rectangle rec_roca1 = roca1.getBounds();
    Rectangle rec_roca2 = roca2.getBounds();
    Rectangle rec_roca3 = roca3.getBounds();
    // Getting bounds and resolving collisions with shooting objects
    ArrayList martillos = Heroe.getMartillos();
    for (int i = 0; i < martillos.size(); i++) {
            Martillo m = (Martillo) martillos.get(i);
            Rectangle m1 = m.getBounds();
            if (r1.intersects(m1) && en.vive())
            {
                    en.setVive(false);
                    m.visible = false;
            }
            else if (r2.intersects(m1)&& en2.vive())
            {
                    en2.setVive(false);
                    m.visible = false;                        
            }
    }
    // Checking if hero touches enemies
    Rectangle heroecuad = h.getBounds();
    if (heroecuad.intersects(r1)&&en.vive()){
        perdio = true;
        System.out.println("PERDIO CON EL ENEMIGO 1");
    }
    if (heroecuad.intersects(r2)&&en2.vive()){
        perdio = true;
        System.out.println("PERDIO CON EL ENEMIGO 2");
    }
    // Checking if hero touches static objects
    if(heroecuad.intersects(rec_roca1)){
        System.out.println("CHOCO ROCA 1");
    }
    if(heroecuad.intersects(rec_roca2)){
        System.out.println("CHOCO ROCA 2");
    }        
    if(heroecuad.intersects(rec_roca3)){
        System.out.println("CHOCO ROCA 3");
    }        
}

我创建了敌人和静态的东西,并在我加载escenario时放置它们,就像这样:

en = new Enemigo(800, ALTURA_PISO+8);
en2 = new Enemigo(900, ALTURA_PISO+8);
roca1 = new Roca(1650, ALTURA_PISO+17);
roca2 = new Roca(2200, ALTURA_PISO+17);
roca3 = new Roca(3400, ALTURA_PISO+17);
// Being the first number the initial X-Pos and the second the initial Y-Pos, this will change everytime the hero moves, of course

可能在你的游戏中,你会将地图上的任何区域定义为可探索区域,并添加一些幽灵对象来检查与英雄的交叉点并停止他的移动。就我所见,水和柱子不应该被你的英雄探索。

希望这能给你一些想法。

最新更新