...

View Full Version : Help with a bug in pong



pineappelle
03-17-2012, 06:11 PM
The code below makes pong. Theres only one big bug in it and that's when the ball hits the top or the bottom of the paddles it goes through it. I don't know how to fix this. Anyone know how?


/**
* Pong!
*
* pong Applet application
*
* @Ian Peacock
* @version 1.2 March 8th, 2012
*/

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class pong extends java.applet.Applet implements KeyListener, Runnable {

Graphics screen, offscreen;
Rectangle ball, paddle;
Thread runner;
int y, x, character_int, y2, x2, ballx, bally, xmove, ymove, pointleft, pointright, paddlemoveR, paddlemoveL;
Image workspace;
boolean p1win, p2win, cheat, startscreen, paused, touchTr, touchTl, touchBl, touchBr, Lmove, Rmove;

public void init() {

requestFocus();
addKeyListener(this);
paddle=new Rectangle();
paddle.x=5;
paddle.y=150;
paddle.width=20;
paddle.height=100;
x2=575;
y2=150;
workspace=createImage(600,400);
offscreen=workspace.getGraphics();
ball=new Rectangle();
ball.x=300;
ball.y=200;
ball.width=20;
ball.height=20;
xmove=3;
ymove=3;
pointleft=0;
pointright=0;
startscreen=true;
paddlemoveR=10;
paddlemoveL=10;
Lmove=false;
Rmove=false;

if (Rmove==true) {
paddlemoveR=10;
paddlemoveL=0;
}
if(Lmove==true) {
paddlemoveL=10;
paddlemoveR=0;
}

}

public void keyPressed(KeyEvent event) {

character_int=event.getKeyCode();

if (character_int==83) {
Lmove=true;
Rmove=false;
paddle.y=paddle.y+paddlemoveL;
if (touchTl==true) {
paddle.y=1;
paddlemoveL=10;
touchTl=false;
}
repaint();
}
else if(character_int==87) {
Lmove=true;
Rmove=false;
paddle.y=paddle.y-paddlemoveL;
if (touchBl==true) {
paddle.y=299;
paddlemoveL=10;
touchBl=false;
}
repaint();
}
else if(character_int==40) {
Rmove=true;
Lmove=false;
y2=y2+paddlemoveR;
if(touchTr==true) {
y2=1;
paddlemoveR=10;
touchTr=false;
}
repaint();
}
else if(character_int==38) {
Rmove=true;
Lmove=false;
y2=y2-paddlemoveR;
if (touchBr==true) {
y2=299;
paddlemoveR=10;
touchBr=false;
}
repaint();
}
else if(character_int==81) {
xmove=10;
}
else if(character_int==65) {
xmove=3;
}
else if(character_int==32) {
paddle.y=0;
paddle.height=400;
cheat=true;
}
else if(character_int==192) {
paddle.y=150;
paddle.height=100;
cheat=false;
}
else if(character_int==10) {

if(p1win=true) {
xmove=-3;
}
else if(p2win=true) {
xmove=3;
}
else {
xmove=3;
}

p1win=false;
p2win=false;
ball.x=300;
ball.y=200;
ymove=3;
pointleft=0;
pointright=0;
paddle.y=150;
y2=150;
paddle.height=100;
cheat=false;
repaint();
}
else if(character_int==49) {
startscreen=false;
ball.x=300;
ball.y=200;
pointright=0;
pointleft=0;
xmove=3;
ymove=3;
}
else if(character_int==80) {
xmove=0;
paused=true;
}
}

public void keyReleased(KeyEvent event) {
}

public void keyTyped(KeyEvent event) {
}

public void paint(Graphics screen) {

offscreen.setColor(Color.white);
offscreen.fillRect(0,0,600,400);
offscreen.setColor(Color.black);
offscreen.fillOval(ball.x,ball.y,ball.width,ball.height);
offscreen.fillRect(paddle.x,paddle.y,paddle.width,paddle.height);
offscreen.fillRect(x2,y2,20,100);
offscreen.drawLine(300,0,300,400);
offscreen.drawRect(0,0,599,399);
offscreen.drawString(""+pointleft,280,15);
offscreen.drawString(""+pointright,320,15);

if(p1win==true) {
offscreen.fillRect(0,0,600,400);
offscreen.setColor(Color.white);
offscreen.drawString("Player 1 wins",250,200);
offscreen.drawString("Hit enter to try again",230,220);
}

if(p2win==true) {
offscreen.fillRect(0,0,600,400);
offscreen.setColor(Color.white);
offscreen.drawString("Player 2 wins",250,200);
offscreen.drawString("Hit enter to try again",230,220);
}

if (startscreen==true) {
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,600,400);
offscreen.setColor(Color.white);
offscreen.drawString("This is Pong",250,200);
offscreen.drawString("Press 1 to start",243,220);
}

screen.drawImage(workspace,0,0,600,400,this);
}

public void start() {
if(runner==null) {
runner=new Thread(this);
runner.start();
}
}

public void run() {

while(true) {
repaint();;
checkbounce();
checkwin();
checktouching();
ball.x=ball.x+xmove;
ball.y=ball.y+ymove;
try {Thread.sleep(20);}
catch(InterruptedException e) {}
}
}

public void checkbounce() {

if(cheat==false) {

if(ball.x<0) {
xmove=xmove*-1;
pointright=pointright+1;
}
if(ball.x+20>600) {
xmove=xmove*-1;
pointleft=pointleft+1;
}
if(ball.y<0||ball.y+20>400) {
ymove=ymove*-1;
}
if(ball.x<25&&ball.y>paddle.y&&ball.y<paddle.y+100) {
xmove=xmove*-1;
}
if(ball.x>560&&ball.y>y2&&ball.y<y2+100) {
xmove=xmove*-1;
}
if(ball.x>575&&ball.y<y2+100&&ball.y>y2) {
ymove=ymove*-1;
}

}
else if(cheat==true){
if(ball.x<0) {
xmove=xmove*-1;
pointright=pointright+1;
}
if(ball.x+20>600) {
xmove=xmove*-1;
pointleft=pointleft+1;
}
if(ball.y<0||ball.y+20>400) {
ymove=ymove*-1;
}
if(ball.x<25&&ball.y>paddle.y-400&&ball.y<paddle.y+400) {
xmove=xmove*-1;
}
if(ball.x>560&&ball.y>y2&&ball.y<y2+100) {
xmove=xmove*-1;
}
}
}

public void checkwin() {
if(pointright==5) {
p2win=true;
}
if(pointleft==5) {
p1win=true;
}
}

public void checktouching() {

if (paddle.y<0) {
paddlemoveL=0;
touchTl=true;
}
if (y2<0) {
paddlemoveR=0;
touchTr=true;
}
if (paddle.y>300) {
paddlemoveL=0;
touchBl=true;
}
if (y2>300) {
paddlemoveR=0;
touchBr=true;
}

}

public void stop() {
if(runner!=null) {
runner.stop();
runner=null;
}
}

public void update(Graphics screen) {
paint(screen);
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum