Question: can someone help me write comments on this code, it's a code for a Paddle.java class for a ping pong game: /** * @authors: Hunter
can someone help me write comments on this code, it's a code for a Paddle.java class for a ping pong game:
/** * @authors: Hunter Lai, Isaac Fehr, Raghad Marta * @about: Class that creates a paddle and accounts for it's moving activity */
import java.awt.event.KeyListener; import objectdraw.DrawingCanvas; import objectdraw.FilledRect;
public class Paddle extends Thread implements KeyListener { private final double START_HEIGHT = .15; private final double START_WIDTH = .01; private final double INCREASE_FACTOR = .2; private final double DECREASE_FACTOR = .2; private final double MAX_HEIGHT_FACT = .5; private final double MIN_HEIGHT_FACT = .1; private int PADDLE_HEIGHT, PADDLE_WIDTH; private final double PADDLE_WALL_PADDING = 0.05; private int side; private int x, y; public FilledRect paddle; private DrawingCanvas canvas; private char upKey; private char downKey; private int dir; private final double SPEED = 1; public Paddle(char side, DrawingCanvas canvas, char up, char down){ PADDLE_WIDTH = (int) ((double)canvas.getWidth() * START_WIDTH); PADDLE_HEIGHT = (int)((double)canvas.getHeight()* START_HEIGHT); y = canvas.getHeight() / 2; this.side = side; if(side == 'l'){ x = (int)(PADDLE_WALL_PADDING * canvas.getWidth()); } else{ x = canvas.getWidth() - PADDLE_WIDTH - (int)(PADDLE_WALL_PADDING * canvas.getWidth()); } paddle = new FilledRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT, canvas); this.canvas = canvas; this.canvas.addKeyListener(this); this.canvas.requestFocus(); upKey = up; downKey = down; dir = 0; } public void increase() { int newHeight = (int) (PADDLE_HEIGHT * (1 + INCREASE_FACTOR)); if(newHeight < (int)(canvas.getHeight() * MAX_HEIGHT_FACT)) { PADDLE_HEIGHT = newHeight; paddle.setHeight(PADDLE_HEIGHT); } } public void decrease() { int newHeight = (int) (PADDLE_HEIGHT * (1 - DECREASE_FACTOR)); if(newHeight > (int)(canvas.getHeight() * MIN_HEIGHT_FACT)) { PADDLE_HEIGHT = newHeight; paddle.setHeight(PADDLE_HEIGHT); } } public void vibrate() { for(int i = 0; i< 5; i++) { paddle.move(5,5); try { this.sleep(100); } catch(Exception e) { e.printStackTrace(); } paddle.move(-5, -5); } } public void run() { while(true){ paddle.move(0, dir * (SPEED * (double)canvas.getHeight()) * ((double)10 / 1000.0)); if(paddle.getY() < 0){ paddle.move(0, paddle.getY() * -1); } else if(paddle.getY() + PADDLE_HEIGHT > canvas.getHeight()){ paddle.move(0, canvas.getHeight() - (paddle.getY() + paddle.getHeight())); } if(side == 'l'){ x = (int)(PADDLE_WALL_PADDING * canvas.getWidth()); } else{ x = canvas.getWidth() - PADDLE_WIDTH - (int)(PADDLE_WALL_PADDING * canvas.getWidth()); } paddle.moveTo(x, paddle.getY()); TrippyPong.catchSleep(10); } }
@Override public void keyPressed(java.awt.event.KeyEvent e) { char key = e.getKeyChar(); if(key == upKey){ dir = -1; } else if(key == downKey){ dir = 1; } }
@Override public void keyReleased(java.awt.event.KeyEvent e) { char key = e.getKeyChar(); if(key == upKey && dir == -1){ dir = -0; } else if(key == downKey && dir == 1){ dir = 0; } }
@Override public void keyTyped(java.awt.event.KeyEvent e) { }
public int getX() { return x; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
