简体   繁体   English

如何使用Java在png图像上创建网格图?

[英]How do i create a grid map on a png image with java?

The thing is that i'm helping a friend with a java project for the universty, and we have to create a remake of MSX's Knightmare. 事实是,我正在帮助一个Java项目的朋友来实现大学,我们必须重制MSX的Knightmare。 The game is pretty developed already but we don't know how to create the collision with the scenary's stuff, like the columns and the bridge over the river. 游戏已经相当成熟,但是我们不知道如何与场景中的东西(如圆柱和河上的桥)产生碰撞。 In order to do this i thought that we could just create a logical "grid-mapping" over the png (the whole level is this image, invoked on the game like a plain image) and do the logic on it, just a boolean logic. 为了做到这一点,我认为我们可以在png上创建逻辑“网格映射”(整个级别是此图像,像在普通图像上一样在游戏中调用)并对其进行逻辑处理,只是布尔逻辑。

So the basic way, that i suggest to do, is a 2D array with this grid calculated on our minds. 因此,我建议这样做的基本方法是根据我们的想法计算出此网格的2D阵列。 Is there some tool that we could use in order to do this easier or automated somehow? 我们是否可以使用某些工具以某种方式更轻松或自动地完成此任务?

Knightmare original version video Knightmare原始版本视频

And this is the image that we are using on the game 这就是我们在游戏中使用的图像

I am going to try answering the title question, which seemed like the important one. 我将尝试回答标题问题,这似乎很重要。

I wrote a little platform game in Java some time ago, and there I wrote a method called comprobarColisiones() (check collisions) which I call everytime 我前段时间用Java编写了一个小平台游戏,在那里我编写了一个名为comprobarColisiones()的方法(检查冲突),我每次都会调用它

// 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();

When I mean "static objects" it's just to differentiate objects with self-movement with those that just scroll as long as the hero moves, in your game (as in mine's) there probably will not be any static object (well, probably the scores and stuff), even rocks will scroll. 当我指的是“静态对象”时,它只是将自我移动的对象与随英雄移动而滚动的对象区分开来,在您的游戏中(如我的),可能不会有任何静态对象(嗯,可能是分数和东西),甚至岩石也会滚动。

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");
    }        
}

I created enemies and static stuff and placed them when I load the escenario, just like this: 我创建了敌人和静态内容,并在加载场景时将其放置,如下所示:

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

Probably in your game, you'll define any area of the map as explorable, and add some ghost objects to check intersection with the hero and stop his movement. 可能在您的游戏中,您将地图的任何区域定义为可探索的,并添加一些幻影对象以检查与英雄的交集并停止其移动。 For what I could see, water and columns should NOT be explorable by your hero. 就我所见,英雄不应该探索水和柱子。

Hope this would give you some ideas. 希望这会给您一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM