Files
ping-pong/src/Paddle.java
T
2026-10-07 13:34:50 +00:00

74 lines
1.5 KiB
Java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Paddle {
private final int x;
private int y;
private final int width;
private final int height;
private final int speed;
private boolean up;
private boolean down;
public Paddle(int x, int y) {
this.x = x;
this.y = y;
this.width = GameConfig.PADDLE_WIDTH;
this.height = GameConfig.PADDLE_HEIGHT;
this.speed = GameConfig.PADDLE_SPEED;
}
public void move() {
if (up && y > 0) {
y -= speed;
}
if (down && y + height < GameConfig.HEIGHT) {
y += speed;
}
}
public void setYByCenter(int centerY) {
int newY = centerY - height / 2;
if (newY < 0) {
newY = 0;
} else if (newY + height > GameConfig.HEIGHT) {
newY = GameConfig.HEIGHT - height;
}
this.y = newY;
}
public void draw(Graphics g, Color color) {
g.setColor(color);
g.fillRoundRect(x, y, width, height, 8, 8);
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public void setUp(boolean up) {
this.up = up;
}
public void setDown(boolean down) {
this.down = down;
}
}