FlappyTurtle/Rectangle.java
2020-12-31 12:29:08 +01:00

90 lines
1.9 KiB
Java

class Rectangle implements Drawable {
private double _x, _y;
private double _width, _height;
private String _color;
private boolean _fill;
public Rectangle(double x, double y, double w, double h, String c, boolean fill)
{
_x = x;
_y = y;
_width = w;
_height = h;
_color = c;
_fill = fill;
}
public void drawRect(Turtle t)
{
t.hinterlasseKeineSpur();
t.geheZu((double)_x, (double)_y);
t.hinterlasseSpur();
t.setzeFarbe(_color);
if (_fill)
{
for (int i = 0; i < _height - 1; ++i)
{
t.geheVor(_width - 1);
boolean right = t.gibRichtung() == 0;
t.setzeRichtung(t.gibRichtung() + (right ? 90 : -90));
t.geheVor(1);
t.setzeRichtung(t.gibRichtung() + (right ? 90 : -90));
}
t.geheVor(_width - 1);
}
else
{
for (int i = 0; i < 2; ++i)
{
t.geheVor(_width - 1);
t.setzeRichtung(t.gibRichtung() + 90);
t.geheVor(_height - 1);
t.setzeRichtung(t.gibRichtung() + 90);
}
}
t.setzeRichtung(0);
}
public void draw(Turtle t, double elapsed)
{
drawRect(t);
}
public boolean intersects(Rectangle r)
{
return _x + _width >= r.getX()
&&_x < r.getX() + r.getWidth()
&& _y + _height >= r.getY()
&& _y < r.getY() + r.getHeight();
}
public double getWidth()
{
return _width;
}
public double getHeight()
{
return _height;
}
public double getX()
{
return _x;
}
public double getY()
{
return _y;
}
public void setPos(double x, double y)
{
_x = x;
_y = y;
}
}