Objektorientierte Programmierung WS 2013/2014 - Datei Katze.java
import java.awt.*;
class Katze implements SpielRolle
{
private static final int SPEED = 1;
private SpielBall ich;
private SpielBall dieMaus;
public Katze (SpielBall ichSelbst, SpielBall dieMausSelbst)
{
ich = ichSelbst;
dieMaus = dieMausSelbst;
}
public void moveSpielBall ()
{ // Neuberechnung meiner Bewegungsrichtung
int xDistanz = ich.x() - dieMaus.x();
int yDistanz = ich.y() - dieMaus.y();
if (Math.abs(xDistanz) > Math.abs(yDistanz))
// laufe horizontal
if (xDistanz > 0) // Maus ist links
ich.setMotion(-SPEED, 0);
else // Maus ist rechts
ich.setMotion(SPEED, 0);
else // laufe vertikal
if (yDistanz > 0) // Maus ist oben
ich.setMotion(0, -SPEED);
else // Maus ist unten
ich.setMotion(0, SPEED);
if (ich.blocked()) ich.setMotion(0, 0);
ich.move();
// hab ich die Maus?
if (ich.region().intersects(dieMaus.region()))
{
dieMaus.setMotion(0, 0);
// schmatzendes Geraeusch, naja fast
Toolkit.getDefaultToolkit().beep();
}
}
public void paintSpielBall (Graphics g)
{
ich.paint(g);
g.setColor(Color.black);
g.drawString("K", ich.x() - 4, ich.y() - 6);
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 26.11.2013


