Here is the code for this method plus the rest of the class for reference:
package TheGame;
import java.util.Random;
import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.*;
public class Methods {
Random rand = new Random();
static int oppX = 490;
static int oppY = 0;
static int oppW = 50;
static int oppH = 360;
static float ballX = 455;
static float ballY = 0;
static float ballW = 25;
static float ballH = 25;
static int vx; //x velocity
static int vy; //y velocity
static int py; //top of paddle
static int opy = py + 55; //bottom of paddle
static int px = 20; //right side of paddle
static int opx = 55; //left side of paddle
public void drawField(Graphics g){ //draw background and center line
g.setBackground(Color.black);
g.setColor(Color.yellow);
g.fillRect(307, 0, 26, 360);
}
public void drawOpposition(Graphics g){ //draw wall
g.setColor(Color.cyan);
g.fillRect(oppX, oppY, oppW, oppH);
}
public void drawBall(Graphics g){ //draw ball
g.setColor(Color.red);
g.fillRect(ballX, ballY, ballW, ballH);
}
public void drawPlayer(Graphics g){ //draw player paddle
g.setColor(Color.cyan);
g.fillRect(px, py, 25, 50);
}
public void setVelocitys(){
if(ballX > px && ballX < opx && (ballY-12) > (py-5) && (ballY-12) < opy ){ //collides with paddle
vx = rand.nextInt(5);
}else if(ballX >= 455){//exit off right side
vx = -(rand.nextInt(5));
}else if(ballX < 0){ // exit off left side
ballX = 455;
}
if(ballY <= 0){ //exit off top
vy = rand.nextInt(5);
}else if(ballY >= 335){ //exit off bottom
vy = -(rand.nextInt(5));
}
}
public void moveBall(GameContainer gc){ //move the ball
setVelocitys();
ballX += vx;
ballY += vy;
checkPlayer(gc);
}
public void checkPlayer(GameContainer gc){ //check player movement
Input input = gc.getInput();
if(input.isKeyDown(Input.KEY_DOWN)){
py = py + 5;
}
if(input.isKeyDown(Input.KEY_UP)){
py = py - 5;
}
if(py < 0){
py = 0;
}
if(py > 310){
py = 310;
}
}
}
(Also, I am using the slick and lwjgl libraries.)
Thanks for any suggestions
Here is where the problem is I believe.
if(ballX > px && ballX < opx && (ballY-12) > (py-5) && (ballY-12) < opy ){ //collides with paddle
This post has been edited by Benzoate: 19 June 2012 - 07:33 PM

New Topic/Question
Reply



MultiQuote



|