zoukankan      html  css  js  c++  java
  • MarteEngine tutorial:Keyboard and mouse input

    本教程假定你已经完成了Entity and World教程。

     对于你创建的游戏的每一种类型,玩家都要通过键盘和鼠标与之交互:本教程解释了如何使用MarteEngine使之成为可能!

    Keyboard
    在上一个教程中你学习了如何在屏幕上放置你的英雄,但是怎样移动它呢?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
            Image img = ResourceManager.getImage("player");
            setGraphic(img);
            
            // define a command to handle input
            define("RIGHT", Input.KEY_RIGHT);
        }
        
    }

     是不是非常简单:仅仅写了一个命令的名字(这里是“Right”)并把它关联到键盘上的一个按键。谢谢slick这个库你可以访问键盘上的每个键(例如这里的Input.KEY_RIGHT

    然而,定义一个命令还不够, 你所需要的是当玩家按一个键的时候处理它,你可以通过覆盖玩家Entity的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
            Image img = ResourceManager.getImage("player");
            setGraphic(img);
            
            // define a command to handle input
            define("RIGHT", Input.KEY_RIGHT);
        }
        
        @Override
        public void update(GameContainer container, int delta)
                throws SlickException {
            super.update(container, delta);
            
            // check if a key is down 
            if(check("RIGHT")){
                // do anything you like, for example:
                x = x+10;
            }
        }
    }

     通过上面的代码可以看到检查一个你命名过的按键是否被触发是多么简单:你不必关心哪个键被按下,焦点是处理的动作。更好的是,使用命令允许你映射不同的按键到一个命令上。

    在这个例子中,你向右移动英雄10个单位/像素。

    显然你可以定义任何你喜欢的命令并以你喜欢的方式处理。

    Mouse
    鼠标输入遵循同样的概念:定义命令和动作。

    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
            Image img = ResourceManager.getImage("player");
            setGraphic(img);
            
            // define a command to handle input
            define("ATTACK", Input.MOUSE_LEFT_BUTTON);
        }
        
        @Override
        public void update(GameContainer container, int delta)
                throws SlickException {
            super.update(container, delta);
            
            Input input = container.getInput();
            
            // check if a key is down or mouse is clicked
            if(check("ATTACK")){
                // do anything you like, for example:
                float mouseX = input.getMouseX();
                float mouseY = input.getMouseY();
                System.out.println("Player fire at ("+mouseX+","+mouseY+")");
            }
        }
        
    }

     现在你已经准备好Basic collision教程。

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    jQuery性能优化
    Google Chrome 浏览器 错误 312 (net::ERR_UNSAFE_PORT):未知错误。
    C#简单数字验证码解析
    折半查找
    平均分配算法之倒序贪婪
    字符串相似度算法(编辑距离算法 Levenshtein Distance)
    c# 冒泡排序算法中三种前后值交换算法效率测试
    二叉树的遍历
    C#使用Tesseract OCR 解析验证码
    页面瘦身之压缩viewState和保存viewState到服务器
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2378269.html
Copyright © 2011-2022 走看看