Objektorientierte Programmierung WS 2013/2014 - Datei KatzUndMaus.java
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Enumeration;
public class KatzUndMaus extends Frame
{
public static final int FRAMEWIDTH = 600;
public static final int FRAMEHEIGHT = 400;
public static final int SPEED = 5;
public static final int RADIUS = 8;
// die beiden Baelle
private SpielBall ball1;
private SpielBall ball2;
// Für das DoubleBuffering
private Graphics bufferGraphics;
private Image buffer;
private void bufferInit ()
{ // wir zeichnen in einen Puffer
buffer = this.createImage(FRAMEWIDTH, FRAMEHEIGHT);
bufferGraphics = buffer.getGraphics();
}
public static void main (String[] args)
{
new KatzUndMaus().setVisible(true);
}
public KatzUndMaus ()
{
setTitle("Katz und Maus");
setSize(FRAMEWIDTH, FRAMEHEIGHT);
setBackground(Color.white);
WindowListener wl = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wl);
ball1 = new SpielBall(20, FRAMEHEIGHT - 20, RADIUS, 0, 0, FRAMEWIDTH,
FRAMEHEIGHT);
ball1.setRolle(new Maus(ball1, this));
ball1.setColor(Color.blue);
ball2 = new SpielBall(FRAMEWIDTH - 20, 40, RADIUS, 0, 0, FRAMEWIDTH,
FRAMEHEIGHT);
ball2.setRolle(new Katze(ball2, ball1));
ball2.setColor(Color.red);
repaint();
}
public void paint (Graphics g)
{
if (buffer == null) bufferInit();
// loesche Puffer
bufferGraphics.setColor(Color.white);
bufferGraphics.fillRect(0, 0, FRAMEWIDTH, FRAMEHEIGHT);
ball1.moveSpielBall();
ball2.moveSpielBall();
ball1.paintSpielBall(bufferGraphics);
ball2.paintSpielBall(bufferGraphics);
g.drawImage(buffer, 0, 0, this);
repaint();
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
System.exit(0);
}
// Wenn das Spiel beendet ist, weil die Katze die
// Maus gefangen hat, tauschen wir die Rollen und
// fangen von vorne an.
if (ball1.x() == ball2.x() && ball1.y() == ball2.y())
{ // die Maus ist weg
if (ball1.getRolle() instanceof Maus)
{ // ball1 war die Maus, wird jetzt zur Katze
ball1.setRolle(new Katze(ball1, ball2));
// ball2 also zur neuen Maus
ball2.setRolle(new Maus(ball2, this));
}
else
{ // und andersrum: ball1 war also die Katze
ball1.setRolle(new Maus(ball1, this));
// ball2 wird jetzt Katze
ball2.setRolle(new Katze(ball2, ball1));
}
// jeder in seine Ecke:
ball1.moveTo(20 - RADIUS, FRAMEHEIGHT - 20 - RADIUS);
ball2.moveTo(FRAMEWIDTH - 20 - RADIUS, 40 - RADIUS);
}
}
public void update (Graphics g)
{
paint(g);
}
}
Generiert mit Camelot | Probleme mit Camelot? | Geändert am: 05.12.2013


