Computer Science Canada Arrowkey movement for Pong |
Author: | grahamhallett [ Mon Jan 01, 2007 2:05 pm ] |
Post subject: | Arrowkey movement for Pong |
This is the movement part of my program so far: public void keyTyped(KeyEvent e) { keypressed += e.getKeyChar(); if (keypressed.equals("w")) { repaint(); try{ Thread.sleep(100); } catch(InterruptedException d) { } Graphics g = getGraphics(); g.setColor(Color.GREEN); g.fillRect(10,paddle1y, 10,40); paddle1y = paddle1y- paddle1ychange; keypressed =""; } else if (keypressed.equals("s")) { repaint(); try{ Thread.sleep(100); } catch(InterruptedException d) { } Graphics g = getGraphics(); g.clearRect(10,paddle1y, 10,40); paddle1y = paddle1y+paddle1ychange; keypressed =""; } if (keypressed.equals("i")) { repaint(); try{ Thread.sleep(100); } catch(InterruptedException d) { } Graphics g = getGraphics(); g.clearRect(380,paddle2y, 10,40); paddle2y = paddle2y-paddle2ychange; keypressed = ""; } else if (keypressed.equals("k")) { repaint(); try{ Thread.sleep(100); } catch(InterruptedException d) { } Graphics g = getGraphics(); g.setColor(Color.GREEN); g.fillRect(380,paddle2y, 10,40); paddle2y = paddle2y+paddle2ychange; keypressed =""; } else{ keypressed = ""; } } public void keyPressed(KeyEvent d) { } public void keyReleased(KeyEvent f) { } public void update(Graphics g) { paint(g); } } I am trying to change the movement with i and k to using movement with the arrowkeys but i am not sure how. Any suggestions? This program is pong by the way. |
Author: | ericfourfour [ Mon Jan 01, 2007 7:08 pm ] | ||||
Post subject: | |||||
I'm not to sure what you are asking but is this what you are trying to do?
If so, that is not very hard to implement once you understand what you are doing. Also if you are going to use threads why not implement one and use the method run to repaint your program instead of writing it inside every if. All painting should also be done in the paint method. It was made for a reason. You should also try to stick to one way to use curly brackets. At some parts i see them immediately after the command and other I see them on the next line. For example:
|
Author: | grahamhallett [ Tue Jan 02, 2007 11:58 pm ] |
Post subject: | |
I am simply trying ot make the paddle move with arrow keys. I was just showing you what i ahve for the movement so far i was just wondering what i have to put in order to do the arrow keys. |
Author: | ericfourfour [ Wed Jan 03, 2007 11:22 pm ] |
Post subject: | |
I'm too lazy to check what it is named, but instead of using e.getKeyChar(); use the one that returns an integer. Then use the VK_'s for the keys. If you have an IDE with an intellisense thing you should be able to find the appropriate one by typing KeyEvent., and if not you can either look up the KeyEvent javadoc or find a list. |
Author: | grahamhallett [ Thu Jan 04, 2007 1:40 pm ] |
Post subject: | |
I am a newbie -_- No idea what you are talking about ha. |
Author: | ericfourfour [ Thu Jan 04, 2007 5:10 pm ] |
Post subject: | |
Look for KeyEvent in the javadoc. You might even find a good tutorial from there. |
Author: | grahamhallett [ Sat Jan 06, 2007 10:04 am ] |
Post subject: | |
Anyone else? :O |
Author: | wtd [ Sat Jan 06, 2007 10:11 am ] |
Post subject: | |
You will be more likely to receive help if your code is posted in its entirety, in code tags, and is well-formatted. |
Author: | grahamhallett [ Sat Jan 06, 2007 10:38 am ] | ||
Post subject: | |||
|
Author: | grahamhallett [ Sat Jan 06, 2007 10:39 am ] |
Post subject: | |
Here's the code. Not sure what code tags are. |
Author: | wtd [ Sat Jan 06, 2007 11:54 am ] |
Post subject: | |
To deal with your earlier question, the KeyEvent class contains a number of integer constants which represent the various keys on your keyboard. If you use these, then you can identify a key which does not have a reasonable string representation. |
Author: | grahamhallett [ Mon Jan 08, 2007 11:58 am ] |
Post subject: | Re: Arrowkey movement for Pong |
okay i figured out the different integers for the arrow keys and figured out what i needed. Thanks a lot everyone. |