|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import java.util.*; |
| 4 | +import javax.swing.*; |
| 5 | + |
| 6 | +public class GamePanel extends JPanel implements Runnable{ |
| 7 | + |
| 8 | + static final int GAME_WIDTH = 1000; |
| 9 | + static final int GAME_HEIGHT = (int)(GAME_WIDTH * (0.5555)); |
| 10 | + static final Dimension SCREEN_SIZE = new Dimension(GAME_WIDTH,GAME_HEIGHT); |
| 11 | + static final int BALL_DIAMETER = 20; |
| 12 | + static final int PADDLE_WIDTH = 25; |
| 13 | + static final int PADDLE_HEIGHT = 100; |
| 14 | + Thread gameThread; |
| 15 | + Image image; |
| 16 | + Graphics graphics; |
| 17 | + Random random; |
| 18 | + Paddle paddle1; |
| 19 | + Paddle paddle2; |
| 20 | + Ball ball; |
| 21 | + Score score; |
| 22 | + |
| 23 | + GamePanel(){ |
| 24 | + newPaddles(); |
| 25 | + newBall(); |
| 26 | + score = new Score(GAME_WIDTH,GAME_HEIGHT); |
| 27 | + this.setFocusable(true); |
| 28 | + this.addKeyListener(new AL()); |
| 29 | + this.setPreferredSize(SCREEN_SIZE); |
| 30 | + |
| 31 | + gameThread = new Thread(this); |
| 32 | + gameThread.start(); |
| 33 | + } |
| 34 | + |
| 35 | + public void newBall() { |
| 36 | + random = new Random(); |
| 37 | + ball = new Ball((GAME_WIDTH/2)-(BALL_DIAMETER/2),random.nextInt(GAME_HEIGHT-BALL_DIAMETER),BALL_DIAMETER,BALL_DIAMETER); |
| 38 | + } |
| 39 | + public void newPaddles() { |
| 40 | + paddle1 = new Paddle(0,(GAME_HEIGHT/2)-(PADDLE_HEIGHT/2),PADDLE_WIDTH,PADDLE_HEIGHT,1); |
| 41 | + paddle2 = new Paddle(GAME_WIDTH-PADDLE_WIDTH,(GAME_HEIGHT/2)-(PADDLE_HEIGHT/2),PADDLE_WIDTH,PADDLE_HEIGHT,2); |
| 42 | + } |
| 43 | + public void paint(Graphics g) { |
| 44 | + image = createImage(getWidth(),getHeight()); |
| 45 | + graphics = image.getGraphics(); |
| 46 | + draw(graphics); |
| 47 | + g.drawImage(image,0,0,this); |
| 48 | + } |
| 49 | + public void draw(Graphics g) { |
| 50 | + paddle1.draw(g); |
| 51 | + paddle2.draw(g); |
| 52 | + ball.draw(g); |
| 53 | + score.draw(g); |
| 54 | +Toolkit.getDefaultToolkit().sync(); // I forgot to add this line of code in the video, it helps with the animation |
| 55 | + |
| 56 | + } |
| 57 | + public void move() { |
| 58 | + paddle1.move(); |
| 59 | + paddle2.move(); |
| 60 | + ball.move(); |
| 61 | + } |
| 62 | + public void checkCollision() { |
| 63 | + |
| 64 | + //bounce ball off top & bottom window edges |
| 65 | + if(ball.y <=0) { |
| 66 | + ball.setYDirection(-ball.yVelocity); |
| 67 | + } |
| 68 | + if(ball.y >= GAME_HEIGHT-BALL_DIAMETER) { |
| 69 | + ball.setYDirection(-ball.yVelocity); |
| 70 | + } |
| 71 | + //bounce ball off paddles |
| 72 | + if(ball.intersects(paddle1)) { |
| 73 | + ball.xVelocity = Math.abs(ball.xVelocity); |
| 74 | + ball.xVelocity++; //optional for more difficulty |
| 75 | + if(ball.yVelocity>0) |
| 76 | + ball.yVelocity++; //optional for more difficulty |
| 77 | + else |
| 78 | + ball.yVelocity--; |
| 79 | + ball.setXDirection(ball.xVelocity); |
| 80 | + ball.setYDirection(ball.yVelocity); |
| 81 | + } |
| 82 | + if(ball.intersects(paddle2)) { |
| 83 | + ball.xVelocity = Math.abs(ball.xVelocity); |
| 84 | + ball.xVelocity++; //optional for more difficulty |
| 85 | + if(ball.yVelocity>0) |
| 86 | + ball.yVelocity++; //optional for more difficulty |
| 87 | + else |
| 88 | + ball.yVelocity--; |
| 89 | + ball.setXDirection(-ball.xVelocity); |
| 90 | + ball.setYDirection(ball.yVelocity); |
| 91 | + } |
| 92 | + //stops paddles at window edges |
| 93 | + if(paddle1.y<=0) |
| 94 | + paddle1.y=0; |
| 95 | + if(paddle1.y >= (GAME_HEIGHT-PADDLE_HEIGHT)) |
| 96 | + paddle1.y = GAME_HEIGHT-PADDLE_HEIGHT; |
| 97 | + if(paddle2.y<=0) |
| 98 | + paddle2.y=0; |
| 99 | + if(paddle2.y >= (GAME_HEIGHT-PADDLE_HEIGHT)) |
| 100 | + paddle2.y = GAME_HEIGHT-PADDLE_HEIGHT; |
| 101 | + //give a player 1 point and creates new paddles & ball |
| 102 | + if(ball.x <=0) { |
| 103 | + score.player2++; |
| 104 | + newPaddles(); |
| 105 | + newBall(); |
| 106 | + System.out.println("Player 2: "+score.player2); |
| 107 | + } |
| 108 | + if(ball.x >= GAME_WIDTH-BALL_DIAMETER) { |
| 109 | + score.player1++; |
| 110 | + newPaddles(); |
| 111 | + newBall(); |
| 112 | + System.out.println("Player 1: "+score.player1); |
| 113 | + } |
| 114 | + } |
| 115 | + public void run() { |
| 116 | + //game loop |
| 117 | + long lastTime = System.nanoTime(); |
| 118 | + double amountOfTicks =60.0; |
| 119 | + double ns = 1000000000 / amountOfTicks; |
| 120 | + double delta = 0; |
| 121 | + while(true) { |
| 122 | + long now = System.nanoTime(); |
| 123 | + delta += (now -lastTime)/ns; |
| 124 | + lastTime = now; |
| 125 | + if(delta >=1) { |
| 126 | + move(); |
| 127 | + checkCollision(); |
| 128 | + repaint(); |
| 129 | + delta--; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + public class AL extends KeyAdapter{ |
| 134 | + public void keyPressed(KeyEvent e) { |
| 135 | + paddle1.keyPressed(e); |
| 136 | + paddle2.keyPressed(e); |
| 137 | + } |
| 138 | + public void keyReleased(KeyEvent e) { |
| 139 | + paddle1.keyReleased(e); |
| 140 | + paddle2.keyReleased(e); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments