diff --git a/Car.java b/Car.java new file mode 100644 index 0000000000000000000000000000000000000000..240c87688b9f9c52100493bb7bdd9cb8e99c7781 --- /dev/null +++ b/Car.java @@ -0,0 +1,54 @@ +import java.awt.*; + +public class Car { + private int x, y, speed; + private static final int WIDTH = 80; + private static final int HEIGHT = 40; + private Color color; + + public Car(int x, int y, int speed) { + this.x = x; + this.y = y; + this.speed = speed; + this.color = getRandomColor(); + } + + public void move() { + x += speed; + } + + public boolean checkCollision(int bunnyX, int bunnyY, int bunnySize) { + return x < bunnyX + bunnySize && + x + WIDTH > bunnyX && + y < bunnyY + bunnySize && + y + HEIGHT > bunnyY; + } + + public int getX() { return x; } + public void setX(int x) { this.x = x; } + public int getY() { return y; } + public int getSpeed() { return speed; } + + public void increaseSpeed() { + speed += speed > 0 ? 1 : -1; + } + + public void draw(Graphics g) { + g.setColor(color); + g.fillRect(x, y, WIDTH, HEIGHT); + g.setColor(Color.BLACK); + g.drawRect(x, y, WIDTH, HEIGHT); + + // Draw wheels + g.fillOval(x + 10, y + HEIGHT - 10, 20, 20); + g.fillOval(x + WIDTH - 30, y + HEIGHT - 10, 20, 20); + } + + private Color getRandomColor() { + return new Color( + (int) (Math.random() * 200) + 55, + (int) (Math.random() * 200) + 55, + (int) (Math.random() * 200) + 55 + ); + } +}