简体   繁体   English

游戏编程中的Java类?

[英]Java Classes in game programming?

I'm doing a little strategy game to help me learn Java in a fun way. 我正在做一个小策略游戏,以帮助我以有趣的方式学习Java。 The thing is I visioned the units as objects that would self draw on the game map (using images and buffering) and would react to the mouse actions with listeners attached to them. 事情是我将单位视为可以在游戏地图上自我绘制的对象(使用图像和缓冲),并且会对附加到其上的听众做出反应。

Now, based on some tutorials I've been reading regarding basic game programming, all seems to be drawn in the Graphics method of my Map class. 现在,基于我一直在阅读的有关基本游戏编程的一些教程,所有内容似乎都是在我的Map类的Graphics方法中绘制的。 If a new unit emerges, i just update the Map.Graphics method, it's not as easy as making a new Unit object which would self draw... In this case, I'd be stuck with a whole bunch of Map methods instead of using classes for rendering new things. 如果出现一个新单元,我只是更新Map.Graphics方法,它不像制作一个自绘的新单元对象那么容易......在这种情况下,我会被一大堆Map方法所困扰,而不是使用类来渲染新事物。

So my question is, is it possible to use classes for rendering units, interface objects, etc, or i'll have to create methods and just do some kind of structural programming instead of object oriented? 所以我的问题是,是否可以使用类来渲染单元,接口对象等,或者我将不得不创建方法,只是做一些结构编程而不是面向对象? I'm a little bit confused and I'd like to have a mental blueprint of how things would be organized. 我有点困惑,我想有一个关于如何组织事情的心理蓝图。

Thanks! 谢谢!

Sounds like your objects need to have a common interface that they can all be down-casted to for rendering. 听起来你的对象需要有一个公共接口,它们都可以下载到渲染中。

Eg have a generic list of your interface type, populate it with the game objects, then enumerate the list and call the common method to do the rendering. 例如,有一个接口类型的通用列表,用游戏对象填充它,然后枚举列表并调用常用方法来进行渲染。


This is C# code, but it should be similar for Java 这是C#代码,但它应该与Java类似

public interface IRenderable
{
    void RenderMe(Graphics graphics)
}

List<IRenderable> myGameObjects;

foreach (IRenderable myGameObject in myGameObjects)
{
    myGameObject.RenderMe(myMap);
}

While the objects are held in the list they can respond to events and update their internal state ready for the next render cycle. 当对象保存在列表中时,它们可以响应事件并更新其内部状态,为下一个渲染周期做好准备。

While "real" game projects come in many forms, ultimately you have something like this: 虽然“真正的”游戏项目有多种形式,但最终你有这样的东西:

while True:
   ProcessUserInput()

   for o in objects:
      o.Update()

   for o in objects:
      o.Render()

Use whatever mechanism you find dear to implement any and all of these functions. 使用您发现的任何机制来实现任何和所有这些功能。 :) Polymorphism is fine, so you should feel free to use that if you're comfortable with that. :)多态性很好,所以如果你对此感到满意,你应该随意使用它。 The "world map" likely should be the first item in the list, unless your rendering system is sufficiently advanced to handle out-of-order drawing (depth sorting, basically). “世界地图”可能应该是列表中的第一项,除非您的渲染系统足够先进以处理无序绘图(基本上是深度排序)。

From your question, it may be that the units are owned by the map? 从您的问题来看,可能是单位归地图所有? In that case, the Map.Render function would iterate over all of the units that it owns, calling Render on each one of them in turn. 在这种情况下,Map.Render函数将迭代它拥有的所有单元,依次调用每个单元上的Render。 Regardless, it's probably cleanest if the code to draw a unit is in the class for that unit. 无论如何,如果绘制单位的代码在该单元的类中,它可能是最干净的。

Good luck! 祝好运!

As ck said, you could do this with a drawable interface, something like: 正如ck所说,你可以用一个可绘制的界面做到这一点,例如:

public interface GameElement {
    public void draw(Graphics g);
}

public class Sprite implements GameElement{
    private Image image;
    private int x;
    private int y;
    public void draw(Graphics g) {
                g.drawImage(image,x,y,null);
    }
}

public class GameLoop {

        public void drawElements(final Graphics2D g, final List<GameElement> elements) {
                for (final GameElement gameElement : elements) {
                        gameElement.draw(g);
                }
        }


}

but the way you said you want to do it, with a listener, may give you problems in the future. 但是你说你想要这样做的方式,听众,可能会在将来给你带来麻烦。 As dash-tom-bang already pointed, all elements are rendered in a single game loop, after all of them were already updated. 正如dash-tom-bang已经指出的那样,在所有元素都已经更新之后,所有元素都在一个游戏循环中呈现。 If you do this with a listener and you have a game loop or any other thread modifying the unit list, you can end up with a concurrent access modification. 如果您使用侦听器执行此操作并且您有游戏循环或修改单元列表的任何其他线程,则最终可以进行并发访问修改。 Besides, this way you will draw all elements in a single point, that will avoid some weird flickering on your game. 此外,通过这种方式,您可以在一个点上绘制所有元素,这将避免游戏中出现一些奇怪的闪烁。

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

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