控制台中的关键事件?

时间:2015-04-11 13:17:14

标签: java keylistener

我正在尝试构建一个基于文本的平台游戏,但他们的键盘输入不起作用......帮助?

理论上,这是代码,只要按下箭头键,它就应该移动小x。

import java.awt.event.*;
public class MAIN implements KeyListener{
    private static int renderSizeX = 100;
    private static int renderSizeY = 25;
    public static String keyInput="";

    public static void main(String[] args){

        point player = new point(50,10,'X');
        while(true==true){
            m.sleep(160);
            if(keyInput=="38")//up key
                player.moveY(1);
            if(keyInput=="40")//down key
                player.moveY(-1);
            if(keyInput=="37")//< key
                player.moveX(-1);
            if(keyInput=="30")//> key
                player.moveX(1);
            m2.render(new point[]{player});
        }
    }

public static int windowX(){ return renderSizeX;}

public static int windowY(){ return renderSizeY;}
//KEYBOARD INPUT!
     public void keyTyped(KeyEvent e){
          keyInput = e.getKeyCode()+"";
     }

     public void keyPressed(KeyEvent e){
     }

     public void keyReleased(KeyEvent e){
          keyInput = "";
     }
}

我认为我的渲染类和点类没有任何问题,但我也会展示它们。

public class point{
    int x, y;
    char letter;
    public point(int inX, int inY, char letterRepresent)
    {
        x = inX;
        y = inY;
        letter=letterRepresent;
    }

    public char getChar(){return letter;}
    public int getX(){return x;}
    public int getY(){return y;}
    public void moveX(int ammount){x+=ammount;}
    public void setX(int location){x=location;}
    public void moveY(int ammount){y+=ammount;}
    public void setY(int location){y=location;} 
}

 public class m{

     //sleep method
    public static void sleep(int milli){
         try {
            Thread.sleep(milli);                 //1000 milliseconds is one second.
         } catch(InterruptedException ex) {
             Thread.currentThread().interrupt();
         }


public class m2

{
        public static void render(point[] input){
            System.out.print("\f");//Clears screen
            for(int y = 0; y < MAIN.windowY(); y++){
            for(int x = 0; x < MAIN.windowX(); x++){
                char found = ' ';

                for(int i = 0; i < input.length;i++){
                    if( input[i].getY() == y && input[i].getX() == x ){//checks to see if list of points contains a point at current x and y position
                        found = input[i].getChar();
                    }
                }
                System.out.print(found);//prints char if found else prints space
            }
            System.out.print("\n");
        }

    }

}

1 个答案:

答案 0 :(得分:0)

找到了解决方案!

我刚刚构建了一个呈现文本的小程序!

如果您有兴趣

,这是代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class _Newmain extends Canvas {
    private static int renderSizeX = 25;
    private static int renderSizeY = 25;

    private point player = new point(10,10,'X');
    public _Newmain() {
        setSize(new Dimension(500, 500));
        addKeyListener(new KeyAdapter(){
                @Override
                public void keyPressed(KeyEvent evt) {
                    moveIt(evt);
                }
            });
    }

    public void paint(Graphics g) {
        String text = m2.outputScreen(new point[]{player});
        int x = 20; int y = 20;
        for (String line : text.split("\n"))
            g.drawString(line, x, y += g.getFontMetrics().getHeight());
        //g.drawString(m2.outputScreen(new point[]{player}), 20, 20);
    }

    public void moveIt(KeyEvent evt) {
        switch (evt.getKeyCode()) {
            case KeyEvent.VK_DOWN:
            player.moveY(1);
            break;
            case KeyEvent.VK_UP:
            player.moveY(-1);
            break;
            case KeyEvent.VK_LEFT:
            player.moveX(-1);
            break;
            case KeyEvent.VK_RIGHT:
            player.moveX(1);
            break;
        }

        repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("TEXT BASED GAME WRITTEN BY SHANE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _Newmain ex = new _Newmain();
        frame.getContentPane().add(ex);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        ex.requestFocus();

    }
}