Untitled
package application; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.Objects; import java.util.Random; 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.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; 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.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class Main extends Application { private Player player1; private boolean left, right , up , down, interact, interactPopup, safe, bview, openInv, useScrewDriver, lobby, doors, key2; private String interactType; private AnimationTimer aniPlayer; private double mouseAnchorX, mouseAnchorY; private Button exitPop; private Pane root; private Rectangle closet, overlay, interactbg, safebox, invDragBox, mini1, mini2, mini3, door2, safeOpen; private Image imgLetter1, imgLetter2, imgLetter3, imgLetter4; private ImageView letter1, letter2, letter3, letter4, screwdriver, key; private int totalLetters; private long lastView = 0; private final long VIEW_COOLDOWN = 1000000000; // 500 ms in nanoseconds private int [] rowAns, rowDigit; private ArrayList invItems; private Button row1up, row2up, row3up, row4up, row1down, row2down, row3down, row4down, safeEnterButton; private GridPane gridPane; private Label digit1, digit2, digit3, digit4, lbl; private Random rnd; private Scene scene; private ImageView ivBg, ivCloset; //Fields/Global Variables private Title title; private int level; private String[][] scores; private Level1 level1; private Timeline countdown; private ArrayList<Object> 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 Player2 player2; private AnimationTimer playerTimer; //This timer moves the player //**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(); scene = new Scene(root, 700, 700); // 1550 820 player1 = new Player(); player1.setLocation(200, scene.getHeight() / 2 + 100); //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 = 1; title = new Title(root.getWidth(), root.getHeight()); player2 = new Player2(); 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<Object>(); KeyFrame kfObjectSpawn = new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { objects.add(new Object(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(player2.getNode().getBoundsInParent())) { root.getChildren().remove(objects.get(i).getNode()); objects.remove(i); level1.decreaseLives(); level1.getLblLives(); if (level1.getFlipped() == true) { player2.setY(player2.getY() + 30); //Send the player a little backwards } else { player2.setX(player2.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(player2.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(player2.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(); player2.setWindowSize(root.getWidth(), root.getHeight()); root.getChildren().add(player2.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 { player2.moveX(left, right, level1.getBg1().getImage().getHeight()); } else //Otherwise, the player can move vertically { player2.moveY(up, down, level1.getBg1().getImage().getHeight()); } } else if (level == 3) { player2.move(up, down, left, right, root.getWidth(), root.getHeight()); } player2.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 player2.setX(player2.getX() + (level1.getBg1().getImage().getHeight() / 2) + (player2.getNode().getWidth() / 2)); player2.setY(root.getHeight() - player2.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(); rnd = new Random(); rowDigit = new int [4]; for (int i = 0; i < 4; i++) { rowDigit[i] = 1; System.out.print(rowDigit[i]); } System.out.println(); rowAns = new int [4]; for (int i = 0; i < 4; i++) { rowAns[i] = rnd.nextInt(1, 5); System.out.print(rowAns[i]); } System.out.println(); invItems = new ArrayList<ImageView>(); totalLetters = 0; ivBg = new ImageView("file:Lobby.png"); lobby = true; Rectangle drawer1 = new Rectangle(750, scene.getHeight() / 2 + 100, 150, 100); drawer1.setFill(Color.GRAY); Rectangle drawer2 = new Rectangle(500, scene.getHeight() / 2 + 100, 150, 100); drawer1.setFill(Color.GRAY); Rectangle closet = new Rectangle(1450, 600, 100, 100); closet.setFill(Color.RED); Rectangle vent = new Rectangle (1020, 210, 100, 50); vent.setFill(Color.TRANSPARENT); Rectangle stairs = new Rectangle (scene.getWidth() / 2 - 100, scene.getHeight() / 2 + 100, 200, 100); // // safeOpen = new Rectangle(scene.getWidth() / 2 - 250, scene.getHeight() / 2 - 250, 500,500); // safeOpen.setFill(Color.GREY); // // interactbg = new Rectangle(scene.getWidth() /2 - 500, scene.getHeight()/ 2 - 250, 1000, 500); // interactbg.setFill(Color.rgb(200, 200, 200)); // // mini1 = new Rectangle(scene.getWidth() / 2 + 250, scene.getHeight() / 2 + 50, 100, 50); // mini2 = new Rectangle(scene.getWidth() / 2 - 50, scene.getHeight() / 2 + 50, 100, 50); // mini3 = new Rectangle(scene.getWidth() / 2 - 350, scene.getHeight() / 2 + 50, 100, 50); // // door2 = new Rectangle (100, scene.getHeight() /2, 100,100); // // letter1 = new ImageView("file:LetterOne"+ rowAns[0] +".png"); // letter1.setPreserveRatio(true); // letter1.setFitWidth(100); // letter1.setX(900); // letter1.setY(400); // // letter2 = new ImageView("file:LetterTwo"+ rowAns[1] +".png"); // letter2.setPreserveRatio(true); // letter2.setFitWidth(100); // letter2.setX(900); // letter2.setY(400); // // letter3 = new ImageView("file:LetterThree"+ rowAns[2] +".png"); // letter3.setPreserveRatio(true); // letter3.setFitWidth(100); // letter3.setX(640); // letter3.setY(145); // // letter4 = new ImageView("file:LetterFour"+ rowAns[3] +".png"); // letter4.setPreserveRatio(true); // letter4.setFitWidth(100); // letter4.setX(700); // letter4.setY(300); // // screwdriver = new ImageView("file:image-removebg-preview.png"); // screwdriver.setPreserveRatio(true); // screwdriver.setFitWidth(100); // screwdriver.setX(600); // screwdriver.setY(300); // // key = new ImageView("file:key.png"); // key.setPreserveRatio(true); // key.setFitWidth(100); // key.setX(scene.getWidth() /2); // key.setY(scene.getHeight() /2); // // // overlay = new Rectangle(0,0, scene.getWidth(), scene.getHeight()); // overlay.setFill(Color.TRANSPARENT); // // safebox = new Rectangle(scene.getWidth() /2 + 100, scene.getHeight()/ 2 + 130, 125, 135); // safebox.setFill(Color.rgb(0,0,0,0.5)); // // invDragBox = new Rectangle(scene.getWidth() /2 - 450, scene.getHeight()/ 2 - 50, 900, 250); // invDragBox.setFill(Color.rgb(220, 220, 220)); // // // // scene.setOnKeyPressed(new EventHandler<KeyEvent>(){ // public void handle(KeyEvent e) { // if(e.getCode() == KeyCode.D) { // right = true; // } // if(e.getCode() == KeyCode.A) { // left = true; // } // if( e.getCode() == KeyCode.W) { // up = true; // } // if(e.getCode() == KeyCode.S) { // down = true; // } // if(interactPopup && e.getCode() == KeyCode.E) { // interact = true; // } // if (e.getCode() == KeyCode.Q) { // openInv = true; // } // // } // }); // // scene.setOnKeyReleased(new EventHandler<KeyEvent>(){ // public void handle(KeyEvent e) { // if(e.getCode() == KeyCode.D) { // right = false; // } // if(e.getCode() == KeyCode.A) { // left = false; // } // if(e.getCode() == KeyCode.W) { // up = false; // } // if(e.getCode() == KeyCode.S) { // down = false; // } // // } // }); // // // // // // // Label popuplbl = new Label(); // Line rBoundary = new Line(root.getWidth(), 400, root.getWidth(), scene.getHeight()); // Line lBoundary = new Line(0, scene.getHeight(), 0, 400); // Line tBoundary = new Line(0, 430, scene.getWidth(), 430); // // digit1 = new Label("1"); // digit2 = new Label("1"); // digit3 = new Label("1"); // digit4 = new Label("1"); // // exitPop = new Button("EXIT"); // exitPop.setLayoutX(scene.getWidth() / 2 - 20); // exitPop.setLayoutY(790); // exitPop.setOnAction(e -> exitPopbut()); // // root.getChildren().addAll(ivBg, player1.getNode(), stairs, mini1,mini2,mini3); // // // // aniPlayer = new AnimationTimer() { // public void handle (long now) { // if(right && !rBoundary.getBoundsInParent().intersects(player1.getBounds())) { // player1.moveEast(); // } // if(left && !lBoundary.getBoundsInParent().intersects(player1.getBounds())) { // player1.moveWest(); // } // if(up) { // player1.moveUp(); // } // if(down) { // // player1.moveDown(); // // } // player1.getNode(); // // if (lobby && player1.getNode().getBoundsInParent().intersects(stairs.getBoundsInParent()) && player1.getFitHeight() < 180) { // ivBg.setImage(new Image("file:3 Door Lobby.png")); // lobby = false; // doors = true; // // // // root.getChildren().remove(stairs); // // player1.setFitHeight(255); // player1.setLocation(scene.getWidth() / 2 - player1.getWidth() /2, 820 - player1.getFitHeight()); // // } // // if (doors && player1.getNode().getBoundsInParent().intersects(mini1.getBoundsInParent()) && player1.getFitHeight() < 180) { // /*START MINI GAME 1*/ // } // if (doors && player1.getNode().getBoundsInParent().intersects(mini3.getBoundsInParent()) && player1.getFitHeight() < 180) { // /*START MINI GAME 3*/ // } // // if (!key2 && doors && player1.getNode().getBoundsInParent().intersects(mini2.getBoundsInParent()) && player1.getFitHeight() < 180) { // ivBg.setImage(new Image("file:escapebg.png")); // doors = false; // player1.setLocation(200, scene.getHeight() / 2 + 100); // // root.getChildren().addAll(popuplbl, vent, closet, door2); // root.getChildren().removeAll(mini1, mini2, mini3); // } // // // if ( key2 && player1.getNode().getBoundsInParent().intersects(door2.getBoundsInParent())) { // ivBg.setImage(new Image("file:3 Door Lobby.png")); // doors = true; // // player1.setLocation(scene.getWidth() / 2 - player1.getWidth() , player1.getHeight() / 2 + 430); // player1.setFitHeight(170); // root.getChildren().removeAll(popuplbl, vent, closet, door2, mini2); // // } // // if(!bview && player1.getNode().getBoundsInParent().intersects(drawer1.getBoundsInParent()) && player1.getFitHeight() < 180) { // interactPopup = true; // interactType = "DRAWER1"; // // } // else if (player1.getNode().getBoundsInParent().intersects(drawer2.getBoundsInParent()) && player1.getFitHeight() < 180) { // interactPopup = true; // interactType = "DRAWER2"; // } // else if (!bview && player1.getNode().getBoundsInParent().intersects(closet.getBoundsInParent())) { // interactPopup = true; // interactType = "CLOSET"; // } // else { // interactPopup = false; // interactType = ""; // } // // if (interactPopup) { // // popuplbl.setPrefHeight(100); // popuplbl.setStyle("-fx-background-color: orange"); // popuplbl.setLayoutX(scene.getWidth() / 2 - popuplbl.getWidth() / 2); // popuplbl.setLayoutY(70); // popuplbl.setText("INTERACT WITH 'E'"); // } // else { // popuplbl.setStyle("-fx-background-color: transparent"); // popuplbl.setText(""); // } // // if (interact) { // aniPlayer.stop(); // popuplbl.setStyle("-fx-background-color: transparent"); // popuplbl.setText(""); // overlay.setFill(Color.rgb(0,0,0,0.6)); // // root.getChildren().addAll(overlay, interactbg); // // if(interactType == "CLOSET") { // root.getChildren().remove(interactbg); // // ivCloset = new ImageView(new Image("file:Untitled design.png")); // ivCloset.setX(scene.getWidth() / 2 - 250); // ivCloset.setY(scene.getHeight() / 2 - 300); // // root.getChildren().addAll(ivCloset, safebox); // safebox.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // safe = true; // safeMode(); // } // }); // // if (!invItems.contains(letter3)) { // root.getChildren().add(letter3); // } // if (!openInv) { // letter3.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (!openInv) { // addItemInv(letter3); // // } // } // }); // } // } // // else if (interactType == "DRAWER1") { // // if (!invItems.contains(letter1)) { // root.getChildren().add(letter1); // } // if (!invItems.contains(screwdriver) && !useScrewDriver) { // // root.getChildren().add(screwdriver); // } // if (!openInv) { // letter1.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (!openInv) { // addItemInv(letter1); // // } // } // }); // // screwdriver.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (!openInv) { // addItemInv(screwdriver); // // } // } // }); // } // // } // else if (interactType == "DRAWER2") { // // if (!invItems.contains(letter2)) { // root.getChildren().add(letter2); // } // // letter2.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (!openInv) { // addItemInv(letter2); // } // } // }); // // } // // root.getChildren().add(exitPop); // } // // vent.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (useScrewDriver) { // interact = true; // interactType = "VENT"; // aniPlayer.stop(); // overlay.setFill(Color.rgb(0,0,0,0.6)); // // root.getChildren().addAll(overlay, interactbg, exitPop); // // if (!invItems.contains(letter4)) { // root.getChildren().add(letter4); // } // // letter4.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (!openInv) { // addItemInv(letter4); // } // } // }); // } // } // }); // // // // // // Switch view // // if (player.getFitHeight() > 260) { // // if (now - lastView >= VIEW_COOLDOWN) { // // if (!bview) { // // bview = true; // // root.getChildren().removeAll(drawer1, closet); // // } // // else { // // bview = false; // // // // root.getChildren().addAll(drawer1, closet); // // } // // lastView = now; // // } // // } // // // key.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // if (!openInv) { // key2 = true; // root.getChildren().remove(key); // } // } // }); // // // // if (openInv) { // aniPlayer.stop(); // overlay.setFill(Color.rgb(0,0,0,0.6)); // // root.getChildren().addAll(overlay, interactbg, invDragBox, exitPop); // // for (int i = 0; i < invItems.size(); i++) { // root.getChildren().add((Node)invItems.get(i)); // // // } // // letter1.setOnMousePressed(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // mouseAnchorX = e.getX(); // mouseAnchorY = e.getY(); // // } // }); // letter1.setOnMouseDragged(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // letter1.setLayoutX(e.getSceneX() - mouseAnchorX); // letter1.setLayoutY(e.getSceneY() - mouseAnchorY); // // } // }); // // letter2.setOnMousePressed(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // // mouseAnchorX = e.getX(); // mouseAnchorY = e.getY(); // // } // }); // letter2.setOnMouseDragged(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // letter2.setLayoutX(e.getSceneX() - mouseAnchorX); // letter2.setLayoutY(e.getSceneY() - mouseAnchorY); // // } // }); // // letter3.setOnMousePressed(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // // mouseAnchorX = e.getX(); // mouseAnchorY = e.getY(); // // } // }); // letter3.setOnMouseDragged(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // letter3.setLayoutX(e.getSceneX() - mouseAnchorX); // letter3.setLayoutY(e.getSceneY() - mouseAnchorY); // // } // }); // // letter4.setOnMousePressed(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // // mouseAnchorX = e.getX(); // mouseAnchorY = e.getY(); // // } // }); // letter4.setOnMouseDragged(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // letter4.setLayoutX(e.getSceneX() - mouseAnchorX); // letter4.setLayoutY(e.getSceneY() - mouseAnchorY); // // } // }); // // screwdriver.setOnMouseClicked(new EventHandler<MouseEvent> () { // // public void handle(MouseEvent e) { // root.getChildren().remove(screwdriver); // useScrewDriver = true; // invItems.remove(screwdriver); // // } // }); // // // // letter1.setOnMouseDragReleased(new EventHandler<MouseEvent> () { // // // // public void handle(MouseEvent e) { // // letter1.setLayoutX(e.getSceneX() - mouseAnchorX); // // letter1.setLayoutY(e.getSceneY() - mouseAnchorY); // // // // } // // }); // } // } // // // }; // aniPlayer.start(); // // // // // // row1up = new Button("<"); // row1up.setOnAction(e -> safedigit(1, false)); // // row1down = new Button(">"); // row1down.setOnAction(e -> safedigit(1, true)); // // row2up = new Button("<"); // row2up.setOnAction(e -> safedigit(2, false)); // // row2down = new Button(">"); // row2down.setOnAction(e -> safedigit(2, true)); // // row3up = new Button("<"); // row3up.setOnAction(e -> safedigit(3, false)); // // row3down = new Button(">"); // row3down.setOnAction(e -> safedigit(3, true)); // // row4up = new Button("<"); // row4up.setOnAction(e -> safedigit(4, false)); // // row4down = new Button(">"); // row4down.setOnAction(e -> safedigit(4, true)); // // safeEnterButton = new Button("ENTER"); // safeEnterButton.setOnAction(e -> safeEnter()); // // lbl = new Label("Hello"); // // gridPane = new GridPane(); // gridPane.setStyle("-fx-background-color: grey"); // gridPane.setHgap(10); // gridPane.setVgap(10); // gridPane.setPrefSize(500, 500); // gridPane.setAlignment(Pos.CENTER); // // gridPane.add(row1up, 0, 0); // row1up.setPadding(new Insets(30, 30, 30,30)); // gridPane.add(digit1, 1, 0); // digit1.setPadding(new Insets(30, 70, 30,70)); // gridPane.add(row1down, 2, 0); // row1down.setPadding(new Insets(30, 30, 30,30)); // // gridPane.add(row2up, 0, 1); // row2up.setPadding(new Insets(30, 30, 30,30)); // gridPane.add(digit2, 1, 1); // digit2.setPadding(new Insets(30, 70, 30,70)); // gridPane.add(row2down, 2, 1); // row2down.setPadding(new Insets(30, 30, 30,30)); // // gridPane.add(row3up, 0, 2); // row3up.setPadding(new Insets(30, 30, 30,30)); // gridPane.add(digit3, 1, 2); // digit3.setPadding(new Insets(30, 70, 30,70)); // gridPane.add(row3down, 2, 2); // row3down.setPadding(new Insets(30, 30, 30,30)); // // // gridPane.add(row4up, 0, 3); // row4up.setPadding(new Insets(30, 30, 30,30)); // gridPane.add(digit4, 1, 3); // digit4.setPadding(new Insets(30, 70, 30,70)); // gridPane.add(row4down, 2, 3); // row4down.setPadding(new Insets(30, 30, 30,30)); // // // gridPane.setColumnSpan(safeEnterButton, 3); // gridPane.setHalignment(safeEnterButton, HPos.CENTER); // gridPane.add(safeEnterButton, 0, 4); // // gridPane.setColumnSpan(lbl, 3); // gridPane.setHalignment(lbl, HPos.CENTER); // gridPane.add(lbl, 0, 5); // // // gridPane.setLayoutX(root.getWidth() /2 -250); // gridPane.setLayoutY(root.getHeight() /2- 250); // gridPane.setGridLinesVisible(true); // // // } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } public void keyPressed(KeyEvent e) { //Check if the Right or Left arrow key has been pressed (X-direction) if (e.getCode() == KeyCode.D) { right = true; } else if (e.getCode() == KeyCode.A) { left = true; } //Check if the Up or Down arrow key has been pressed (Y-direction) if (e.getCode() == KeyCode.W) { up = true; } else if (e.getCode() == KeyCode.S) { 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.D) { right = false; } else if (e.getCode() == KeyCode.A) { left = false; } //Check if the Up or Down arrow key has been pressed (Y-direction) if (e.getCode() == KeyCode.W) { up = false; } else if (e.getCode() == KeyCode.S) { 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 void exitPopbut () { interact = false; aniPlayer.start(); root.getChildren().removeAll(ivCloset, exitPop, overlay, letter1, letter2, letter3, letter4, screwdriver, safebox, interactbg, safeOpen, key); interactType = ""; if (openInv) { root.getChildren().remove(invDragBox); for (int i = 0; i < invItems.size(); i++) { root.getChildren().remove((Node)invItems.get(i)); } openInv = false; } if (safe) { root.getChildren().removeAll(gridPane); safe = false; } } public void safeMode() { root.getChildren().removeAll(safebox, interactbg); root.getChildren().addAll(gridPane); } public void safedigit(int row, boolean up) { if (up) { if (rowDigit[row -1] < 4) { rowDigit[row -1]++; } else { rowDigit[row-1] = 1; } } else{ if (rowDigit[row -1] > 1) { rowDigit[row -1]--; } else { rowDigit[row-1] = 4; } } if(row == 1) { digit1.setText(Integer.toString(rowDigit[row -1])); } if(row == 2) { digit2.setText(Integer.toString(rowDigit[row -1])); } if(row == 3) { digit3.setText(Integer.toString(rowDigit[row -1])); } if(row == 4) { digit4.setText(Integer.toString(rowDigit[row -1])); } } public void safeEnter() { int counter = 0; for (int i = 0; i < rowAns.length; i++) { if (rowAns[i] == rowDigit[i]) { counter ++; } } if (counter == 4) { lbl.setText("CORRECT!"); root.getChildren().remove(gridPane); root.getChildren().addAll(safeOpen); if (!key2) { root.getChildren().add(key); } } else { lbl.setText("WRONG! You have " + counter + " digits correct."); } } public void addItemInv(ImageView item) { invItems.add(item); //POSITION THE ITEM IN INV HERE if (item != screwdriver) { totalLetters++; item.setX((scene.getWidth() /2 - 450) + (totalLetters * 150)); } else { item.setX(scene.getWidth() /2 - 400); } item.setY(scene.getHeight()/ 2 + 25); root.getChildren().remove(item); } 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