Minecraft 2d Remake:建造和摧毁方块问题

发布于 2025-01-06 16:16:28 字数 616 浏览 2 评论 0原文

我很喜欢用 Java 创建我的世界的 2D 重制版。我知道它可以做得很好,youtube 上的 Orange451 激励我尝试做这个。我从文本文件加载地图上的所有块,当游戏加载时,它将文本文件中的所有块添加到 ArrayList。我创建了一个算法来计算游戏中光标所在块的索引,并添加了一个 MouseListener,这样当我单击它时,它就会用空气块替换该块(基本上会破坏该块)。为了替换 ArrayList 中的块,我使用了 ArrayList set(index, obj) 方法。从理论上讲,它应该能够正常工作,并且在某种程度上确实如此。唯一的问题是,它还在几个街区外的地图上创建了一个黑色空间。这非常令人沮丧,尤其是因为我已经走了这么远。附加信息:我需要一种方法来替换 ArrayList 中的对象,或者更好的方法来执行此操作,因为我的碰撞检测方法还使用 ArrayList 来检测块位置。请帮我!我无法发布图像,但它将块设置为空气纹理,但在它附近创建了一个黑色方块(数组列表中的间隙?)。由于代码太多要发布,这里是整个项目的源代码: Blockworld 2D 源

I am well into creating my 2d remake of minecraft in java. I know it can be done well, orange451 on youtube inspired me to try and make this. I have all blocks on the map loading from text files, and when the game loads, it adds all of the blocks from the text files to an ArrayList. I created an algorithm for calculating the index of the block your cursor is on in the game, and added a MouseListener so that when i clicked it would replace the block with an air block (basically destroying the block). To replace the block in the ArrayList, I used the ArrayList set(index, obj) method. In theory, it should be working correctly, and it in a way does. The only problem is that it also creates a black space in the map a few blocks away. This is extremly frustrating, especially since I have come so far. ADDITIONAL INFO: I need a method that will replace the object in the ArrayList, or a better way to do it because my collision detection method also uses the ArrayList to detect a blocks position. PLEASE HELP ME! I cant post images but its setting the block to the air texture but creating a black square (a gap in the arraylist mabey?) near it. Because theres too much code to post, heres the source code for the whole project: Blockworld 2D Source

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绻影浮沉 2025-01-13 16:16:28

您正在为此苦苦挣扎,因为知道其坐标的对象的 ArrayList 是表示此 2d 结构的疯狂方式。它是无序的 - 你可以反转或洗牌你的 ArrayList,它会绘制相同的内容。它的更新时间为 O(N),因为您必须在 ArrayList 中搜索适当坐标的对象,然后才能替换它。它可以有多个具有相同坐标的对象。它可能处于可见坐标根本没有对应对象的状态——这就是您在这里遇到的情况。

请帮助我

吧。从字节的二维数组(数组,而不是 ArrayList)开始。它允许您使用 256 种块,并且允许您的玩家进行挖掘,而无需通过您的 new AirBlock(0, 0) 疯狂不断地分配内存。要绘制世界,请迭代可见坐标并将字节映射到位图等。

另外:2d Minecraft 已经存在。它的名字叫泰拉瑞亚。

You're struggling with this because an ArrayList of objects that know their coordinate is an insane way to represent this 2d structure. It's unordered - you could reverse or shuffle your ArrayList and it would paint the same. It has O(N) update, as you have to search the ArrayList for an object of the appropriate coordinate before you can replace it. It can have more than one object with the same coordinate. It can be in a state where visible coordinates do not have corresponding objects at all -- which is what you've encountered, here.

PLEASE HELP ME

OK. Start with a two dimensional array (array, not ArrayList) of byte. Which allows you 256 kinds of block, and which allows your players to dig without constantly allocating memory with your new AirBlock(0, 0) madness. To draw the world, iterate over visible coordinates and map bytes to Bitmap or like.

Also: a 2d Minecraft already exists. It's called Terraria.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文