zoukankan      html  css  js  c++  java
  • MarteEngine tutorial:Entity and world

    在完成了最初的Hello World教程,你能够在屏幕上渲染一些文本 ,但是在MarteEngine中的概念还没有很好的解释。

    Entity

    一个Entity是游戏里的任何东西:被玩家控制的英雄,一些闪烁的信息文本和敌人等等的几乎所有东西。MarteEngine中选择的这个概念是为了简单性。

    Entity可以按(也应该按)如下基本Java方式继承:
    public class Player extends Entity {

    }

     如果你创建了一个新的Java类,并使它集成Entity,你必须要做的只有一件事:为它定义一个构造函数:

    public class Player extends Entity {

        /**
         * 
    @param x, x coordinate on screen where Player starts
         * 
    @param y, y coordinate on screen where Player starts
         
    */
        public Player(float x, float y) {
            super(x, y);
            // TODO Auto-generated constructor stub
        }
    }

     你永远可以使用额外的参数定义不同的构造函数:MarteEngine不会强迫你,它只会帮助你。

    另一个例子是在你的英雄上附着一副图像,这样你可以在屏幕适当的坐标上看到一些东西。

    public class Player extends Entity {

        /**
         * 
    @param x, x coordinate on screen where Player starts
         * 
    @param y, y coordinate on screen where Player starts
         
    */
        public Player(float x, float y) {
            super(x, y);
            // load Image from disk and associate it as player image
            setGraphic(new Image("data/image/player.png"));
        }
        
    }

     好了!不需要任何复杂的编码,玩家图像可以自动在给定坐标渲染。

     当然,MarteEngine还提供了更多内置功能(物理效果,碰撞检测和其他),下面的教程将解释这些特点,这些特点是你专注于你的游戏,而不是复杂的编码。所有这些都已经为你做好了。

    World

    World是Entity的容器:可以把它看做是游戏的一关。游戏开始,英雄在带有障碍物、敌人、天空等等的关卡内到处移动, 英雄仅仅是一个Entity,许多其他Entity也是允许的。World允许这样做!

    创建一个World同样是简单的,继承MarteEngine的World类并提供一个基本的构造函数:

    public class Level extends World {

        /**
         * 
    @param id, unique identifier for World
         * 
    @param container, container for World
         
    */
        public Level(int id, GameContainer container) {
            super(id, container);
            // TODO Auto-generated constructor stub
        }

    }

     你可以覆写World类的init方法来加载一个或更多的Entity到World中去。

        @Override
        public void init(GameContainer container, StateBasedGame game)
                throws SlickException {
            super.init(container, game);
            
            Player player = new Player(100,100);
            add(player);
        }

     注意:你永远可以使用下面的代码移除一个Entity

    ME.world.remove(entity);

    这是因为MarteEngine在ME类中用一个静态变量保存当前world的引用,所以你可以在任何地方使用world的所有方法。

    Render and update

     另一个需要理解的关键地方是Entity和World都能更新游戏逻辑并在屏幕上渲染东西。要这样做,你所有的类都要继承Entity或World并覆写两个方法:render和update。

    public class Player extends Entity {

        /**
         * 
    @param x, x coordinate on screen where Player starts
         * 
    @param y, y coordinate on screen where Player starts
         
    */
        public Player(float x, float y) {
            super(x, y);
            // load Image from disk and associate it as player image
            setGraphic(new Image("data/image/player.png"));
        }
        
        @Override
        public void update(GameContainer container, int delta)
                throws SlickException {
            super.update(container, delta);
        }
        
        @Override
        public void render(GameContainer container, Graphics g)
                throws SlickException {
            super.render(container, g);
        }
        
    }
    public class Level extends World {

        /**
         * 
    @param id, unique identifier for World
         * 
    @param container, container for World
         
    */
        public Level(int id, GameContainer container) {
            super(id, container);
        }

        
        @Override
        public void init(GameContainer container, StateBasedGame game)
                throws SlickException {
            super.init(container, game);
            
            Player player = new Player(100,100);
            add(player);
        }
        
        @Override
        public void update(GameContainer container, StateBasedGame game, int delta)
                throws SlickException {
            super.update(container, game, delta);
        }
        
        @Override
        public void render(GameContainer container, StateBasedGame game, Graphics g)
                throws SlickException {
            super.render(container, game, g);
        }
        
    }

     这到底意味着什么?基本上它意味着你可以用任何你喜欢的方式渲染entities(或者说,让MarteEngine帮助你),或者以你自己的方式为特殊的entities更新游戏逻辑(例如玩家控制的entity)。你可以自由地实验和阅读MarteEngine的源代码来理解MarteEngine是怎样帮助你的。

    ResourceManager

    ResourceManager是一个工具类:用不用随你:它只是在resource.xml中保存所有媒体文件的指针(精灵、图像、声音、字体、常量)。你可以在Java类之外写入该文件以你需要的方式配置游戏。你可以仅仅使用常量引用来使用这些媒体文件,例如我们重新写一下Player类,这次使用ResourceManager来加载图像。

    public class Player extends Entity {

        /**
         * 
    @param x, x coordinate on screen where Player starts
         * 
    @param y, y coordinate on screen where Player starts
         
    */
        public Player(float x, float y) {
            super(x, y);
            // load Image using resource.xml file using costant name
            Image img = ResourceManager.getImage("player");
            setGraphic(img);
        }

     是不是足够简单!你可以在游戏开始的时候这样初始化ResourceManager:

    ResourceManager.loadResources("data/resources.xml");

     resources.xml看起来像这样:

    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
        <!-- basedir -->
        <basedir path="data" />
        <!-- sounds -->
        <!-- songs -->
        <!-- images -->
        <image key="player" file="player.png" />
        <!-- sheets -->
        <!-- fonts -->
    </resources>

     语法(希望是)很清楚!在resource.xml中定义了一个使用player.png的player,所以在游戏中你能使用player引用它。

     ————————————————————————————————————————————————————————————

    在test包中你可以找到一些使用上述这些概念的例子。

     现在你应该继续Keyboard and Mouse Input教程。

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    权重平等分布局And TableRow布局误区
    播放视频的框架Vitamio的使用问题
    Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
    使用PullToRefresh实现下拉刷新和上拉加载
    如何安全退出已调用多个Activity的Application?
    Stirng,Stringbuffer,Stringbuild的区别浅淡
    python3 logging
    从集合中筛选数据
    python3 模块
    python3 字符串的 maketrans,translate 方法详解
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2378250.html
Copyright © 2011-2022 走看看