-1

I am attempting to build a text based platformer, but they keyboard input isnt working... Help?

Here is the code theoretically it should move the little x whenever you press the arrow keys.

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 = "";
     }
}

I dont think there is anything wrong with my rendering class and point class but i will show them as well.

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");
        }

    }

}
10 Replies
  • 426
  • 9
  • 25
  • 1
    A KeyListener is used to listen to keyboard events happening on graphical awt components. Not in the console. And a KeyListener has to be added to something that emits KeyEvents. Otherwise, it will never get called. To read from the console, use `System.in` and learn Java IO. https://docs.oracle.com/javase/tutorial/essential/io/cl.html – JB Nizet Apr 11 '15 at 13:21
  • Should I use the scanner class? or will that not work for what I am tryng to do? – 10 Replies Apr 11 '15 at 13:33
  • I don't think anything in the standard API would allow you doing what you want to do if you want to stay console-based. – JB Nizet Apr 11 '15 at 13:39
  • Change your console to [raw mode](http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) or look into native key listeners (which can give the illusion of typing in the console, as in key events register without GUI, without actually needing to type in the console) – Vince Apr 11 '15 at 14:57
  • I looked into raw mode... and it seems like it is fairly nonportable. Is there a way to get an applet that just hides itself but can still use the keyevents? – 10 Replies Apr 11 '15 at 22:17

1 Answers1

0

Found the solution!

I just built an applet that renders text!

here's the code if your intererested

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();

    }
}
10 Replies
  • 426
  • 9
  • 25