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