Hello, i am trying to make a pong-like game for a project in my java class, and things have been going fine so far, except… The collision checks are not working for the paddle located on the left… This is likely a problem with our collide check method, but… From looking at the method itself, it seems like it would work fine.
Here is the code so far, if anyone could offer a suggestion, that would be great
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class TestApplet extends Applet implements Runnable
{
//Paddle dimensions
int PaddleHeight = 50;
int PaddleWidth = 10;
//Other variables
boolean isStop = true;
int[] theKeys = new int[256]; //Array with one element representing each key
Random Rand = new Random();
int bounces = 0;
boolean isAI2 = false;
boolean isAI1 = false;
ArrayList <Ball> TheBalls = new ArrayList <Ball>();
ArrayList <Paddle> ThePaddles = new ArrayList <Paddle>();
//Player scores
int Score1 = 0;
int Score2 = 0;
private Image dblimage;
private Graphics dbg;
public void init()
{
//Set Background color to black
setBackground(Color.black);
}
public void start()
{
//Define new thread
Thread th = new Thread(this);
//Start the new thread
th.start();
for(int z=0; z<256; z++)
theKeys[z] = 0; //Set all keys to default position (released)
enableEvents(AWTEvent.KEY_EVENT_MASK);
ThePaddles.add(new Paddle(true, PaddleHeight, PaddleWidth, 10, this.getWidth(), this.getHeight(), 'w', 's'));
ThePaddles.add(new Paddle(false, PaddleHeight, PaddleWidth, 10, this.getWidth(), this.getHeight(), '8', '5'));
TheBalls.add(new Ball(this.getHeight(), this.getWidth()));
TheBalls.get(0).AddPaddle(ThePaddles.get(0));
TheBalls.get(0).AddPaddle(ThePaddles.get(1));
}
public void run ()
{
//Minimize thread priority
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
for(int i=0; i<ThePaddles.size(); i++)
{
if(isKeyPress(87))
{
ThePaddles.get(i).Move(1);
}
else if(isKeyPress(83))
{
ThePaddles.get(i).Move(-1);
}
}
if(isKeyPress(32))
{
for(int x=0; x < TheBalls.size(); x++)
TheBalls.get(x).Start();
isStop = false;
bounces = 0;
}
for(int i=0; i<TheBalls.size(); i++)
{
TheBalls.get(i).Move();
String scored = TheBalls.get(i).IsScore();
if(scored == "Player 1")
{
Score1++;
break;
}
else if(scored == "Player 2")
{
Score2++;
break;
}
}
repaint();
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{
// do nothing
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g)
{
//set color
g.setColor(Color.green);
for(Ball temp : TheBalls)
{
int[] tempfo = temp.BallInfo();
g.fillOval(tempfo[0], tempfo[1], tempfo[2], tempfo[3]);
}
for(Paddle temp: ThePaddles)
{
int[] PadTemp = temp.PaddleData();
g.fillRect(PadTemp[0], PadTemp[1], PadTemp[2], PadTemp[3]);
}
//paint player 1 score
g.drawString("Player 1 score: "+Score1, 10, 50);
//paint player 2 score
g.drawString("Player 2 score: "+Score2, 400, 50);
//paint current bounce count
g.drawString("Bounces: "+bounces, 250, 50);
g.drawString(""+ThePaddles.get(0).GetUp(), 100, 100);
g.drawString(""+ThePaddles.get(0).GetDown(), 100, 400);
g.drawString(""+ThePaddles.get(1).GetUp(), 200, 100);
g.drawString(""+ThePaddles.get(1).GetDown(), 200, 400);
g.drawString(""+TheBalls.get(0).Collide(), 250, 250);
g.drawLine(ThePaddles.get(0).getX(), ThePaddles.get(0).GetTop(), ThePaddles.get(0).getX()-500, ThePaddles.get(0).GetTop());
}
public void update (Graphics g)
{
// initialize buffer
if (dblimage == null)
{
dblimage = createImage (this.getSize().width, this.getSize().height);
dbg = dblimage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dblimage, 0, 0, this);
}
public void processKeyEvent(KeyEvent ev)
{
int keycode = (ev.getKeyCode()&0xff);
if (ev.getID() == KeyEvent.KEY_PRESSED)
{
theKeys[keycode] = 1; //1 Represents key is pressed
}
else if (ev.getID() == KeyEvent.KEY_RELEASED)
{
theKeys[keycode] = 0; //0 Represents key is not pressed
}
repaint();
}
//Checks if a given key is pressed
public boolean isKeyPress(int theKey)
{
return (theKeys[theKey] != 0);
}
//Checks if a given key is released
public boolean isKeyRel(int theKey)
{
return(theKeys[theKey] == 0);
}
}