May 7th, 2021
584
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. package testGame;
2.
3. import java.awt.Color;
4. import java.awt.Graphics;
5. import java.awt.Rectangle;
6.
7. class Paddle extends Rectangle {
8.     //int x, y, width, height;  //WARNING: cannot redefine these Rectangle variables.
9.
10.     boolean upAccel = false;
11.     boolean downAccel = false;
12.     //all doubles must have a decimal when declared. This makes it obvious to others that it is a double
13.     double yVel = 0.0;
14.     static final double MOVESPEED = 2.0;
15.
16.     //final double GRAVITY = 0.98; //This is NOT gravity. It's an acceleation parameter. Your balls don't fall to the ground
17.     final double DAMPING = 0.98; //So we'll call it damping.
18.
20.         this.width = 35;
21.         this.height = 90;
22.         this.x = 100;  //fake number
23.         this.y = Game.SCREEN_HEIGHT / 2 - (height / 2);
24.         if (c == 'L') this.x = 10;
25.         if (c == 'R') this.x = Game.SCREEN_WIDTH - 10 - this.width;
26.
27.     }
28.
29.
30.     public void move() {
31.         if (upAccel) {
32.             yVel -= MOVESPEED;
33.         } else if (downAccel) {
34.             yVel += MOVESPEED;
35.         } else if (!upAccel && !downAccel) {
36.             if((int)yVel != 0) {
37.                 yVel *= DAMPING;
38.             }
39.         }
40.
41.         if (yVel > 5) {
42.             yVel = 5;
43.         }
44.         if (yVel < -5) {    //replaced ElseIf with If
45.             yVel = -5;
46.         }
47.
48.         y += (int)yVel;
49.
50.         checkWallCollision();
51.     }
52.
53.     //There's a bunch of stuff that no other class ever needs to see, so make it private instead of public or default.
54.     //Rename this so that it's clear that we're only checking wall collisions here
55.     private void checkWallCollision(){
56.         if(y <= 0) {
57.             y = 0;
58.             yVel = 0;
59.             return;
60.         }
61.
62.         if(y + height >= Game.SCREEN_HEIGHT) {
63.             y = Game.SCREEN_HEIGHT - height;
64.             yVel = 0;
65.         }
66.     }
67.
68.     void draw(Graphics g) {
69.         g.setColor(Color.white);
70.         g.fillRect(x, y, width, height);
71.
72.     }
73. }
74.
75.
76.
RAW Paste Data