|
|
@ -4,7 +4,6 @@ import javax.swing.*; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.event.KeyAdapter; |
|
|
|
import java.awt.event.KeyEvent; |
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
|
|
|
|
public class GamePanel extends JPanel implements Runnable { |
|
|
@ -22,16 +21,105 @@ public class GamePanel extends JPanel implements Runnable { |
|
|
|
Score score; |
|
|
|
Image image; |
|
|
|
Graphics graphics; |
|
|
|
CollisionChecker collisionChecker; |
|
|
|
Thread gameThread; |
|
|
|
|
|
|
|
|
|
|
|
public GamePanel() { |
|
|
|
newPaddle(); |
|
|
|
newBall(); |
|
|
|
score = new Score(FRAME_WIDTH, FRAME_HEIGHT); |
|
|
|
collisionChecker = new CollisionChecker(); |
|
|
|
this.setFocusable(true); |
|
|
|
this.addKeyListener(new AL()); |
|
|
|
this.setPreferredSize(SCREEN); |
|
|
|
|
|
|
|
gameThread = new Thread(this); |
|
|
|
gameThread.start(); |
|
|
|
} |
|
|
|
|
|
|
|
public void checkCollision() { |
|
|
|
//Reflection of Ball when it touches top and bottom edges! |
|
|
|
if(collisionChecker.didTouchTopOrBottomEdge(ball.y, FRAME_HEIGHT-BALL_DIAMETER)) { |
|
|
|
ball.setYDirection(-ball.yVelocity); |
|
|
|
} |
|
|
|
if(collisionChecker.didTouchPaddle(ball, new Rectangle(paddle_1.x, paddle_1.y1, paddle_1.width, paddle_1.height))) { |
|
|
|
ball.xVelocity = Math.abs(ball.xVelocity); |
|
|
|
ball.xVelocity++; |
|
|
|
if (ball.yVelocity > 0) |
|
|
|
ball.yVelocity++; |
|
|
|
else |
|
|
|
ball.yVelocity--; |
|
|
|
ball.setXDirection(ball.xVelocity); |
|
|
|
ball.setYDirection(ball.yVelocity); |
|
|
|
} |
|
|
|
if(collisionChecker.didTouchPaddle(ball, new Rectangle(paddle_2.x, paddle_2.y2, paddle_2.width, paddle_2.height))) { |
|
|
|
ball.xVelocity = Math.abs(ball.xVelocity); |
|
|
|
ball.xVelocity++; |
|
|
|
if (ball.yVelocity > 0) |
|
|
|
ball.yVelocity++; |
|
|
|
else |
|
|
|
ball.yVelocity--; |
|
|
|
ball.setXDirection(-ball.xVelocity); |
|
|
|
ball.setYDirection(ball.yVelocity); |
|
|
|
} |
|
|
|
if (paddle_1.y1 <= 0) |
|
|
|
paddle_1.y1 = 0; |
|
|
|
if (paddle_1.y1 >= (FRAME_HEIGHT-PADDLE_HEIGHT)) |
|
|
|
paddle_1.y1 = FRAME_HEIGHT-PADDLE_HEIGHT; |
|
|
|
if (paddle_2.y2 <= 0) |
|
|
|
paddle_2.y2 = 0; |
|
|
|
if (paddle_2.y2 >= (FRAME_HEIGHT-PADDLE_HEIGHT)) |
|
|
|
paddle_2.y2 = FRAME_HEIGHT-PADDLE_HEIGHT; |
|
|
|
|
|
|
|
if(collisionChecker.didTouchLeftEdge(ball.x)) { |
|
|
|
score.player_2++; |
|
|
|
newPaddle(); |
|
|
|
newBall(); |
|
|
|
|
|
|
|
} |
|
|
|
if(collisionChecker.didTouchRightEdge(ball.x, FRAME_WIDTH-BALL_DIAMETER)) { |
|
|
|
score.player_1++; |
|
|
|
newPaddle(); |
|
|
|
newBall(); |
|
|
|
} |
|
|
|
if(score.player_1 == 20){ |
|
|
|
score.player_1 = 0; |
|
|
|
score.player_2 = 0; |
|
|
|
newPaddle(); |
|
|
|
newBall(); |
|
|
|
} |
|
|
|
if(score.player_2 == 20) { |
|
|
|
score.player_1 = 0; |
|
|
|
score.player_2 = 0; |
|
|
|
newPaddle(); |
|
|
|
newBall(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void run() { |
|
|
|
long lastTime = System.nanoTime(); |
|
|
|
double amountOfTicks = 60.0; |
|
|
|
double nanoseconds = 1000000000 / amountOfTicks; |
|
|
|
double delta = 0; |
|
|
|
while (true) { |
|
|
|
long now = System.nanoTime(); |
|
|
|
delta += (now - lastTime) / nanoseconds; |
|
|
|
lastTime = now; |
|
|
|
if (delta >= 1) { |
|
|
|
move(); |
|
|
|
checkCollision(); |
|
|
|
repaint(); |
|
|
|
delta--; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void draw(Graphics g) { |
|
|
@ -41,8 +129,9 @@ public class GamePanel extends JPanel implements Runnable { |
|
|
|
ball.draw(g); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void move() { |
|
|
|
|
|
|
|
|
|
|
|
paddle_1.move(); |
|
|
|
paddle_2.move(); |
|
|
|
ball.move(); |
|
|
@ -59,7 +148,10 @@ public class GamePanel extends JPanel implements Runnable { |
|
|
|
|
|
|
|
ball = new Ball((FRAME_WIDTH/2)-(BALL_DIAMETER/2), ((FRAME_HEIGHT/2)-(BALL_DIAMETER/2)), BALL_DIAMETER, BALL_DIAMETER); |
|
|
|
} |
|
|
|
|
|
|
|
public void newPaddle() { |
|
|
|
paddle_1 = new Paddle(0, (FRAME_HEIGHT/2)-(PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT, 1 ); |
|
|
|
paddle_2 = new Paddle(FRAME_WIDTH-PADDLE_WIDTH, (FRAME_HEIGHT/2)-(PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT, 2 ); |
|
|
|
} |
|
|
|
public class AL extends KeyAdapter { |
|
|
|
|
|
|
|
public void keyPressed(KeyEvent e) { |
|
|
|