Original Post
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class PongIncomplete extends Applet implements KeyListener, Runnable
{
int width, height; // applet width and height
int humanpaddlex = 50, humanpaddley = 50; // human paddle initial starting location
int comppaddlex = 10, comppaddley = 150;
int humanpaddlewidth = 20, humanpaddleheight = 100; // paddle width and height
int comppaddlewidth = 20, comppaddleheight = 100;
int ballx = 50, bally = 300; // ball starting location
int ballwidth = 10, ballheight = 10; // ball width and height
int deltax = 3, deltay = 3;
Image backbuffer;
Graphics backg;
boolean leftdown = false; // flags for smooth response to key presses
boolean rightdown = false;
boolean updown = false;
boolean downdown = false;
Thread t;
public void init()
{
addKeyListener(this);
width = getSize().width; //get dimensions of applet
height = getSize().height;
humanpaddlex = width - 2 * humanpaddlewidth; //re-position user paddle
//offset from right side
backbuffer = createImage( width, height );//create backbuffer
backg = backbuffer.getGraphics();
drawToBackbuffer();
t = new Thread(this); //create thread for animation loop
t.start();
}
public void update( Graphics g )
{
g.drawImage( backbuffer, 0, 0, this );
}
public void paint( Graphics g )
{
update( g );
}
public void drawToBackbuffer() //drawing code goes here
{
backg.setColor(Color.white);
backg.fillRect( 0,0, getSize().width, getSize().height);
backg.setColor(Color.black);
backg.drawRect(0,0,width-1,height-1);
backg.fillRect(humanpaddlex,humanpaddley,humanpaddlewidth,humanpaddleheight);//draw userpaddle
backg.setColor(Color.black);
backg.drawRect(0,0,width-1,height-1);
backg.fillRect(comppaddlex,comppaddley,comppaddlewidth,comppaddleheight);
backg.fillOval(ballx,bally,ballwidth,ballheight); //draw ball;
repaint();
}
public void keyPressed(KeyEvent ke)
{
switch (ke.getKeyCode())
{
case KeyEvent.VK_UP:
updown = true; //we only change states of flags here
break;
case KeyEvent.VK_DOWN:
downdown = true;
break;
}
}
public void keyReleased(KeyEvent ke)
{
switch (ke.getKeyCode())
{
case KeyEvent.VK_UP:
updown = false;
break;
case KeyEvent.VK_DOWN:
downdown = false;
break;
}
}
public void keyTyped(KeyEvent ke){}
public void updateUserPaddle() //get flag states
{
if (updown && humanpaddley>0) //only if arrow up is pressed and
// paddle not too close to top
{
humanpaddley-=4; // move paddle
}
if (downdown && humanpaddley + humanpaddleheight < height) //same for bottom
{
humanpaddley+=4;
}
}
public void updateComputerPaddle()
{
if (bally < comppaddley)
{
comppaddley -= deltay;
}
else if (bally > comppaddley)
{
comppaddley += deltay;
}
}
public void updateBall()
{
ballx = ballx + deltax; //increment ball position by delta
bally = bally + deltay;
if (ballx<=0) //hit left?
{
ballx = 0; //reset to left side
deltax = -deltax; //change movement direction
}
if (ballx + ballwidth >= width) //hit right?
{
ballx = 0;
deltax = -deltax;
}
// If top-left of ball is left of top-right of paddle and
// top-left of ball is to the right of top-left of paddle
if (ballx + ballwidth < humanpaddlex + humanpaddlewidth && ballx > humanpaddlex) // paddle collision
{
// If top of ball is below top of paddle and top of ball is above bottom of paddle
if (bally > humanpaddley && bally < humanpaddley + humanpaddleheight)
{
deltax = -deltax; // reverse direction of ball
}
}
if (bally<=0) //hit top?
{
bally = 0;
deltay = -deltay;
}
if (bally + ballheight >= height) //hit bottom?
{
bally = height-ballheight;
deltay = -deltay;
}
}
public void run()
{
while(t!=null) //animation loop
{
updateBall();
updateUserPaddle();
updateComputerPaddle()
drawToBackbuffer();
try
{
Thread.sleep(20);
}catch(InterruptedException ie){}
}
}
}
Hi guys, this assignment is for a mod class I am taking. Our assignment is to take an almost empty pong game and make it into a complete one. What was given to us initially was the userpaddle and the ball drawings. All the collision detection was added by me. That being said, for some reason the computer paddle AI isn't working and I'm not sure what's wrong. Please keep in mind I am still a beginner programmer and java is a new language to me. Any help would be greatly appreciated.