Kollisionsabfrage
This commit is contained in:
@@ -8,7 +8,6 @@ import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.layout.*;
|
||||
import javafx.scene.paint.ImagePattern;
|
||||
@@ -19,10 +18,7 @@ import javafx.util.Duration;
|
||||
//import java.awt.*;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLOutput;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class HappyBirdMain extends Application {
|
||||
/* Formatierungsklasse */
|
||||
@@ -106,13 +102,25 @@ public class HappyBirdMain extends Application {
|
||||
//gameRunning: das Spiel läuft/ GameScreen ist aufgerufen
|
||||
private boolean gameRunning;
|
||||
|
||||
|
||||
/*TranslateTransitions für obere und untere Pipes*/
|
||||
private TranslateTransition top;
|
||||
private TranslateTransition bottom;
|
||||
private TranslateTransition birdTransition;
|
||||
|
||||
/*ArrayList, in die alle Transitions gespeichert werden*/
|
||||
private final ArrayList<Transition> transitions = new ArrayList<Transition>();
|
||||
|
||||
/*Rechteck für den Bird*/
|
||||
private Rectangle happyBird;
|
||||
|
||||
/*ArrayList, in der alle Rechtecke/ Pipes gespeichert werden*/
|
||||
private final ArrayList<Rectangle> pipesArrayList = new ArrayList<Rectangle>();
|
||||
|
||||
/*Timeline, auf der sich die Pipes und der Vogel abspielen*/
|
||||
private Timeline timeline;
|
||||
private final ArrayList<Transition> transitions = new ArrayList<Transition>();;
|
||||
|
||||
/*AnimationTimer checkt im Spielverlauf, ob eine Pipe mit dem Bird kollidiert*/
|
||||
private AnimationTimer collisionChecker;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
@@ -141,6 +149,27 @@ public class HappyBirdMain extends Application {
|
||||
stage.show();
|
||||
}
|
||||
|
||||
/***
|
||||
* Erstellt den Bird als Rectangle mit einem Bild.
|
||||
* @return happyBird ein Rectangle
|
||||
*/
|
||||
public Rectangle createBird() {
|
||||
/* Startposition des Birds */
|
||||
int y = 325;
|
||||
int x = 50;
|
||||
|
||||
/* Erstellen eines Rechtecks mit der passenden Größe */
|
||||
Rectangle happyBird = new Rectangle(x,y,45,30);
|
||||
|
||||
/* Bild in das Rechteck setzen */
|
||||
happyBird.setFill(new ImagePattern(bird));
|
||||
|
||||
/* View-Order auf 1 setzen, damit der Bird über den anderen Elementen auf dem Spielscreen liegt */
|
||||
happyBird.setStyle("-fx-view-order: 1");
|
||||
|
||||
return happyBird;
|
||||
}
|
||||
|
||||
/***
|
||||
* Erstellt zwei Pipes als Rectangle und fügt ihnen eine Animation über eine Transition hinzu
|
||||
* @return pipes zwei Rectangles
|
||||
@@ -158,45 +187,39 @@ public class HappyBirdMain extends Application {
|
||||
* +400 +250, für Abstand zwischen oberer Pipe und unterer Pipe */
|
||||
Rectangle bottomPipe = new Rectangle(900, y + 400 + 250, 91, 500);
|
||||
|
||||
/* oberer Pipe ein Hintergrundbild geben */
|
||||
Image img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeTop.png");
|
||||
topPipe.setFill(new ImagePattern(pipeTop));
|
||||
topPipe.setStyle("-fx-view-order: 1");
|
||||
/* Pipes in PipesArrayList schreiben */
|
||||
pipesArrayList.add(topPipe);
|
||||
pipesArrayList.add(bottomPipe);
|
||||
|
||||
/* unterer Pipe ein Hintergrundbild geben */
|
||||
img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
|
||||
/* Pipes einen Hintergrundbild geben */
|
||||
topPipe.setFill(new ImagePattern(pipeTop));
|
||||
bottomPipe.setFill(new ImagePattern(pipeBottom));
|
||||
|
||||
/* View-Order auf 1 setzen, damit die Pipe über den anderen Elementen auf dem Spielscreen liegt */
|
||||
topPipe.setStyle("-fx-view-order: 1");
|
||||
bottomPipe.setStyle("-fx-view-order: 1");
|
||||
|
||||
/* neue Animationen über TranslateTransitions */
|
||||
top = new TranslateTransition();
|
||||
bottom = new TranslateTransition();
|
||||
|
||||
/* top-Transition die obere Pipe hinzufügen */
|
||||
/* Pipes den Transitions hinzufügen */
|
||||
top.setNode(topPipe);
|
||||
|
||||
/* die Pipe braucht 7 Sekunden, bis sie von rechts nach links gelaufen ist */
|
||||
top.setDuration(Duration.seconds(7));
|
||||
|
||||
/* Pipe läuft 1100px nach links */
|
||||
top.setToX(-1100);
|
||||
|
||||
/* nach Erreichen des linken Randes, wird topPipe von Pane entfernt */
|
||||
top.setOnFinished(e -> {
|
||||
pane.getChildren().remove(topPipe);
|
||||
});
|
||||
|
||||
|
||||
/* top-Transition die untere Pipe hinzufügen */
|
||||
bottom.setNode(bottomPipe);
|
||||
|
||||
/* die Pipe braucht 7 Sekunden, bis sie von rechts nach links gelaufen ist */
|
||||
/* die Pipes brauchen 7 Sekunden, bis sie von rechts nach links gelaufen ist */
|
||||
top.setDuration(Duration.seconds(7));
|
||||
bottom.setDuration(Duration.seconds(7));
|
||||
|
||||
/* Pipe läuft 1100px nach links */
|
||||
top.setToX(-1100);
|
||||
bottom.setToX(-1100);
|
||||
|
||||
/* nach Erreichen des linken Randes, wird topPipe von Pane entfernt */
|
||||
|
||||
/* nach Erreichen des linken Randes, wird die Pipe von Pane entfernt */
|
||||
top.setOnFinished(e -> {
|
||||
pane.getChildren().remove(topPipe);
|
||||
});
|
||||
bottom.setOnFinished(e -> {
|
||||
pane.getChildren().remove(bottomPipe);
|
||||
});
|
||||
@@ -211,7 +234,10 @@ public class HappyBirdMain extends Application {
|
||||
return pipes;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* Generiert den GameScreen durch Ausblenden und
|
||||
* Sichtbarmachen der richtigen Buttons, Labels, ...
|
||||
*/
|
||||
public void generateMenueScreen() {
|
||||
dataQuery();
|
||||
menuScreenRunning = true;
|
||||
@@ -344,11 +370,11 @@ public class HappyBirdMain extends Application {
|
||||
startGameButton.setVisible(false);
|
||||
menuButton.setVisible(false);
|
||||
|
||||
/*Vogel hinzufügen, bisher nur als Bild*/
|
||||
/*Vogel hinzufügen, bisher nur als Bild
|
||||
ImageView birdView = new ImageView(bird);
|
||||
pane.getChildren().add(birdView);
|
||||
birdView.setLayoutY(325);
|
||||
birdView.setLayoutX(50);
|
||||
birdView.setLayoutX(50);*/
|
||||
|
||||
/*Pipes oben, unten hinzufügen, bisher nur als Bild
|
||||
ImageView upperPipeView = new ImageView(upperPipe);
|
||||
@@ -372,14 +398,62 @@ public class HappyBirdMain extends Application {
|
||||
pane.getChildren().add(pipe);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*Transitions abspielen*/
|
||||
top.play();
|
||||
bottom.play();
|
||||
}));
|
||||
|
||||
timeline.setCycleCount(Animation.INDEFINITE);
|
||||
/*timelineBird = new Timeline(new KeyFrame(Duration.seconds(50), e -> {
|
||||
Rectangle happyBird = createBird();
|
||||
pane.getChildren().add(happyBird);
|
||||
birdTransition.play();
|
||||
}));*/
|
||||
|
||||
happyBird = createBird();
|
||||
pane.getChildren().add(happyBird);
|
||||
|
||||
timeline.setCycleCount(Animation.INDEFINITE);
|
||||
timeline.play();
|
||||
|
||||
System.out.println("tl: "+timeline.getStatus());
|
||||
|
||||
collisionChecker = new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long timestamp) {
|
||||
for (Rectangle rectangle : pipesArrayList) {
|
||||
if(rectangle != null){
|
||||
checkCollision(happyBird,rectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
collisionChecker.start();
|
||||
|
||||
}
|
||||
|
||||
/***
|
||||
* Überprüft, ob eine Pipe mit dem Bird kollidiert
|
||||
* @param happyBird Rechteck mit dem Bird
|
||||
* @param pipe Rechteck mit einer Pipe
|
||||
*/
|
||||
public void checkCollision(Rectangle happyBird, Rectangle pipe) {
|
||||
|
||||
/* Überprüft, ob eine der Grenzen des Rechtecks des Birds mit einer übergebenen Pipe kollidiert
|
||||
* wenn ja, hält das Spiel an bzw. wird beendet */
|
||||
if(happyBird.getBoundsInParent().intersects(pipe.getBoundsInParent())) {
|
||||
|
||||
/*Pausieren der Transitions/ Pipes*/
|
||||
for(Transition transition : transitions) {
|
||||
transition.pause();
|
||||
}
|
||||
|
||||
/*Pausieren der Timeline, sodass keine neuen Pipes spawnen*/
|
||||
timeline.stop();
|
||||
gameRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -568,7 +642,7 @@ public class HappyBirdMain extends Application {
|
||||
|
||||
/*Klick auf den Pause-Button, um das Pause-Menü aufzurufen*/
|
||||
pauseButton.setOnAction(event -> {
|
||||
if(!menuScreenRunning) {
|
||||
if (!menuScreenRunning) {
|
||||
pauseButtonClick(gameRunning);
|
||||
gameRunning = !gameRunning;
|
||||
}
|
||||
@@ -576,7 +650,7 @@ public class HappyBirdMain extends Application {
|
||||
|
||||
/*ESC-Tast drücken, um das Pause-Menü aufzurufen*/
|
||||
scene.setOnKeyPressed(e -> {
|
||||
if(e.getCode() == KeyCode.ESCAPE && !menuScreenRunning) {
|
||||
if (e.getCode() == KeyCode.ESCAPE && !menuScreenRunning) {
|
||||
pauseButtonClick(gameRunning);
|
||||
gameRunning = !gameRunning;
|
||||
}
|
||||
@@ -866,8 +940,6 @@ public class HappyBirdMain extends Application {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* public void generateEndScreen() generiert den Endscreen - sobald das Spiel beendet ist
|
||||
* (man verloren hat). Es werden die Punkte des Spielers angezeigt, und es bestehen die Möglichkeiten,
|
||||
|
Reference in New Issue
Block a user