delete paddle

master
universe 2022-05-30 14:40:46 -04:00 committed by bob
parent 9d154bbbe6
commit 9f30f43361
9 changed files with 2 additions and 83 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

1
save.txt Normal file
View File

@ -0,0 +1 @@
4 2

View File

@ -1,82 +0,0 @@
/* Eric Li, ICS4U, Completed 5/29/2022
Paddle class defines behaviours for the left and right player-controlled paddles */
import java.awt.*;
import java.awt.event.KeyEvent;
public class Paddle extends GenericObject {
public final int SPEED = 5;
public static final int PADDLE_WIDTH = 8;
public static final int PADDLE_HEIGHT = 80;
public int upKey, downKey;
public Paddle(int x, int y, int upKey, int downKey) {
super(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
this.upKey = upKey;
this.downKey = downKey;
}
// this is private because it's only intended to be called from checkCollision
// this checks whether the ball has collided with the paddle in either the 'L' or the 'R' direction, and returns a boolean
private boolean hasCollided(Ball ball, char direction) {
// while this.x and this.y were not needed, they were added to prevent confusion with ball.x and ball.y
if ((Math.abs(ball.x - this.x - this.xVelocity) <= Paddle.PADDLE_WIDTH && direction == 'L' && Math.signum(ball.xVelocity) == -1.0) ||
(Math.abs(ball.x - this.x - this.xVelocity) <= Ball.BALL_DIAMETER && direction == 'R' && Math.signum(ball.xVelocity) == 1.0)) {
return (this.y - ball.y <= Ball.BALL_DIAMETER && this.y - ball.y >= 0) ||
(ball.y - this.y <= Paddle.PADDLE_HEIGHT && ball.y - this.y >= 0);
}
return false;
}
// this uses hasCollided() to check if a collision has happened; if it has, the xDirection is reversed and the speed is changed
// returns a boolean which is used for sanity checking in GamePanel.checkCollision()
public boolean checkCollision(Ball ball, char direction) {
if (hasCollided(ball, direction)) {
// reverse x direction and add a portion of paddle speed
// also adds random spray
ball.yVelocity += this.yVelocity / 4 + (int)(Math.random() * 3 - 1);
ball.xVelocity = -(ball.xVelocity) + (int)(Math.random() * 3 - 1);
return true;
}
return false;
}
// moves paddle when key is pressed
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == upKey){
setYDirection(SPEED*-1);
move();
}
if(e.getKeyCode() == downKey){
setYDirection(SPEED);
move();
}
}
// stops moving paddle when key is released
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == upKey){
setYDirection(0);
move();
}
if(e.getKeyCode() == downKey){
setYDirection(0);
move();
}
}
// calls parent then does sanity checking to ensure that the paddle does not leave the screen
public void move() {
super.move();
// collisions logic for paddles to prevent them from going into the borders
if (y + PADDLE_HEIGHT + 5 > GamePanel.GAME_HEIGHT) {
y = GamePanel.GAME_HEIGHT - PADDLE_HEIGHT - 5;
} else if (y < 5) {
y = 5;
}
}
public void draw(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
}
}