pong in java

Started by
2 comments, last by PyroDragn 11 years, 6 months ago

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.
Advertisement

for some reason the computer paddle AI isn't working and I'm not sure what's wrong.

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?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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
/********************************************************************************\
/**********************He Who Dares, Wins**********************************\
/********************************************************************************\

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


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:

// top-left of ball is to the right of top-left of paddle

This bit just needs a bit of thought.

This topic is closed to new replies.

Advertisement