• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading input from keys

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have a project for my class, where I need to make a pacman game. So I want to be able to control pacman using the arrow keys. We are using gridworld(if any of you have heard of it) to make the game in, which is a grid thing that you can put objects in and manipulate them. So basically, I have been looking at KeyListener, buts seems like it requires a textfield(something I can't use for the project). Are there any other ways to check if a button was pressed. This is a turn based version, so it doesn't have to read continuously.

Hopefully this clarifies things, thanks in advance.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

For your question, you definitely need KeyListener or KeyAdapter. But only text field I doubt that. Ok ask yourself this, what component supports key listeners?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use things like System.in.read(); which reads one keystroke at a time. Have a look at the API documentation and see whether that is of any use.
 
Jimbo Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, so K. Tsang, I'm not really sure what a component is, but at least I know I don't need a textfield. I tried implementing Keylistener, but it gives me a "cannot find symbol" error. I'm pretty sure you import java.awt.* but I might be wrong. Campbell, I'm not really sure how to use read, but it would probably be something like this? I'm not sure.


Please correct me if I'm wrong, but I presume this is how it is used, or would I have to make an object of sorts? Thanks for the replies.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it. It looks promising. You might be better off with a switch-case-default block however. You will have to try it, and put a "System.out.println(x);" line in, just to see whether you get a useful input from the four arrow keys. You may need it inside a while loop, which tests whether you have entered a key representing "exit."

Beware: The System.in.read() method declares it throws an IOException, which is checked, so you must deal with that exception somehow.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, another point. Look up the return type of System.in.read(). I think it is int not char, so you might need a cast.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this to see what happensDo it yourself and see what happens.
 
Jimbo Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, so I tried the program using keys and arrows, but nothing happened, and yes I did copy it correcly. I ran it, and pressed some keys, but no output.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it didn't work? I got it to work, but only by using the enter key. It obviously doesn't answer your problem.

KeyListeners might work, but I think that would require a GUI component which has the focus. I have never found a way of implementing the "press any key to continue" in Java.

Has anybody else got any ideas, please?
 
Jimbo Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well my teacher mentioned something about ActionListener. Not sure what that is all about though, thought it was related to KeyListener. I guess I should check that out. Also, what happened when you hit enter?
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have more luck looking in the Games section of JavaRanch, but besides that I have done many games in Java and KeyListeners work great (including arrow keys, and no you don't need a text box). I am at work so I do not have any code with me at the moment but I will get some out when I get home for you.

As this is a learning site and not a "do your homework for you" site I'll only be posting code fragments and psudo-code. It should be enough though for you to figure the rest out from (sorry). When is your assignment due by? Have you gotten the rest of the program done? Is anything else holding you up? Are you adding mouse functionality to it (not much more code then keyboard reading)?

I don't know what your level of experience or how involved the pacman game is but keep in mind that if you're programming correctly using object oriented methods then even if you are completely hammered on the key-reading part you should still be able to write everything else. Hope that made sense. I should be able to get something posted for you in 2-3 hours.
 
Brian Legg
Ranch Hand
Posts: 488
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PM sent
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never used the Gridworld API, (never even heard of it until about 5 minutes ago). But from just a cursory look at the documentation for info.gridworld.world.World<T>, it exposes a method named keyPressed(String, Location) who's documentation reads:

This method is called when a key was pressed. Override it if your world wants to consume some keys (e.g. "1"-"9" for Sudoku). Don't consume plain arrow keys, or the user loses the ability to move the selection square with the keyboard.

Parameters:
description - the string describing the key, in this format.
loc - the selected location in the grid at the time the key was pressed
Returns:
true if the world consumes the key press, false if the GUI should consume it.



Perhaps, this is the method you should be looking to implement.
 
Jimbo Jenkins
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Brian, thanks for the PM. I know that I shouldn't ask people for homework, which is why I asked specifically for the key input. The rest of my program is done, it's a turn based game, so the ghosts wait for pacman to move before they move(I know it weird, but its how gridworld is set up). Also Garrett, we never received that part of the API, which would explain why I never saw that. I will check it out and that should be much easier to integrate into the program.


Thanks everybody fr the help. It's much appreciated.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Legg wrote:PM sent

Please don't. We all want to see the solution. Look at this FAQ.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic