ok what i'm trying to do is make a pong game in an applet. here's my code:
//<applet code = Pong width = 800 height = 600></applet>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.bruceeckel.swing.*;
public class Pong extends JApplet implements KeyListener
{
int px1 = 770, px2 = 20, py1 = px2, py2 = px2, cx = 400, cy = 300;
int temp_py1 = py1, temp_py2 = py2;
boolean esc = false, down_pressed1 = false, down_pressed2 = false, up_pressed1 = false, up_pressed2 = false;
static final int WIDTH = 10, HEIGHT = 100, CIRCLE = 10;
public void init()
{
setBackground(Color.black);
addKeyListener(this);
}
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillOval(cx, cy, CIRCLE, CIRCLE);
g.fillRect(px1, py1, WIDTH, HEIGHT);
g.fillRect(px2, py2, WIDTH, HEIGHT);
if(down_pressed1)
{
temp_py1 = py1;
py1 += 10;
g.clearRect(px1, temp_py1, WIDTH, 10);
}
if(down_pressed2)
{
temp_py2 = py2;
py2 += 10;
g.clearRect(px2, temp_py2, WIDTH, 10);
}
if(up_pressed1 && py1 > 0)
{
temp_py1 = py1 + 90;
py1-= 10;
g.clearRect(px1, temp_py1, WIDTH, 10);
}
if(up_pressed2 && py2 > 0)
{
temp_py2 = py2 + 90;
py2-= 10;
g.clearRect(px2, temp_py2, WIDTH, 10);
}
repaint();
wait(20);
}
public void keyPressed(KeyEvent e)
{
/*if(e.getKeyCode() == KeyEvent.VK_ESCAPE && !esc)
{
esc = true;
}
else if(e.getKeyCode() == KeyEvent.VK_ESCAPE && esc)
{
esc = false;
}*/
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
down_pressed1 = true;
}
else if(e.getKeyCode() == KeyEvent.VK_UP)
{
up_pressed1 = true;
}
else if(e.getKeyCode() == KeyEvent.VK_S)
{
down_pressed2 = true;
}
else if(e.getKeyCode() == KeyEvent.VK_W)
{
up_pressed2 = true;
}
e.consume();
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_DOWN)
down_pressed1 = false;
if(e.getKeyCode() == KeyEvent.VK_S)
down_pressed2 = false;
}
public void keyTyped(KeyEvent e)
{
}
public static void wait(int n){
long t0, t1;
t0 = System.currentTimeMillis();
do{
t1 = System.currentTimeMillis();
}
while (t1 - t0 < n);
}
}
whenever one of the paddles goes to edge of the top i cant move it down at all and when it hits the bottom and i press the up key (for either side) it rockets to the top and stays there. how can i fix this?