import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Hurdler extends Applet implements ActionListener, Runnable, KeyListener{
private Button start, nextLevel, restart;
private JLabel ScoreAnzeige;
private JLabel LPAnzeige;
private Screen Screen1;
private Huerde[] Huerde = new Huerde[20];
private Grube[] Grube = new Grube[10];
private Spielfigur Hurdler;
private int Level = 0;
public boolean go = true;
private int[] won = new int[100];
//buffer
private Image cacheBild;
private Graphics cacheGrafik;
private int pass = 1;
public void init() {
this.setSize(1250, 250);
Panel panel1 = new Panel();
Panel panel2 = new Panel();
start = new Button("Start");
nextLevel = new Button("Next Level");
restart = new Button("Try Again");
panel1.add(start);
panel1.add(nextLevel);
add(panel1);
start.addActionListener(this);
nextLevel.addActionListener(this);
restart.addActionListener(this);
nextLevel.setVisible(false);
restart.setVisible(false);
ScoreAnzeige = new JLabel("Score: ");
LPAnzeige = new JLabel("Lebenspunkte: ");
ScoreAnzeige.setVisible(true);
ScoreAnzeige.setVisible(false);
LPAnzeige.setVisible(false);
panel2.add(ScoreAnzeige);
panel2.add(LPAnzeige);
panel2.add(restart);
add(panel2);
//Objekte instanzieren
Screen1 = new Screen(1250, 250);
//LVL 1
Huerde[1] = new Huerde(50,350,200);
Huerde[2] = new Huerde(100,500,200);
Grube[1] = new Grube(100,750,200);
//LVL2
Huerde[3] = new Huerde(50,150,200);
Huerde[4] = new Huerde(50,280,200);
Huerde[5] = new Huerde(100,400,200);
Huerde[6] = new Huerde(100,580,200);
Grube[2] = new Grube(100,650,200);
//LVL3
Huerde[7] = new Huerde(100,200,200);
Huerde[8] = new Huerde(50,350,200);
Huerde[9] = new Huerde(100,500,200);
Huerde[10] = new Huerde(50,650,200);
Huerde[11] = new Huerde(100,900,200);
//LVL4
Huerde[12] = new Huerde(100,200,200);
Huerde[13] = new Huerde(100,300,200);
Huerde[14] = new Huerde(100,400,200);
Huerde[15] = new Huerde(100,500,200);
Huerde[16] = new Huerde(100,600,200);
Huerde[17] = new Huerde(100,700,200);
Huerde[18] = new Huerde(100,800,200);
Huerde[19] = new Huerde(100,900,200);
Hurdler = new Spielfigur(20,200);
addKeyListener(this);
}
public void update(Graphics a) {
// Initialisierung des Buffers
if (cacheBild == null) {
cacheBild = createImage(this.getSize().width, this.getSize().height);
cacheGrafik = cacheBild.getGraphics();
}
// Hintergreund loeschen
cacheGrafik.setColor(getBackground());
cacheGrafik.fillRect(0, 0, this.getSize().width, this.getSize().height);
// Fordergrund zeichnen
cacheGrafik.setColor(getForeground());
paint(cacheGrafik);
// Gezeichnetes Bild anzeigen
a.drawImage(cacheBild, 0, 0, this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == start) {
Level = 1;
start.setVisible(false);
ScoreAnzeige.setVisible(true);
LPAnzeige.setVisible(true);
requestFocus();
}
if (event.getSource() == nextLevel) {
Level = Level + 1;
nextLevel.setVisible(false);
this.nextLevel();
requestFocus();
}
if (event.getSource() == restart) {
restart.setVisible(false);
this.nextLevel();
requestFocus();
}
repaint();
}
public void paint (Graphics g) {
if (Level > 0) {
if (Hurdler.getTot() == true) {
Hurdler.paintSpielfigurTot(g); //Toter Hurdler zeichnen
}
else if (Hurdler.getGewonnen() == true) {
Hurdler.paintSpielfigurGewonnen(g);
}
else {
Hurdler.paintSpielfigur(g); //Hurdler zeichnen
}
}
switch(Level) {
case 1:
setSize(Screen1.getBreite(), Screen1.getHoehe()); //Fenstergrösse setzen
Screen1.paintScreen(g); //Hintergrund zeichnen
Huerde[1].paintkleineHuerde(g); //Huerde zeichnen
Huerde[2].paintgrosseHuerde(g); //Huerde zeichnen
Grube[1].paintgrosseGrube(g); //Grube zeichnen
break;
case 2:
setSize(Screen1.getBreite(), Screen1.getHoehe());
Screen1.paintScreen(g);
Huerde[3].paintkleineHuerde(g);
Huerde[4].paintkleineHuerde(g);
Huerde[5].paintgrosseHuerde(g);
Huerde[6].paintgrosseHuerde(g);
Grube[2].paintgrosseGrube(g);
break;
case 3:
setSize(Screen1.getBreite(), Screen1.getHoehe());
Screen1.paintScreen(g);
Huerde[7].paintgrosseHuerde(g);
Huerde[8].paintkleineHuerde(g);
Huerde[9].paintgrosseHuerde(g);
Huerde[10].paintkleineHuerde(g);
Huerde[11].paintgrosseHuerde(g);
break;
case 4:
setSize(Screen1.getBreite(), Screen1.getHoehe());
Screen1.paintScreen(g);
Huerde[12].paintkleineHuerde(g);
Huerde[13].paintkleineHuerde(g);
Huerde[14].paintkleineHuerde(g);
Huerde[15].paintkleineHuerde(g);
Huerde[16].paintkleineHuerde(g);
Huerde[17].paintkleineHuerde(g);
Huerde[18].paintkleineHuerde(g);
break;
case 5:
setSize(Screen1.getBreite(), Screen1.getHoehe());
Screen1.paintScreen(g);
break;
default:
break;
}
}
public void start() {
Thread th = new Thread(this);
th.start();
}
public void run() {
Hurdler.sleep(3000);
//Endlosschleife
for (int i=0;i!=1;) {
//Gehen
while (go == true) {
Hurdler.setWalk();
//Überprüfungen beim Gehen
if (this.Kollision() == true) {
Hurdler.setTot(true);
}
if (Hurdler.getTot() == true) {
go = false;
}
if (Hurdler.getGewonnen() == true) {
go = false;
nextLevel.setVisible(true);
validate();
setSize(Screen1.getBreite(), Screen1.getHoehe());
}
repaint();
}
}
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
// PFEILTASTE OBEN
case 38:
go = false;
Hurdler.setBigJump();
for(int i=0;i<Hurdler.getAnzahl();i=i+1) {
if (Hurdler.getTot() == false) {
if (Hurdler.getGewonnen() == false) {
Hurdler.Jump();
if (this.Kollision() == true) {
Hurdler.setTot(true);
}
if (Hurdler.getTot() == true) {
go = false;
}
if (Hurdler.getGewonnen() == true) {
go = false;
nextLevel.setVisible(true);
validate();
}
update(getGraphics());
}
}
}
Hurdler.setmSPosY(200);
go = true;
break;
// PFEILTASTE UNTEN
case 40:
go = false;
Hurdler.setSmallJump();
for(int i=0;i<Hurdler.getAnzahl();i=i+1) {
if (Hurdler.getTot() == false) {
if (Hurdler.getGewonnen() == false) {
Hurdler.Jump();
if (this.Kollision() == true) {
Hurdler.setTot(true);
}
if (Hurdler.getTot() == true) {
go = false;
}
if (Hurdler.getGewonnen() == true) {
go = false;
}
update(getGraphics());
}
}
}
Hurdler.setmSPosY(200);
go = true;
break;
// DEFAULT
default:
break;
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public boolean Kollision() {
int x; //Rechter Arm
int y; //Rechter Arm
int a; //Rechtes Bein
int b; //Rechtes Bein
int c; //Linkes Bein
int d; //Linkes Bein
int score; //Punkte
boolean z = false;
a = Hurdler.getmSPosX()+10;
b = Hurdler.getmSPosY();
c = Hurdler.getmSPosX();
d = Hurdler.getmSPosY();
x = Hurdler.getmSPosX()+12;
y = Hurdler.getmSPosY()-17;
int q = Huerde[1].getStartPosX()+Huerde[1].getBreite();
int p = Huerde[1].getStartPosY()-Huerde[1].getHoehe();
int o = Huerde[2].getStartPosX()+Huerde[2].getBreite();
int s = Huerde[2].getStartPosY()-Huerde[2].getHoehe();
//Ziel erreicht
if (x >= 1250) {
Hurdler.setGewonnen(true);
}
if (Hurdler.getTot() == false) {
switch (Level) {
case 1:
//Hürden Überprüfen
/*for(int HuerdenNr=1;HuerdenNr <= 2;HuerdenNr=HuerdenNr+1) {
if (x >= Huerde[HuerdenNr].getStartPosX() && x <= Huerde[HuerdenNr].getStartPosX()+Huerde[HuerdenNr].getBreite()) {
if (y >= Huerde[HuerdenNr].getStartPosY()-Huerde[HuerdenNr].getHoehe() && y <= Huerde[HuerdenNr].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[HuerdenNr].getStartPosX() && a <= Huerde[HuerdenNr].getStartPosX()+Huerde[HuerdenNr].getBreite()) {
if (b >= Huerde[HuerdenNr].getStartPosY()-Huerde[HuerdenNr].getHoehe() && b <= Huerde[HuerdenNr].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[HuerdenNr].getStartPosX() && c <= Huerde[HuerdenNr].getStartPosX()+Huerde[HuerdenNr].getBreite()) {
if (d >= Huerde[HuerdenNr].getStartPosY()-Huerde[HuerdenNr].getHoehe()&& d <= Huerde[HuerdenNr].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[HuerdenNr].getStartPosX()+Huerde[HuerdenNr].getBreite()) && z == false && won[pass] == 0) {
won[pass] = 1;
pass = pass + 1;
System.out.println(pass);
Hurdler.setScore(Huerde[HuerdenNr].getPoints());
}
}
//Gruben Überprüfen
for(int GrubenNr=1;GrubenNr == 1;GrubenNr=GrubenNr+1) {
if (x >= Grube[GrubenNr].getStartPosX() && x <= Grube[GrubenNr].getStartPosX()+Grube[GrubenNr].getBreite()) {
if (y >= 200) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Grube[GrubenNr].getStartPosX() && c <= Grube[GrubenNr].getStartPosX()+Grube[GrubenNr].getBreite()) {
if (d >= 200) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Grube[GrubenNr].getStartPosX()+Grube[GrubenNr].getBreite()) && z == false && won[pass] == 0) {
won[pass] = 1;
pass = pass + 1;
Hurdler.setScore(Grube[GrubenNr].getPoints());
}
}*/
/*
//Huerde[1] überprüfen
if (x >= Huerde[1].getStartPosX() && x <= Huerde[1].getStartPosX()+Huerde[1].getBreite()) {
if (y >= Huerde[1].getStartPosY()-Huerde[1].getHoehe() && y <= Huerde[1].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[1].getStartPosX() && a <= Huerde[1].getStartPosX()+Huerde[1].getBreite()) {
if (b >= Huerde[1].getStartPosY()-Huerde[1].getHoehe() && b <= Huerde[1].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[1].getStartPosX() && c <= Huerde[1].getStartPosX()+Huerde[1].getBreite()) {
if (d >= Huerde[1].getStartPosY()-Huerde[1].getHoehe()&& d <= Huerde[1].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[1].getStartPosX()+Huerde[1].getBreite()) && z == false && won[1] == 0) {
won[1] = 1;
Hurdler.setScore(Huerde[1].getPoints());
}
//Huerde[2] überprüfen
if (x >= Huerde[2].getStartPosX() && x <= Huerde[2].getStartPosX()+Huerde[2].getBreite()) {
if (y >= Huerde[2].getStartPosY()-Huerde[2].getHoehe() && y <= Huerde[2].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[2].getStartPosX() && c <= Huerde[2].getStartPosX()+Huerde[2].getBreite()) {
if (d >= Huerde[2].getStartPosY()-Huerde[2].getHoehe()&& d <= Huerde[2].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[2].getStartPosX() && a <= Huerde[2].getStartPosX()+Huerde[2].getBreite()) {
if (b >= Huerde[2].getStartPosY()-Huerde[2].getHoehe()&& b <= Huerde[2].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[2].getStartPosX()+Huerde[2].getBreite()) && z == false && won[2] == 0) {
won[2] = 1;
Hurdler.setScore(Huerde[2].getPoints());
}
//Grube[1] überprüfen
if (x >= Grube[1].getStartPosX() && x <= Grube[1].getStartPosX()+Grube[1].getBreite()) {
if (y >= 200) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Grube[1].getStartPosX() && c <= Grube[1].getStartPosX()+Grube[1].getBreite()) {
if (d >= 200) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Grube[1].getStartPosX()+Grube[1].getBreite()) && z == false && won[3] == 0) {
won[3] = 1;
Hurdler.setScore(Grube[1].getPoints());
}*/
break;
case 2:
//Huerde[3] überprüfen
if (x >= Huerde[3].getStartPosX() && x <= Huerde[3].getStartPosX()+Huerde[3].getBreite()) {
if (y >= Huerde[3].getStartPosY()-Huerde[3].getHoehe() && y <= Huerde[3].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[3].getStartPosX() && a <= Huerde[3].getStartPosX()+Huerde[3].getBreite()) {
if (b >= Huerde[3].getStartPosY()-Huerde[3].getHoehe() && b <= Huerde[3].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[3].getStartPosX() && c <= Huerde[3].getStartPosX()+Huerde[3].getBreite()) {
if (d >= Huerde[3].getStartPosY()-Huerde[3].getHoehe()&& d <= Huerde[3].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[3].getStartPosX()+Huerde[3].getBreite()) && z == false && won[4] == 0) {
won[4] = 1;
Hurdler.setScore(Huerde[3].getPoints());
}
//Huerde[4] überprüfen
if (x >= Huerde[4].getStartPosX() && x <= Huerde[4].getStartPosX()+Huerde[4].getBreite()) {
if (y >= Huerde[4].getStartPosY()-Huerde[4].getHoehe() && y <= Huerde[4].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[4].getStartPosX() && a <= Huerde[4].getStartPosX()+Huerde[4].getBreite()) {
if (b >= Huerde[4].getStartPosY()-Huerde[4].getHoehe() && b <= Huerde[4].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[4].getStartPosX() && c <= Huerde[4].getStartPosX()+Huerde[4].getBreite()) {
if (d >= Huerde[4].getStartPosY()-Huerde[4].getHoehe()&& d <= Huerde[4].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[4].getStartPosX()+Huerde[4].getBreite()) && z == false && won[5] == 0) {
won[5] = 1;
Hurdler.setScore(Huerde[4].getPoints());
}
//Huerde[5] überprüfen
if (x >= Huerde[5].getStartPosX() && x <= Huerde[5].getStartPosX()+Huerde[5].getBreite()) {
if (y >= Huerde[5].getStartPosY()-Huerde[5].getHoehe() && y <= Huerde[5].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[5].getStartPosX() && a <= Huerde[5].getStartPosX()+Huerde[5].getBreite()) {
if (b >= Huerde[5].getStartPosY()-Huerde[5].getHoehe() && b <= Huerde[5].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[5].getStartPosX() && c <= Huerde[5].getStartPosX()+Huerde[5].getBreite()) {
if (d >= Huerde[5].getStartPosY()-Huerde[5].getHoehe()&& d <= Huerde[5].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[5].getStartPosX()+Huerde[5].getBreite()) && z == false && won[6] == 0) {
won[6] = 1;
Hurdler.setScore(Huerde[5].getPoints());
}
//Huerde[6] überprüfen
if (x >= Huerde[6].getStartPosX() && x <= Huerde[6].getStartPosX()+Huerde[6].getBreite()) {
if (y >= Huerde[6].getStartPosY()-Huerde[6].getHoehe() && y <= Huerde[6].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (a >= Huerde[6].getStartPosX() && a <= Huerde[6].getStartPosX()+Huerde[6].getBreite()) {
if (b >= Huerde[6].getStartPosY()-Huerde[6].getHoehe() && b <= Huerde[6].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Huerde[6].getStartPosX() && c <= Huerde[6].getStartPosX()+Huerde[6].getBreite()) {
if (d >= Huerde[6].getStartPosY()-Huerde[6].getHoehe()&& d <= Huerde[6].getStartPosY()) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c > (Huerde[6].getStartPosX()+Huerde[6].getBreite()) && z == false && won[7] == 0) {
won[7] = 1;
Hurdler.setScore(Huerde[6].getPoints());
}
//Grube[2] überprüfen
if (x >= Grube[2].getStartPosX() && x <= Grube[2].getStartPosX()+Grube[2].getBreite()) {
if (y >= 200) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
if (c >= Grube[2].getStartPosX() && c <= Grube[2].getStartPosX()+Grube[2].getBreite()) {
if (d >= 200) {
z = true;
Hurdler.setLivepoints(Hurdler.getLivepoints()-1);
}
}
break;
default:
break;
}
}
//Anzeige Aktualisieren
ScoreAnzeige.setText("Score: "+Hurdler.getmScore());
LPAnzeige.setText("Lebenspunkte: "+Hurdler.getLivepoints());
//try again wenn hurdler tot
if (z == true) {
restart.setVisible(true);
validate();
}
return z;
}
public void nextLevel() {
Hurdler.setmSPosY(200);
Hurdler.setmSPosX(20);
Hurdler.setGewonnen(false);
Hurdler.setTot(false);
go = true;
}
}