Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java Pong Help, Please!

Started by SynysterGates Jan 16, 2008 at 6:32 PM 4 replies 1k views
Original Post
SynysterGates
SynysterGates
[EDITED] Got the user input to work, thanks everyone! If you want to share your AI for the computer paddle feel free. thanks! [Edited by - SynysterGates on January 20, 2008 6:20:49 PM]
greggles
greggles
First of all, welcome!

You have two APIs from which to choose. You could use the event framework provided in awt, as described in the swing tutorial (specifically, you would probably implement the java.awt.event.KeyListener interface). You could also use jinput. The one that you choose depends on the types of software that you are planning on making. jinput is designed for "high performance games in Java" and gives you the ability to use things such as controllers, while awt Events are designed more towards gui. I would, however, suggest that in this case you will probably want to stick with awt events for now since your game is just a basic pong game and awt events are easier to set up and use.

So, I would suggest that you read the swing events tutorial, which should give you enough information to begin. If you have further questions, than feel free to ask.
SynysterGates
SynysterGates
Hey thanks for those links guys. They were really interesting to read through but unfortunatly I'm not very java-literate when it comes to EventListeners, KeyEvent, etc. I tried to read through it but one concept relied on another i was not familiar with and that one relied on yet another i wasnt familiar with.
Needless to say, it didn't work out :P

Anyway, what i am looking for is some code that can run in an infinite loop (while(1==1){...}) that would read the users input. I did find some of these but most were in a text input box (something that would clearly not work for Pong.)

For example, if the user is holding down the 'a' key on the keyboard, the program would output "hoorah" as long as the key is being held. Else, if the user is holding 'b' the program would output "Hooray" as long as the key is being held. Else, if some other button (or no button at all) is being held/pressed the program would do nothing. (This is obviously a refrence to the pong game, the 'a' action can be turned to paddleRight.setYHigher(1); and 'b' would be paddleRight.setYHigher(-1); for example)

From the reading i did from your links, i basically broke the code down to this example:
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if(c =='a')
System.out.println("hoorah");
else if(c == 'b')
System.out.println("Hooray");
}

keyTyped(e) could be called inside the infinite loop. The problem is... I don't know how to get e (or the key typed) in the infinite loop. If anyone can please either simply show me how or give me a website that is a bit easier to understand for a beginer it would be greatly appriciated. :)

Sorry the posts are like 400 lines long, i just want to make this thing work!
[Last visual "game" i made was with VB where the user controlled an image of a car from an arial view.]
Thanks in advance!
Swattkidd
Swattkidd
This should help you out a lot!

http://cksstudios.com/

go to the "Intro game dev tutorials"

Looks like what you are looking for :)
SynysterGates
SynysterGates
Thanks for that last link but I got it to work about four days ago :D!
I'm really happy with how the Pong game turned out.
Though the AI for the computer controlled paddle could be a bit more clever.
If anyone is willing to share how they would/did design the AI, feel free to post it there.
Currently mine is:
if ball is above you, go up.
if ball is below you, go down.

not too tricky :D

ok thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.