Highscores einfügen & Score im Spiel

This commit is contained in:
Mats Pape 2022-01-11 13:37:15 +01:00
parent e2e726adfb
commit 1f544960ac
9 changed files with 86 additions and 9 deletions

View File

@ -2,10 +2,7 @@ package com.example.happy_bird;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
@ -13,8 +10,11 @@ import javafx.scene.layout.*;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import java.sql.ResultSet;
public class HappyBirdMain extends Application {
/*Datenbank einlesen*/
MariaDB datenbank = new MariaDB();
/*Panes erstellen und zuweisen*/
@ -29,13 +29,14 @@ public class HappyBirdMain extends Application {
private final Label acronymLabel = new Label("KÜRZEL");
private final Label headline = new Label("HAPPY BIRD");
private final Label soundLabel = new Label("SOUND");
private final Label currentScoreLabel = new Label("SCORE");
private final Label highscoreLabel = new Label("HIGHSCORE");
private final Label currentScoreLabel = new Label("SCORE: ");
private final Label highscoreLabel = new Label("HIGHSCORE: ");
private final Label pauseMenuLabel = new Label("PAUSE");
private final Label soundLabelPause = new Label("SOUND");
private final Label highscoreMenuLabel = new Label();
/*Labels in Arrays speichern*/
private final Label[] labels = {nameLabel, acronymLabel, soundLabel, currentScoreLabel, highscoreLabel};
private final Label[] labels = {nameLabel, acronymLabel, soundLabel, currentScoreLabel, highscoreLabel, highscoreMenuLabel};
private final Label[] headlineLabels = {headline};
/*Buttons erstellen und zuweisen*/
@ -110,6 +111,7 @@ public class HappyBirdMain extends Application {
public void generateMenueScene() {
dataQuery();
menuScreenRunning = true;
/*Bild für Hintergrund erstellen:*/
@ -198,7 +200,8 @@ public class HappyBirdMain extends Application {
*/
public void generateGameScreen() {
menuScreenRunning = false;
int playerhighscore = getPlayerHighscore();
highscoreLabel.setText("HIGHSCORE: " + playerhighscore);
/*Headline-Label umbenennen*/
nameLabel.setText("KÜRZEL: " + acronym.getText());
@ -494,6 +497,16 @@ public class HappyBirdMain extends Application {
public void highscoresButtonClick() {
/*Überschrift umbenennen*/
headline.setText("HIGHSCORES");
/*Label für die Highscores einfügen und füllen*/
highscoreMenuLabel.setLayoutX(275);
highscoreMenuLabel.setLayoutY(225);
highscoreMenuLabel.setPrefHeight(300);
highscoreMenuLabel.setPrefSize(250, 250);
highscoreMenuLabel.setVisible(true);
String bestFiveHighscores = calculateHighscores();
System.out.println(bestFiveHighscores);
highscoreMenuLabel.setText(bestFiveHighscores);
/*nicht verwendete Buttons ausblenden, verwendete einblenden*/
startButton.setVisible(false);
@ -516,7 +529,7 @@ public class HappyBirdMain extends Application {
settingsButton.setVisible(true);
menuButton.setVisible(false);
startGameButton.setVisible(false);
highscoreMenuLabel.setVisible(false);
/*soundSlider ausblenden*/
soundSlider.setVisible(false);
@ -650,4 +663,68 @@ public class HappyBirdMain extends Application {
pane.getChildren().remove(pause);
}
}
/***
* Erste Datenabfrage als Test - wird später gelöscht
*/
public void dataQuery()
{
String dataquery = "SELECT * FROM happybirddb;";
try (ResultSet rs = datenbank.st.executeQuery(dataquery)) {
while (rs.next())
{
System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
}
}
catch (Exception e)
{
System.out.println("Falsche Abfrage");
}
}
/***
* getPlayerHighscore() berechnet den höchsten erreichten Wert des aktuellen Spielers
* (nicht den gesamt höchsten Highscore)
*
* @return den Highscore des aktuellen Spielers
*/
public int getPlayerHighscore()
{
int highscore = 0;
String playeracronym = acronym.getText();
String sql = "SELECT MAX(punkte) FROM happybirddb WHERE kuerzel = '" + playeracronym + "';";
System.out.println(sql);
try (ResultSet rs = datenbank.st.executeQuery(sql)) {
while (rs.next())
{
highscore = rs.getInt(1);
}
}
catch (Exception e)
{
System.out.println("Falsche Abfrage");
}
return highscore;
}
/***
* writeHighscores() gibt die Highscores aus ( die 5 höchsten Werte, die bis jetzt gespielt wurden )
*/
public String calculateHighscores()
{
String bestFiveHighscores = "PLAYER\t\tSCORE\n\n";
int i = 0;
String sql = "SELECT * FROM happybirddb ORDER BY punkte DESC LIMIT 5;";
try (ResultSet rs = datenbank.st.executeQuery(sql)) {
while (rs.next())
{
bestFiveHighscores+= rs.getString(4) + "\t" + rs.getString(3)+"\n";
}
}
catch (Exception e)
{
System.out.println("Falsche Abfrage");
}
return bestFiveHighscores;
}
}