Skip to content
Snippets Groups Projects
Commit 6d0d2765 authored by Simon Peter Marneros's avatar Simon Peter Marneros
Browse files

car.java

parent 88adc7c0
No related branches found
No related tags found
No related merge requests found
Car.java 0 → 100644
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
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment