Archived

This topic is now archived and is closed to further replies.

[java] getting input

This topic is 5380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im working on a zelda like game for my OOP class, and im using BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String input = stdin.readLine(); if(input.equals("W")) { //move up... } to make the player move. is there a function that returns the current state of the keyboard? the way im doing it now, the game waits on the player to get input, and i dont want to do that. i need to have something like getInput(); moveSprites(); drawGraphics(); also the game right now is done in the console. could someone tell me which classes to use to print stuff out as graphics? just point me in the right direciton, i dont need a tutorial. thx in advance. really appreciate it. -stephen

Share this post


Link to post
Share on other sites
For graphics and keyboard handling, check out java''s Abstract Windows Toolkit package : java.awt

But if you haven''t used it before then unless you''re a really hard worker you might find it takes to long to get a grasp of.

Share this post


Link to post
Share on other sites
I'd sooner use swing (javax.swing.*) for graphical rendering myself, but only because I'm more familiar with it. It has just about anything you need to work with (keystrokes, imageicons could be used for animation [though you will need to ensure that your users have a JVM above 1.4 for these], timers, etc.)



#Old Steve, he said to Xerox "Boys, turn your heads and cough"
And when no-one was looking he ripped their interfaces off#
Three Dead Trolls in a Baggie on the truth behind MacOS 1

SketchSoft | SketchNews




[edited by - doodle_sketch on March 24, 2003 5:39:18 AM]

Share this post


Link to post
Share on other sites
Here try using this. These are my Key''s and keyboard classes that I use.

keyJ Class:

  
public class keyJ{
private String keyName;
private int keyValue;
private boolean down;

keyJ(String strName, int intValue){
this.keyName = new String(strName);
this.keyValue = intValue;
this.down = false;
}

public void press(){
this.down = true;
}

public void release(){
this.down = false;
}

public String getName(){
return this.keyName;
}

public int getValue(){
return this.keyValue;
}

public boolean isDown(){
return this.down;
}
}


keyboardJ class

  
import java.util.Vector;
import java.awt.Component;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class keyboardJ implements KeyListener {
private Vector keyList;

keyboardJ(Component c){
this.keyList = new Vector();
c.addKeyListener(this);
}

public void addKey(String name, int val){
this.keyList.addElement(new keyJ(name,val));
}

//abstract methods from the KeyListener that must be implemented

public void keyPressed(KeyEvent e){
this.keyDown(e.getKeyCode());
}

//abstract methods from the KeyListener that must be implemented

public void keyReleased(KeyEvent e){
this.keyUp(e.getKeyCode());
}

//abstract methods from the KeyListener that must be implemented

public void keyTyped(KeyEvent e){
//Do nothing

}

public void keyDown(int kVal){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getValue() == kVal){
((keyJ)this.keyList.elementAt(i)).press();
}
}
}

public void keyDown(String kName){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
((keyJ)this.keyList.elementAt(i)).press();
}
}
}

public void keyUp(int kVal){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getValue() == kVal){
((keyJ)this.keyList.elementAt(i)).release();
}
}
}

public void keyUp(String kName){
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
((keyJ)this.keyList.elementAt(i)).release();
}
}
}

public boolean isKeyDown(String kName){
boolean bRetVal = false;
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
bRetVal = ((keyJ)this.keyList.elementAt(i)).isDown();
}
}

return bRetVal;
}

public int getKeyValue(String kName){
int bRetVal = -1;
for(int i=0; i < this.keyList.size(); i++){
if(((keyJ)this.keyList.elementAt(i)).getName().equalsIgnoreCase(kName)){
bRetVal = ((keyJ)this.keyList.elementAt(i)).getValue();
}
}

return bRetVal;
}

public void report(){
//Report about data stored here

System.out.println("========= KEYBOARD REPORT =========");
System.out.println("Total keys: " + this.keyList.size());
System.out.println("========= KEYBOARD REPORT =========");
}

}



To create the keyboard and a few keys to listen for do something like this:

  
//Set the keyboard

gameKeyboard = new keyboardJ(this);

//Create the keyboard keys

gameKeyboard.addKey("Up", KeyEvent.VK_UP);
gameKeyboard.addKey("Down", KeyEvent.VK_DOWN);
gameKeyboard.addKey("Left", KeyEvent.VK_LEFT);
gameKeyboard.addKey("Right", KeyEvent.VK_RIGHT);


To check if a key is down do something like this:

if(gameKeyboard.isKeyDown("Up")){
Move player up one tile;
}


With this you won''t have to wait for a key to be pressed to continue your loop.

The only thing I should warn you about is in the Java 1.4 the "Tab" key is consumed. Which means that if you try to use that key in your game, it will act like that key has never been pressed no matter how many times you press the Tab key. This doesn''t happen in any of the earlier Java runtimes, but it is just easier for everyone involved if you don''t use that key.

Good luck with your game and be sure to send us a link whe nyou have something for us to see!

Share this post


Link to post
Share on other sites
I really like the aboive, but if you want something a bit more simple, try:

  
class FlightKeys extends KeyAdapter {
public boolean left = false;
public boolean right = false;
public boolean up = false;
public boolean down = false;
public boolean fire = false;
public boolean start = false;

public FlightKeys() {
}

public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){

case(KeyEvent.VK_LEFT):{left=true;};break;
case(KeyEvent.VK_RIGHT):{right=true;};break;
case(KeyEvent.VK_UP):{up=true;};break;
case(KeyEvent.VK_DOWN):{down=true;};break;
case(32):{fire=true;};break;
}
}

public void keyReleased(KeyEvent e){
switch(e.getKeyCode()){

case(KeyEvent.VK_LEFT):{left=false;};break;
case(KeyEvent.VK_RIGHT):{right=false;};break;
case(KeyEvent.VK_UP):{up=false;};break;
case(KeyEvent.VK_DOWN):{down=false;};break;
case(32):{fire=false;};break;
}
}
}

Simply test the boolean values to see if the key''s down at the time.

Share this post


Link to post
Share on other sites
thx for the responses, i think im gonna try that simple thing with the KeyState... i think we''re only allowed to use java''s libraries, not some third party one.

thx again.

-stephen

Share this post


Link to post
Share on other sites