Select Git revision
build.gradle
Car.java 1.43 KiB
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
);
}
}