Pipes Bewegung
This commit is contained in:
parent
e2457886c8
commit
49e63e3d3b
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
After Width: | Height: | Size: 4.6 KiB |
Binary file not shown.
@ -236,4 +236,9 @@ public class FormatingClass {
|
||||
pane.getChildren().add(backgroundButtons[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void menueScreenFormating(Label[] labels, Button[] buttons) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,40 @@
|
||||
package com.example.happy_bird;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class GameObject {
|
||||
int x, y, width, height;
|
||||
Rectangle topPipe, bottomPipe;
|
||||
int distance = 105;
|
||||
boolean isPassedOn = false;
|
||||
import javafx.scene.Node;
|
||||
|
||||
// Objekt Initialisierung
|
||||
public abstract class GameObject extends Node {
|
||||
protected int xKoord;
|
||||
protected int yKoord;
|
||||
protected int width;
|
||||
protected int height;
|
||||
protected boolean isPassedOn = false;
|
||||
|
||||
/***
|
||||
* Konstruktor GameObject
|
||||
* Variablen werden zugewiesen
|
||||
* @param x x-Position
|
||||
* @param y y-Position
|
||||
* @param width Breite
|
||||
* @param height Höhe
|
||||
*/
|
||||
public GameObject(int x, int y, int width, int height) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.xKoord = x;
|
||||
this.yKoord = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
topPipe = new Rectangle(x, y, width, height);
|
||||
bottomPipe = new Rectangle(x, height + distance, width, height);
|
||||
}
|
||||
|
||||
// Auf Anfangs Position setzen
|
||||
public void resetToNewPosition(int newX) {
|
||||
topPipe.x = newX;
|
||||
bottomPipe.x = newX;
|
||||
x = newX;
|
||||
topPipe.y = -(new Random().nextInt(140) + 100);
|
||||
bottomPipe.y = topPipe.y + height + distance;
|
||||
isPassedOn = false;
|
||||
}
|
||||
|
||||
public abstract void setImage();
|
||||
|
||||
/***
|
||||
* Setzt ein Object auf eine neue Position
|
||||
* param newX neue x-Position
|
||||
*/
|
||||
public abstract void resetToNewPosition(int newX);
|
||||
|
||||
/*
|
||||
// Prüfung ob Rectangle true oder false ist
|
||||
public boolean intersect(Rectangle rectangle) {
|
||||
return rectangle.intersects(topPipe) || rectangle.intersects(bottomPipe);
|
||||
@ -36,13 +42,13 @@ public class GameObject {
|
||||
|
||||
// Röhren Counter → Check, wenn Röhre durchflogen ist
|
||||
public boolean passedOn(Rectangle rectangle) {
|
||||
return rectangle.x > x + width && !isPassedOn;
|
||||
return rectangle.x > xKoord + width && !isPassedOn;
|
||||
}
|
||||
|
||||
// Bei Bewegung der X Koordinate werden Röhren generiert
|
||||
public void moveX(int dx) {
|
||||
x -= dx;
|
||||
xKoord -= dx;
|
||||
topPipe.x -= dx;
|
||||
bottomPipe.x -= dx;
|
||||
}
|
||||
}*/
|
||||
}
|
@ -1,16 +1,28 @@
|
||||
package com.example.happy_bird;
|
||||
|
||||
import javafx.animation.*;
|
||||
import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.*;
|
||||
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;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.scene.shape.*;
|
||||
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 */
|
||||
@ -85,8 +97,8 @@ public class HappyBirdMain extends Application {
|
||||
/*Bilder*/
|
||||
private final Image bird = new Image("file:src/main/resources/com/example/happy_bird/pics/bird.png", 50, 35, false, false);
|
||||
|
||||
private final Image upperPipe = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeOben.png", 86, 300, false, false);
|
||||
private final Image lowerPipe = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeUnten.png", 86, 300, false, false);
|
||||
private final Image pipeTop = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeTop.png");
|
||||
private final Image pipeBottom = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
|
||||
|
||||
/*Variablen für den Spielverlauf*/
|
||||
//menuScreenRunning: das Hauptmenü ist aufgerufen
|
||||
@ -94,16 +106,26 @@ public class HappyBirdMain extends Application {
|
||||
//gameRunning: das Spiel läuft/ GameScreen ist aufgerufen
|
||||
private boolean gameRunning;
|
||||
|
||||
|
||||
private TranslateTransition top;
|
||||
private TranslateTransition bottom;
|
||||
|
||||
private Timeline timeline;
|
||||
private final ArrayList<Transition> transitions = new ArrayList<Transition>();;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
launch(args);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) {
|
||||
|
||||
stage.setTitle("Happy Bird");
|
||||
stage.setScene(scene);
|
||||
generateMenueScene();
|
||||
generateMenueScreen();
|
||||
|
||||
/*ClickEvents:*/
|
||||
startButton.setOnAction(event -> startButtonClick());
|
||||
@ -119,11 +141,81 @@ public class HappyBirdMain extends Application {
|
||||
startGameButtonClick();
|
||||
});
|
||||
|
||||
|
||||
stage.show();
|
||||
}
|
||||
|
||||
/***
|
||||
* Erstellt zwei Pipes als Rectangle und fügt ihnen eine Animation über eine Transition hinzu
|
||||
* @return pipes zwei Rectangles
|
||||
*/
|
||||
public Rectangle[] createPipes() {
|
||||
|
||||
public void generateMenueScene() {
|
||||
/* y-Koordinate für die Pipes
|
||||
* -300, da die obere Pipe 500px hoch ist */
|
||||
int y = (int) (Math.random() * (200))-300;
|
||||
|
||||
/* Pipe oben; spawnt bei x=900 -> 100 Pixel neben Bild */
|
||||
Rectangle topPipe = new Rectangle(900, y, 91, 500);
|
||||
|
||||
/* Pipe unten; spawnt auf der gleichen Höhe wie obere Pipe
|
||||
* +400, da y-Koord die untere linke Ecke ist
|
||||
* +250, der 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));
|
||||
|
||||
/* unterer Pipe ein Hintergrundbild geben */
|
||||
img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
|
||||
bottomPipe.setFill(new ImagePattern(pipeBottom));
|
||||
|
||||
/* neue Animationen über TranslateTransitions */
|
||||
top = new TranslateTransition();
|
||||
bottom = new TranslateTransition();
|
||||
|
||||
/* top-Transition die obere Pipe 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 */
|
||||
bottom.setDuration(Duration.seconds(7));
|
||||
|
||||
/* Pipe läuft 1100px nach links */
|
||||
bottom.setToX(-1100);
|
||||
|
||||
/* nach Erreichen des linken Randes, wird topPipe von Pane entfernt */
|
||||
bottom.setOnFinished(e -> {
|
||||
pane.getChildren().remove(bottomPipe);
|
||||
});
|
||||
|
||||
/* Array für die Pipes */
|
||||
Rectangle[] pipes = {topPipe, bottomPipe};
|
||||
|
||||
/* Transitions in ArrayList speichern, um sie nachher stoppen zu können */
|
||||
transitions.add(top);
|
||||
transitions.add(bottom);
|
||||
|
||||
return pipes;
|
||||
}
|
||||
|
||||
|
||||
public void generateMenueScreen() {
|
||||
dataQuery();
|
||||
menuScreenRunning = true;
|
||||
|
||||
@ -204,8 +296,9 @@ public class HappyBirdMain extends Application {
|
||||
yPositionInputFields += 150;
|
||||
}
|
||||
|
||||
/*SoundSlicer anpassen:*/
|
||||
/*SoundSlider anpassen:*/
|
||||
formatingClass.soundSliderFormating(soundSlider, pane);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -260,7 +353,7 @@ public class HappyBirdMain extends Application {
|
||||
birdView.setLayoutY(325);
|
||||
birdView.setLayoutX(50);
|
||||
|
||||
/*Pipes oben, unten hinzufügen, bisher nur als Bild*/
|
||||
/*Pipes oben, unten hinzufügen, bisher nur als Bild
|
||||
ImageView upperPipeView = new ImageView(upperPipe);
|
||||
pane.getChildren().add(upperPipeView);
|
||||
upperPipeView.setLayoutX(400);
|
||||
@ -269,7 +362,27 @@ public class HappyBirdMain extends Application {
|
||||
ImageView lowerPipeView = new ImageView(lowerPipe);
|
||||
pane.getChildren().add(lowerPipeView);
|
||||
lowerPipeView.setLayoutX(400);
|
||||
lowerPipeView.setLayoutY(430);
|
||||
lowerPipeView.setLayoutY(430);*/
|
||||
|
||||
|
||||
/*Timeline: alle 2 Sekunden spawnt eine neue Pipe am rechten Rand*/
|
||||
timeline = new Timeline(new KeyFrame(Duration.seconds(2), e ->{
|
||||
/*Pipes erstellen*/
|
||||
Rectangle[] pipesTopBottom = createPipes();
|
||||
|
||||
/*Pipes der Pane hinzufügen*/
|
||||
for(Rectangle pipe : pipesTopBottom) {
|
||||
pane.getChildren().add(pipe);
|
||||
}
|
||||
|
||||
/*Transitions abspielen*/
|
||||
top.play();
|
||||
bottom.play();
|
||||
}));
|
||||
|
||||
timeline.setCycleCount(Animation.INDEFINITE);
|
||||
|
||||
timeline.play();
|
||||
}
|
||||
|
||||
/***
|
||||
@ -474,7 +587,6 @@ public class HappyBirdMain extends Application {
|
||||
} else {
|
||||
System.out.println("Name oder Kürzel falsch!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/***
|
||||
@ -552,10 +664,20 @@ public class HappyBirdMain extends Application {
|
||||
|
||||
menuButtonPause.setOnAction(event -> {
|
||||
menuButtonPauseClick();
|
||||
generateMenueScene();
|
||||
generateMenueScreen();
|
||||
});
|
||||
|
||||
settingsButtonPause.setOnAction(event -> settingsButtonPauseClick());
|
||||
|
||||
/*Pausieren der Transitions/ Pipes*/
|
||||
for(Transition transition : transitions) {
|
||||
transition.pause();
|
||||
System.out.println(transition.toString());
|
||||
}
|
||||
|
||||
/*Pausieren der Timeline, sodass keine neuen Pipes spawnen*/
|
||||
timeline.pause();
|
||||
|
||||
} else {
|
||||
/*pauseButton einblenden*/
|
||||
pauseButton.setVisible(true);
|
||||
@ -568,6 +690,14 @@ public class HappyBirdMain extends Application {
|
||||
|
||||
/*pause von pane entfernen*/
|
||||
pane.getChildren().remove(pause);
|
||||
|
||||
/*Wiederabspielen der Transitions/ Pipes*/
|
||||
for(Transition transition : transitions) {
|
||||
transition.play();
|
||||
}
|
||||
|
||||
/*Wiederabspielen der Timeline, sodass neue Pipes spawnen*/
|
||||
timeline.play();
|
||||
}
|
||||
}
|
||||
|
||||
@ -845,6 +975,6 @@ public class HappyBirdMain extends Application {
|
||||
*/
|
||||
private void backMenuButtonClick() {
|
||||
pane.getChildren().remove(endScreen);
|
||||
generateMenueScene();
|
||||
generateMenueScreen();
|
||||
}
|
||||
}
|
110
Happy_Bird/src/main/java/com/example/happy_bird/Pipes.java
Normal file
110
Happy_Bird/src/main/java/com/example/happy_bird/Pipes.java
Normal file
@ -0,0 +1,110 @@
|
||||
package com.example.happy_bird;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.animation.Transition;
|
||||
import javafx.animation.TranslateTransition;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.scene.paint.ImagePattern;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import javafx.animation.TranslateTransition;
|
||||
import javafx.util.Duration;
|
||||
|
||||
public class Pipes extends Rectangle {
|
||||
private int y;
|
||||
private Rectangle topPipe;
|
||||
private Rectangle bottomPipe;
|
||||
private int distanceBeweenPipes = 250;
|
||||
private boolean isPassedOn = false;
|
||||
/***
|
||||
* Konstruktor GameObject
|
||||
* Variablen werden zugewiesen
|
||||
* param x x-Position
|
||||
* param y y-Position
|
||||
* param width Breite
|
||||
* param height Höhe
|
||||
*/
|
||||
public Pipes(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Rectangle[] createPipes(Transition top, Transition bottom, Pane pane) {
|
||||
int y = (int) (Math.random() * (200))-300;
|
||||
Rectangle topPipe = new Rectangle(1000, y, 91, 500);
|
||||
Rectangle bottomPipe = new Rectangle(1000, y + 400 + 250, 91, 500);
|
||||
|
||||
|
||||
Image img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeTop.png");
|
||||
topPipe.setFill(new ImagePattern(img));
|
||||
|
||||
img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
|
||||
bottomPipe.setFill(new ImagePattern(img));
|
||||
|
||||
top = new TranslateTransition();
|
||||
bottom = new TranslateTransition();
|
||||
|
||||
((TranslateTransition) top).setNode(topPipe);
|
||||
((TranslateTransition) top).setDuration(Duration.seconds(5));
|
||||
((TranslateTransition) top).setToX(-1200);
|
||||
top.setOnFinished(e -> {
|
||||
pane.getChildren().remove(topPipe);
|
||||
});
|
||||
|
||||
((TranslateTransition) bottom).setNode(bottomPipe);
|
||||
((TranslateTransition) bottom).setDuration(Duration.seconds(5));
|
||||
((TranslateTransition) bottom).setToX(-1200);
|
||||
bottom.setOnFinished(e -> {
|
||||
pane.getChildren().remove(bottomPipe);
|
||||
});
|
||||
|
||||
Rectangle[] pipes = {topPipe, bottomPipe};
|
||||
return pipes;
|
||||
}
|
||||
|
||||
public void destroy(Pane pane) {
|
||||
|
||||
pane.getChildren().remove(topPipe);
|
||||
pane.getChildren().remove(bottomPipe);
|
||||
}
|
||||
|
||||
/***
|
||||
* Setzt für die beiden Rechtecke der Pipes das Hintergrundbild
|
||||
*/
|
||||
public void setImage() {
|
||||
Image img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeTop.png");
|
||||
topPipe.setFill(new ImagePattern(img));
|
||||
|
||||
img = new Image("file:src/main/resources/com/example/happy_bird/pics/PipeBottom.png");
|
||||
bottomPipe.setFill(new ImagePattern(img));
|
||||
}
|
||||
|
||||
/***
|
||||
* Setzt ein Object auf eine neue Position
|
||||
* param newX neue x-Position
|
||||
*/
|
||||
public void resetToNewPosition(int newX) {
|
||||
topPipe.setX(newX);
|
||||
bottomPipe.setX(newX);
|
||||
//xKoord = newX;
|
||||
}
|
||||
|
||||
/*
|
||||
// Prüfung ob Rectangle true oder false ist
|
||||
public boolean intersect(Rectangle rectangle) {
|
||||
return rectangle.intersects(topPipe) || rectangle.intersects(bottomPipe);
|
||||
}
|
||||
|
||||
// Röhren Counter → Check, wenn Röhre durchflogen ist
|
||||
public boolean passedOn(Rectangle rectangle) {
|
||||
return rectangle.x > xKoord + width && !isPassedOn;
|
||||
}
|
||||
|
||||
// Bei Bewegung der X Koordinate werden Röhren generiert
|
||||
public void moveX(int dx) {
|
||||
xKoord -= dx;
|
||||
topPipe.x -= dx;
|
||||
bottomPipe.x -= dx;
|
||||
}*/
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 4.9 KiB |
Binary file not shown.
After Width: | Height: | Size: 4.6 KiB |
Loading…
Reference in New Issue
Block a user