
-----------------------------------
grahamhallett
Mon Jan 01, 2007 2:05 pm

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.

-----------------------------------
ericfourfour
Mon Jan 01, 2007 7:08 pm


-----------------------------------
I'm not to sure what you are asking but is this what you are trying to do?

if i pressed
    move paddle up on y-axis
else if k pressed
    move paddle down on y-axis

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:
try{
Thread.sleep(100);
}
catch(InterruptedException d)
{

}Thread.sleep(100) should also be indented in this example. Actually, most of your code switched between different styles making it very hard to read. You should try sticking to one, preferably the one that is used among most Java programmers (look at some source code to see).

-----------------------------------
grahamhallett
Tue Jan 02, 2007 11:58 pm


-----------------------------------
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.

-----------------------------------
ericfourfour
Wed Jan 03, 2007 11:22 pm


-----------------------------------
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.

-----------------------------------
grahamhallett
Thu Jan 04, 2007 1:40 pm


-----------------------------------
I am a newbie -_- No idea what you are talking about ha.

-----------------------------------
ericfourfour
Thu Jan 04, 2007 5:10 pm


-----------------------------------
Look for KeyEvent in the javadoc. You might even find a good tutorial from there.

-----------------------------------
grahamhallett
Sat Jan 06, 2007 10:04 am


-----------------------------------
Anyone else? :O

-----------------------------------
wtd
Sat Jan 06, 2007 10:11 am


-----------------------------------
You will be more likely to receive help if your code is posted in its entirety, in code tags, and is well-formatted.

-----------------------------------
grahamhallett
Sat Jan 06, 2007 10:38 am


-----------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FinalProjectHal extends JApplet implements KeyListener//makes it so keys are able to move
{
	int x = 10;//variables
	int y = 12 ;
	int xChange =10;
	int yChange = 10;
	
	String keypressed = "";
	
	
	int paddle1y = 180;//paddle variables
	int paddle2y = 180;

	int paddle1ychange = 10;
	int paddle2ychange = 10;
	
	public void init()
	{
	addKeyListener(this);//allows it so keys can be pressed

	
	}	
	//background and ball
	public void paint(Graphics g)
	{
	g.setColor(Color.YELLOW);
	g.fillRect(1,1,399,399);
	g.setColor(Color.BLACK);
	g.fillOval(x,y,15,15);
	g.fillRect(10,paddle1y,10,40);
	g.fillRect(380,paddle2y,10,40);
	repaint();
	
	

		try {Thread.sleep(100); //delay time in millisecs
		}
		catch(InterruptedException ex) { }
		g.setColor(Color.YELLOW);
		g.fillRect(x,y,15,15);

	x = x + xChange;//set variables to allow bouncing to occurr
	y = y + yChange;
	//bounce part of the program
	if (x>=400|| x=400|| y= paddle2y && y