For my final in cs i made a ping pong type program. I have 4 paddles for each wall and im trying to get it so that the mouse moves the top and bottom (which works fine). I am trying to get the up and down arrow keys to work for the left and right paddles but every time the game starts and you press the arrow keys the paddles go right up to the top left corner. The right paddle disappears for good Ive gone threw the code so many times and ive ran out of ideas so please help me.
I know this is a few pages worth of code most of it is unimportantant to my problem but the method in question is
public void keyPressed( KeyEvent event )
thats where i got it set to move at. Also to get around this problem i tried to set the computer to be the left and right paddles and they start in middle with ball and float off diagionally to the top right. they are commented out in the pong class.
package pingpong;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Pong extends Applet implements Runnable, MouseMotionListener, MouseListener, FocusListener, KeyListener
{
final int refreshRate = 10;
protected Thread Game;
protected Graphics osGraphic;
protected Image osi;
protected Graphics bGraphic;
protected Image bImage;
Ball orb = new Ball(300, 300, 15, 15);
//bottom
Paddle player = new Paddle(300, 583, 60, 10, 70, 30,70,30);
//top
Paddle player1 = new Paddle(300, 8, 60, 10, 70, 30,70,30);
//left
Paddle player2 = new Paddle(8, 300, 10, 60, 70, 30,70,30);
//right
Paddle player3 = new Paddle(782, 300, 10, 60, 70, 30,70,30);
final int width = 800;
final int height = 650;
boolean gameStart = false;
boolean gameOver = false;
boolean frun = true;
int computerScore =0;
int playerScore = 0;
int life = 3;
// This is a char variable used for the move method in the ball class.
// If this value is 'n', neither the computer nor the player have scored.
// If this value is 'c', the player scored a point.
// If this value is 'p', the computer scored a point.
// If this value is 'w', the ball hit the wall.
// If this value ia 'a', the ball hit the bottom paddle.
char rval='n';
public void focusGained(FocusEvent fEvent){
repaint();
}
public void focusLost(FocusEvent fEvent){
repaint();
}
public void init()
{
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener( this );
this.setSize(width, height);
this.setName("Ping Pong");
osi=createImage(width,height);
osGraphic=osi.getGraphics();
Game=new Thread(this);
this.setFocusable(true);
}
public void startUp()
{
if(frun)
Game.start();
frun = false;
System.out.println("Game Started");
}
public void paint(Graphics g)
{
//background color and size
osGraphic.setColor(Color.black);
osGraphic.fillRect(0,0,width,height);
osGraphic.setColor(Color.white);
// draws strings
osGraphic.drawString("Player Score: " + playerScore, 15, 625);
osGraphic.drawString("Lifes Remaining: " + life, 300, 625);
// draws ball
osGraphic.setColor(Color.green);
osGraphic.fillOval(orb.getX(), orb.getY(), orb.getH(), orb.getW());
osGraphic.setColor(Color.blue);
//bottom = player, top = player1, left = player2, right = player3
osGraphic.fillRect(player.getX(), player.getY(), player.getWidth(), player.getHeight());
osGraphic.fillRect(player1.getX(), player1.getY(), player1.getWidth(), player1.getHeight());
// player2.setPosition(orb.getX()- 24, player2.getY()- 1);
osGraphic.fillRect(player2.getX(), player2.getY(), player2.getWidth(), player2.getHeight());
//player3.setPosition(orb.getX()- 24, player3.getY()- 1);
osGraphic.fillRect(player3.getX(), player3.getY(), player3.getWidth(), player3.getHeight());
if(!gameStart)
{
osGraphic.setColor(Color.white);
osGraphic.drawString("Click to start", (width/2)+30, (height/2)-40);
}
if(gameOver)
{
osGraphic.setColor(Color.white);
osGraphic.drawString("You ran out of lifes try again please", (width/2)-30, (height/2)-40);
}
g.drawImage(osi,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
this.requestFocus();
while(!gameOver)
{
if(gameStart)
rval = orb.move(player, player1, player2, player3);
// If this value is 'c', the ball hit top wall.
if(rval == 'c')
{
}
// If this value is 'p', the ball hit paddle.
if(rval == 'p')
{
playerScore ++;
bouncePaddle();
}
// If this value is 'w', the ball hit the wall.
if(rval == 'w')
{
bounceWall();
}
// If this value ia 'a', the ball hit the bottom wall.
if(rval == 'a')
{
life--;
lose();
}
try
{
Game.sleep(refreshRate);
}
catch(Exception e)
{}
this.repaint();
}
}
public void keyPressed( KeyEvent event )
{//left paddle
if (event.getKeyCode() == KeyEvent.VK_UP )
{
// left paddle
if((getY() < player2.getUBound()) || (getY() > player2.getDBound()))
player2.setPosition(getY() + 2, player2.getX());
if((getY() < player3.getUBound()) || (getY() > player3.getDBound()))
player3.setPosition(getY() + 2, player3.getX());
}
if (event.getKeyCode() == KeyEvent.VK_DOWN )
{ //right paddle
if((getY() < player2.getUBound()) || (getY() > player2.getDBound()))
player2.setPosition(getY() - 2, player2.getX());
if((getY() < player3.getUBound()) || (getY() > player3.getDBound()))
player3.setPosition(getY() - 2, player3.getX());
}
requestFocus();
// event.getKeyText( event.getKeyCode() );
}
public void keyReleased( KeyEvent event )
{
}
public void keyTyped( KeyEvent event )
{
}
public void mouseMoved(MouseEvent e)
{
//bottom paddle
if((e.getX() < player.getRBound()) || (e.getX() > player.getLBound()))
player.setPosition(e.getX() - 25, player.getY());
//top paddle
if((e.getX() < player1.getRBound()) || (e.getX() > player1.getLBound()))
player1.setPosition(e.getX() - 25, player1.getY());
//requestFocus();
}
public void mouseEntered(MouseEvent event)
{}
public void mouseExited(MouseEvent event)
{}
public void mousePressed(MouseEvent event)
{}
public void mouseReleased(MouseEvent event)
{}
public void mouseClicked(MouseEvent e)
{
if(!gameStart || gameOver)
{
gameOver = false;
gameStart = true;
startUp();
}
}
public void mouseDragged(MouseEvent e)
{}
public void bounceWall()
{
}
public void bouncePaddle()
{
}
public void lose(){
if (life == 0){
gameOver = true;
this.repaint();
}
}
public static final int VK_UP = 26;
public static final int VK_DOWN = 28;
}