Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package testGame;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- class Paddle extends Rectangle {
- //int x, y, width, height; //WARNING: cannot redefine these Rectangle variables.
- boolean upAccel = false;
- boolean downAccel = false;
- //all doubles must have a decimal when declared. This makes it obvious to others that it is a double
- double yVel = 0.0;
- static final double MOVESPEED = 2.0;
- //final double GRAVITY = 0.98; //This is NOT gravity. It's an acceleation parameter. Your balls don't fall to the ground
- final double DAMPING = 0.98; //So we'll call it damping.
- Paddle(char c) {
- this.width = 35;
- this.height = 90;
- this.x = 100; //fake number
- this.y = Game.SCREEN_HEIGHT / 2 - (height / 2);
- if (c == 'L') this.x = 10;
- if (c == 'R') this.x = Game.SCREEN_WIDTH - 10 - this.width;
- }
- public void move() {
- if (upAccel) {
- yVel -= MOVESPEED;
- } else if (downAccel) {
- yVel += MOVESPEED;
- } else if (!upAccel && !downAccel) {
- if((int)yVel != 0) {
- yVel *= DAMPING;
- }
- }
- if (yVel > 5) {
- yVel = 5;
- }
- if (yVel < -5) { //replaced ElseIf with If
- yVel = -5;
- }
- y += (int)yVel;
- checkWallCollision();
- }
- //There's a bunch of stuff that no other class ever needs to see, so make it private instead of public or default.
- //Rename this so that it's clear that we're only checking wall collisions here
- private void checkWallCollision(){
- if(y <= 0) {
- y = 0;
- yVel = 0;
- return;
- }
- if(y + height >= Game.SCREEN_HEIGHT) {
- y = Game.SCREEN_HEIGHT - height;
- yVel = 0;
- }
- }
- void draw(Graphics g) {
- g.setColor(Color.white);
- g.fillRect(x, y, width, height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement