Skip to main content

Non-GUI KeyListener for Arrow Keys

No replies
justinpchang
Offline
Joined: 2013-08-31
Points: 0

Hello, everyone!
I have been working on a very very simple text game. So far, I have created the map, and I am trying to create the player. The player is just a combination of characters such as -[]- or something. This is the code for the main class:

package me.justin.JavaGame;

import java.io.IOException;

import javax.swing.*;

public class Program extends javax.swing.JFrame{

public static void main(String args[]){
Map map = new Map();
map.mapCreate();
Player player = new Player();
}

}

This is the code for the map class:

package me.justin.JavaGame;

class Map{

public static int[] map =
//1 2 3 4 5 6 7 8 9 10
{ 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, //0
1, 1, 1, 2, 2, 1, 1, 1, 1, 1, //1
1, 1, 1, 1, 2, 2, 1, 1, 1, 1, //2
1, 1, 1, 1, 1, 2, 2, 1, 1, 1, //3
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, //4
1, 1, 1, 1, 1, 1, 2, 2, 1, 1, //5
1, 1, 1, 1, 1, 1, 1, 2, 2, 1, //6
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, //7
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //8
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 //9
};
public static int mapCol, mapRow;
public static int mapRef = (mapCol + (mapRow * 10)) - 1;

String grass = "[|] ";
String water = "[%] ";

private int counter = 0;

void mapCreate(){
while(counter != 100){
if((counter == 9) || (counter == 19) || (counter == 29) || (counter == 39) || (counter == 49)
|| (counter == 59) || (counter == 69) || (counter == 79) || (counter == 89)){
if(map[counter] == 1){
System.out.println(grass);
} else if(map[counter] == 2){
System.out.println(water);
}
} else {
if(map[counter] == 1){
System.out.print(grass);
} else if(map[counter] == 2){
System.out.print(water);
}
}
counter++;
}
}

int findBlockValue(int refCol, int refRow){
mapCol = refCol;
mapRow = refRow;
int blockVal = map[mapRef];
return blockVal;
}

int mapReference(int refCol, int refRow){
mapCol = refCol;
mapRow = refRow;
return mapRef;
}
}

Please help me with a key reader for the player. I need the player to move based on the pressing of the arrow keys. I am not using JFrame so the Java KeyReader on the java.sun website does not work. IMPORTANT: I am not looking for the code to move the sprite. I am looking for the code that reads the arrow keys. Thanks!