gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:46 25 4
gpt4 key购买 nike

事情是,我正在帮助一位 friend 进行大学的 java 项目,我们必须创建 MSX 的 Knightmare 的重制版。游戏已经相当成熟,但我们不知道如何创建与场景元素的碰撞,例如柱子和河上的桥梁。为了做到这一点,我认为我们可以在 png 上创建一个逻辑“网格映射”(整个关卡就是这个图像,在游戏中像普通图像一样调用)并在其上执行逻辑,只是一个 boolean 逻辑。

所以我建议做的基本方法是一个二维数组,并在我们的脑海中计算出这个网格。我们是否可以使用一些工具来更轻松地或以某种方式实现自动化?

Knightmare original version video

And this is the image that we are using on the game

最佳答案

我将尝试回答标题问题,这似乎很重要。

我前段时间用 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();

当我说“静态对象”时,它只是为了区分具有 self 移动的对象和那些只要英雄移动就滚动的对象,在你的游戏中(就像我的游戏一样)可能不会有任何静态对象(好吧,可能是分数之类的),甚至岩石也会滚动。

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

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

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

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

希望这能给你一些想法。

关于java - 如何使用java在png图像上创建网格图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20090134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com