Sign in to follow this  

pong in java

This topic is 1907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[CODE]
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){}
}
}
}
[/CODE]

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.

Share this post


Link to post
Share on other sites
[quote name='dk3790' timestamp='1348172856' post='4982159']
for some reason the computer paddle AI isn't working and I'm not sure what's wrong.
[/quote]
Me neither, but it's hard to say anything when I'm not even sure what you mean by "isn't working." Can you clearly define exactly how the AI should work and how it's currently (not) working?

Share this post


Link to post
Share on other sites
[quote]
public void run()
{
while(t!=null) //animation loop
{
updateBall();
updateUserPaddle();
updateComputerPaddle()

drawToBackbuffer();
try
{
Thread.sleep(20);
}catch(InterruptedException ie){}
}
}
[/quote]

you forgot your semicolon at the end up updateComputerPaddle()

Thats probably why its not working. What IDE are you using. did it run?

Also, just something I noticed. the collision looks okay for when the ball bits the players panel. but when it hits the computer panel, it looks like it is not bouncing back until
ballx + ballWidth <= computerPaddleX + computerPaddleWidth
if you get what I mean Edited by !Null

Share this post


Link to post
Share on other sites
[quote name='!Null' timestamp='1348212137' post='4982265']
Also, just something I noticed. the collision looks okay for when the ball bits the players panel. but when it hits the computer panel, it looks like it is not bouncing back until
ballx + ballWidth <= computerPaddleX + computerPaddleWidth
if you get what I mean
[/quote]

The computer paddle collision detection isn't added yet (that I can see). The collision detection only involves play paddle variables. Although, granted, this wouldn't correlate exactly to the computer paddle.

Some notes: (trying not to give too much detail)
I would adjust your AI slightly. I think you'll see what's wrong once you fix the missing semi-colon !Null pointed out, and you see the paddle moving.

Your paddle collision detection is slightly off:
[quote name='dk3790' timestamp='1348172856' post='4982159']
// top-left of ball is to the right of top-left of paddle
[/quote]
This bit just needs a bit of thought. Edited by PyroDragn

Share this post


Link to post
Share on other sites

This topic is 1907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this