Untitled
package application; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import javafx.animation.AnimationTimer; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.VPos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.util.Duration; public class Main extends Application { //Fields/Global Variables private Pane root; private Title title; private int level; private String[][] scores; private Level1 level1; private Timeline countdown; private ArrayList<Objects> objects; private AnimationTimer objectMove; private Timeline objectSpawn; private Timeline powerup; private AnimationTimer bgTimer; //This timer moves the background private Timeline flipTimer; //This timer actually rotates the background private Button btnFlip; private ArrayList<LightBeam> lightBeam; private Timeline spawnBeam; private int beamDuration; private Player player; private AnimationTimer playerTimer; //This timer moves the player private boolean up, down, left, right; //**Never go backwards Alert, theres no escaping //**Each life earns you 30 seconds off in overall time at the end //** Door at the end (stop the objectSpawn timer, but not the objectMove timer) public void start(Stage primaryStage) { try { root = new Pane(); root.setStyle("-fx-background-color:white"); Scene scene = new Scene(root, 1500, 750); primaryStage.setScene(scene); primaryStage.setTitle("Game"); primaryStage.setResizable(false); //This makes sure the user cannot resize the Stage/Window primaryStage.show(); level = 3; title = new Title(root.getWidth(), root.getHeight()); player = new Player(); level1 = new Level1(); scores = new String[1000][2]; GridPane grid = new GridPane(); grid.setGridLinesVisible(true); grid.setPadding(new Insets(15, 10, 15, 10)); //Set a Padding around the ENTIRE GridPane grid.setHgap(10); //Create a horizontal gap between each column grid.setVgap(15); //Create a vertical gap between each row ColumnConstraints col1 = new ColumnConstraints(450); //This sets a ColumnConstraint, meaning that the entire column has be to a certain size grid.getColumnConstraints().addAll(col1, col1, col1); //Add the column constraints to the GridPane layout grid.add(title.getTitle(), 0, 0); GridPane.setColumnSpan(title.getTitle(), 3); GridPane.setHalignment(title.getTitle(), HPos.CENTER); grid.add(title.getName(), 0, 1); GridPane.setHalignment(title.getName(), HPos.RIGHT); grid.add(title.getTFName(), 1, 1); //GridPane.setColumnSpan(title.getTFName(), 2); GridPane.setHalignment(title.getTFName(), HPos.CENTER); grid.add(title.getBtnScore(), 0, 2); GridPane.setColumnSpan(title.getBtnScore(), 3); GridPane.setHalignment(title.getBtnScore(), HPos.CENTER); //0,3 grid.add(title.getLblScore(), 0, 3); GridPane.setColumnSpan(title.getLblScore(), 3); GridPane.setHalignment(title.getLblScore(), HPos.CENTER); //grid.add(title.getRad1(), 0, 3); grid.add(title.getRad1(), 0, 4); GridPane.setHalignment(title.getRad1(), HPos.CENTER); grid.add(title.getRad2(), 2, 4); GridPane.setHalignment(title.getRad2(), HPos.CENTER); grid.add(title.getBox(), 1, 4); //GridPane.setRowSpan(title.getBox(), 2); GridPane.setHalignment(title.getBox(), HPos.CENTER); GridPane.setValignment(title.getBox(), VPos.CENTER); grid.add(title.getControls(), 0, 5); GridPane.setHalignment(title.getControls(), HPos.CENTER); grid.add(title.getPlay(), 1, 5); GridPane.setHalignment(title.getPlay(), HPos.CENTER); grid.add(title.getExit(), 2, 5); GridPane.setHalignment(title.getExit(), HPos.CENTER); Platform.runLater(new Runnable() { //This ensures the program has completely computed the size of the GridPane with all the contents in it, so that the program can give the size of the entire GridPane public void run() { grid.setLayoutX(root.getWidth() / 2 - grid.getWidth() / 2); grid.setLayoutY(root.getHeight() / 2 - grid.getHeight() / 2); } }); //root.getChildren().add(grid); //Level 3 beamDuration = 5; lightBeam = new ArrayList<LightBeam>(); KeyFrame kfBeam = new KeyFrame(Duration.seconds(beamDuration), new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { lightBeam.add(new LightBeam(root.getWidth(), root.getHeight())); root.getChildren().add(lightBeam.get(lightBeam.size() - 1).getLightBeam()); } }); spawnBeam = new Timeline(kfBeam); spawnBeam.setCycleCount(7); title.getBtnScore().setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { String line; //File file = new File("TestScores.txt"); //This gets the text file //FileReader input = new FileReader(textFile); //This creates an input stream for the text file try { BufferedReader readFile = new BufferedReader(new FileReader(new File("scores.txt"))); //This allows to actually read data from the input stream, which is pointed to the text file int rowCounter = 0; //If the line of text is NOT Null, run the While Loop while((line = readFile.readLine()) != null) { scores[rowCounter] = line.split(" , "); //This splits the line of text into two parts: the name and their score, and stores it in each row of the 2D array rowCounter++; title.getLblScore().setText(bubbleSort()); //Call the Sorting Algorithm to display the user's high score/best time //**The file is always written with the user's name, a space, a comma, another space, and their score //**The first column in the "scores" 2D array is always the name, while the second column is always their score } readFile.close(); } catch (Exception f) { f.printStackTrace(); } } }); //root.getChildren().addAll(level1.getBg1(), level1.getBg2()); //Scene scene = new Scene(root, level1.getBg1().getImage().getWidth() , level1.getBg1().getImage().getWidth()); //Square level1.getLblLives().setLayoutX(20); level1.getLblLives().setLayoutY(10); root.getChildren().add(level1.getLblLives()); level1.getLblTime().setLayoutX(400); level1.getLblTime().setLayoutY(10); root.getChildren().add(level1.getLblTime()); KeyFrame kfCountdown = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { level1.decreaseTime(); level1.getLblTime(); if (level1.getMinutes() == 0 && level1.getSeconds() == 0) { playerTimer.stop(); bgTimer.stop(); objectMove.stop(); objectSpawn.stop(); countdown.stop(); } } }); countdown = new Timeline(kfCountdown); countdown.setCycleCount(Timeline.INDEFINITE); objects = new ArrayList<Objects>(); KeyFrame kfObjectSpawn = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { objects.add(new Objects(level1.getBg1().getImage().getHeight(), root.getWidth())); objects.get(objects.size() - 1).setX(level1.getFlipped()); objects.get(objects.size() - 1).setY(level1.getFlipped()); //root.getChildren().add(objects.get(objects.size() - 1).getNode()); } }); objectSpawn = new Timeline(kfObjectSpawn); objectSpawn.setCycleCount(Timeline.INDEFINITE); KeyFrame kfPowerup = new KeyFrame(Duration.seconds(40), new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { level1.setPowerupX(root.getWidth(), level1.getBg1().getImage().getHeight()); level1.setPowerupY(root.getHeight(), level1.getBg1().getImage().getHeight()); //root.getChildren().add(level1.getPowerup()); powerup.stop(); } }); powerup = new Timeline(kfPowerup); powerup.setCycleCount(Timeline.INDEFINITE); objectMove = new AnimationTimer() { public void handle(long val) { if (level == 1) { for (int i = 0; i < objects.size(); i++) { if (level1.getFlipped() == true) { objects.get(i).moveY(); } else { objects.get(i).moveX(); } if (objects.get(i).getNode().getBoundsInParent().intersects(player.getNode().getBoundsInParent())) { // root.getChildren().remove(objects.get(i).getNode()); // objects.remove(i); level1.decreaseLives(); level1.getLblLives(); if (level1.getFlipped() == true) { player.setY(player.getY() + 30); //Send the player a little backwards } else { player.setX(player.getX() - 70); //Send the player a little backwards } //**Sound effects for getting hit (OW!) -> randomized if (level1.getLives() == 0) { System.exit(0); } } else if (objects.get(i).getX() + objects.get(i).getNode().getWidth() < 0 || objects.get(i).getY() > root.getHeight()) { // root.getChildren().remove(objects.get(i).getNode()); // objects.remove(i); } } //Power-Up if (level1.getFlipped() == true) { level1.powerMoveY(); } else { level1.powerMoveX(); } if (level1.getPowerup().getBoundsInParent().intersects(player.getNode().getBoundsInParent())) { // root.getChildren().remove(level1.getPowerup()); // powerup.play(); } } else if (level == 3) { for (int i = 0; i < lightBeam.size(); i++) { lightBeam.get(i).moveLightBeam(); System.out.println(lightBeam.get(0).getStartX() + " nn " + lightBeam.get(0).getEndX()); if (lightBeam.get(i).getLightBeam().getBoundsInParent().intersects(player.getNode().getBoundsInParent())) { //decrease health bar //HAVE HEALTH BAR AND MAKE IT a Label, and decrease its size when the player loses it } else if (lightBeam.get(i).getEndX() < 0) { root.getChildren().remove(lightBeam.get(i).getLightBeam()); lightBeam.remove(i); } } } } }; bgTimer = new AnimationTimer() { public void handle(long val) { if (level1.getFlipped() == true) //If the background has flipped { level1.moveY(up, down, root.getHeight()); } else //If the background has not flipped { level1.moveX(left, right, root.getWidth()); if (level1.getCount() == 5) { bgTimer.stop(); //Stop all the timers to stop moving everything playerTimer.stop(); objectMove.stop(); objectSpawn.stop(); powerup.stop(); level1.setFlipped(); //Sets "flipped" to True flipTimer.play(); //Start this timer to rotate the backgrounds } } } }; btnFlip = new Button("hmm..."); btnFlip.setLayoutX(400); btnFlip.setLayoutY(10); btnFlip.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { bgTimer.stop(); //Stop all the timers to stop moving everything playerTimer.stop(); objectMove.stop(); objectSpawn.stop(); level1.setFlipped(); //Sets "flipped" to True flipTimer.play(); //Start this timer to rotate the backgrounds } }); //root.getChildren().add(btnFlip); //player = new Player(); player.setWindowSize(root.getWidth(), root.getHeight()); root.getChildren().add(player.getNode()); up = false; down = false; left = false; right = false; playerTimer = new AnimationTimer() { public void handle(long val) { if (level == 1) { if (level1.getFlipped() == true) //If the background has been flipped, the player can move horizontally { player.moveX(left, right, level1.getBg1().getImage().getHeight()); } else //Otherwise, the player can move vertically { player.moveY(up, down, level1.getBg1().getImage().getHeight()); } } else if (level == 3) { player.move(up, down, left, right, root.getWidth(), root.getHeight()); } player.getNode().requestFocus(); } }; KeyFrame kfTimer = new KeyFrame(Duration.millis(100), new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { level1.rotate(); if (level1.getDegrees() == 90) //If it reaches 90 degrees { flipTimer.stop(); //Stop this timer to stop rotating //Set the player's position to the bottom of the Scene and align horizontally player.setX(player.getX() + (level1.getBg1().getImage().getHeight() / 2) + (player.getNode().getWidth() / 2)); player.setY(root.getHeight() - player.getNode().getHeight() - 70); //30 is a guess and check value level1.setBg1X(level1.getBg1X() + 1); //Increase both X positions by 1 so that the If Statements below can accurately check their conditions, cuz the x-value of the first background could still meet the first condition even though the 2nd background is displayed, or vice versa level1.setBg2X(level1.getBg2X() + 1); if (level1.getBg1X() >= 0 && level1.getBg1X() <= root.getWidth()) //If the first background is the one displayed on the Scene { //Reset the position of the second background to be above the first background (out of the Window) level1.setBg2X(level1.getBg1X()); level1.setBg2Y(level1.getBg1Y() - level1.getBg1().getImage().getWidth()); } else if (level1.getBg2X() >= 0 && level1.getBg2X() <= root.getWidth()) //If the second background is the one displayed on the Scene { //Reset the position of the first background to be above the second background (out of the Window) level1.setBg1X(level1.getBg2X()); level1.setBg1Y(level1.getBg2Y() - level1.getBg2().getImage().getWidth()); } playerTimer.start(); //Start all the timers again to start moving bgTimer.start(); objectMove.start(); objectSpawn.play(); powerup.play(); } } }); flipTimer = new Timeline(kfTimer); flipTimer.setCycleCount(Timeline.INDEFINITE); //This timer should run forever, until manually stopped scene.setOnKeyPressed(e -> keyPressed(e)); //Lamba Expressions to call the "keyPressed" and "keyReleased" methods, which control keyboard input scene.setOnKeyReleased(e -> keyReleased(e)); //Start all the timers playerTimer.start(); // bgTimer.start(); // // objectSpawn.play(); // powerup.play(); objectMove.start(); // // countdown.play(); spawnBeam.play(); } catch(Exception e) { e.printStackTrace(); } } public void keyPressed(KeyEvent e) { //Check if the Right or Left arrow key has been pressed (X-direction) if (e.getCode() == KeyCode.RIGHT) { right = true; } else if (e.getCode() == KeyCode.LEFT) { left = true; } //Check if the Up or Down arrow key has been pressed (Y-direction) if (e.getCode() == KeyCode.UP) { up = true; } else if (e.getCode() == KeyCode.DOWN) { down = true; } //Make the X and Y directions into separate if-statements so that the player can move diagonally (both in the X and Y direction at the same time) } public void keyReleased(KeyEvent e) { //Check if the Right or Left arrow key has been pressed (X-direction) if (e.getCode() == KeyCode.RIGHT) { right = false; } else if (e.getCode() == KeyCode.LEFT) { left = false; } //Check if the Up or Down arrow key has been pressed (Y-direction) if (e.getCode() == KeyCode.UP) { up = false; } else if (e.getCode() == KeyCode.DOWN) { down = false; } //Make the X and Y directions into separate if-statements so that the player can move diagonally (both in the X and Y direction at the same time) } public static void main(String[] args) { launch(args); } public String bubbleSort() { ArrayList<Integer> points = new ArrayList<Integer>(); //ArrayList to store all the person's scores for (int i = 0; i < scores.length; i++) //"i" is the index counter for each ROW in the 2D array { if (scores[i][0] != null && scores[i][0].equalsIgnoreCase(title.getTFName().getText())) //Check if the name the user entered equals any of the names in the file, which is already stored in the "scores" 2D array { points.add(Integer.parseInt(scores[i][1])); } } //**The first column (index 0) in the "scores" 2D array is always the name, while the second column (index 1) is always their score if (points.isEmpty()) //If the ArrayList is empty, then that means the user is a new player and thus, their scores are not in the file { return "New user detected"; } else //If the ArrayList is NOT empty, then that means the user already has data stored in the file, so sort it and find the highest score { //Bubble Sort Algorithm boolean done = false; for (int end = points.size() - 1; end > 0; end--) //"end" keeps track of the index at the end of the ArrayList, and decreases because after each Pass/iteration, the largest number will always be at the end, so there will be no need to check until the very end (just check until right before the previous "end" value) { done = true; //Assume that the ArrayList is already sorted from Least to Greatest for (int i = 0; i < end; i++) //For Loop to go through the ArrayList until the "end" value { if (points.get(i) > points.get(i + 1)) //If the number at index "i" is greater than the number to the right of it, then swap (cuz u want the smallest number first) { done = false; //Make it false to run the outer For Loop again to make sure everything else is sorted //Swap elements int temp = points.get(i); //Store the number at index "i" in a temporary variable points.set(i, points.get(i + 1)); //Replace the number at index "i" with the number to the right of index "i" points.set(i + 1, temp); //Replace the number to the right of index "i" with the temporary variable, which stores the previous number at index "i" } } //"i" increases by 1 if (done == true) //If "done" is true, then that means all the elements in the ArrayList have been sorted from Least to Greatest break; } return Integer.toString(points.get(points.size() - 1)); //This returns the score in the last index of the "points" ArrayList, which is the GREATEST score } } }
Leave a Comment