Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Math.Game1;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.List;
- import javafx.animation.*;
- import javafx.util.*;
- import java.util.*;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.input.DragEvent;
- import static java.time.Clock.system;
- import javafx.animation.KeyFrame;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Cursor;
- import javafx.scene.Scene;
- import javafx.scene.Group;
- import javafx.scene.Node;
- import javafx.scene.control.Button;
- import javafx.scene.control.*;
- import javafx.scene.input.InputMethodEvent;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.StackPane;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- import javafx.scene.shape.Line;
- import javafx.scene.shape.Shape;
- import javafx.scene.shape.Circle;
- import javafx.scene.text.Text;
- import javafx.scene.paint.Color;
- import javafx.scene.paint.Paint;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.text.Font;
- import javafx.stage.Modality;
- import javafx.util.Duration;
- import static sun.net.www.http.HttpClient.New;
- /**
- *
- * @author Craig
- */
- public class MathGame1 extends Application {
- private static final int CIRCLES_NUMBER = 5;
- private static final int CIRCLE_RADIUS = 50;
- private static final double WINDOW_WIDTH = 600;
- private static final double WINDOW_HEIGHT = 400;
- private static final double LINE_OFFSET = 50;
- private static final String CIRCLE_ID = "Circle";
- private static final String TEXT_ID = "Text";
- private static final String LINE_ID = "Line";
- int clockTime;
- //Customizable Cirlce Strings
- int C0;
- int C1;
- int C2;
- int C3;
- int C4;
- ArrayList<String> listOfStrings;
- boolean level_1;
- boolean level_2;
- boolean level_3;
- boolean countDown;
- boolean timerOn;
- Timeline timeLine;
- Scene startScene;
- Scene editScene;
- Scene gameScene;
- Scene instrucScene1;
- Scene game1Scene;
- Scene game2Scene;
- Scene instructScene2;
- Scene gameModeSelect;
- //Start Screen
- Stage stage;
- //Editing Screen
- Stage stage2;
- //OG game Screen
- Stage stage3;
- //instruction 1 screen
- Stage stage4;
- //Count down Version
- Stage stage5;
- //intruction 2
- Stage stage6;
- //game select screen
- Stage stage7;
- @Override
- public void start(Stage primaryStage) {
- C0 = 1;
- C1 = 2;
- C2 = 3;
- C3 = 4;
- C4 = 5;
- stage3 = primaryStage;
- level_1 = true;
- startScreen();
- System.out.println("job done");
- }
- private void startScreen(){
- // Game Title
- Text gameTitle = new Text();
- gameTitle.setText("Mr.Baker's Education Fun House :D");
- gameTitle.setFill(Color.DEEPPINK);
- gameTitle.setFont(Font.font("Guardians", 9));
- gameTitle.setX(10);
- gameTitle.setY(10);
- //Start Problem
- Button buttonPlay = new Button();
- buttonPlay.setText("Play Standard Mode");
- buttonPlay.setOnAction(e -> buttonPlay_Click());
- buttonPlay.setLayoutX(30);
- buttonPlay.setLayoutY(30);
- Button buttonPlayC = new Button();
- buttonPlayC.setText("Play Countdown Mode");
- buttonPlayC.setOnAction(e -> buttonPlayC_Click());
- buttonPlayC.setLayoutX(200);
- buttonPlayC.setLayoutY(30);
- Button buttoninstruc1 = new Button();
- buttoninstruc1.setText("Instructions");
- buttoninstruc1.setOnAction(e -> buttoninstruc1_Click());
- buttoninstruc1.setLayoutX(100);
- buttoninstruc1.setLayoutY(30);
- VBox pane1 = new VBox(10);
- pane1.getChildren().addAll(gameTitle,buttonPlay,buttonPlayC,buttoninstruc1);
- //Starting Screen
- startScene = new Scene(pane1,300,300);
- stage = new Stage();
- stage.setScene(startScene);
- stage.show();
- }
- Button buttonOK;
- /* private void createEditScreen() {
- System.out.println("Create Edit Screen: " + Boolean.toString(true));
- //Editing Scene
- C0 = new TextField();
- C0.setPrefColumnCount(20);
- C0.setPromptText("Set Circle 0's Value");
- C0.setOnInputMethodTextChanged(textEntered);
- C0.setOnAction(textAction);
- C0.setOnKeyReleased(e -> { System.out.println("Key Released: " + Boolean.toString(true));});
- C1 = new TextField();
- C1.setPrefColumnCount(20);
- C1.setPromptText("Set Circle 1's Value");
- C1.setOnInputMethodTextChanged(textEntered);
- C1.setOnAction(textAction);
- C2 = new TextField();
- C2.setPrefColumnCount(20);
- C2.setPromptText("Set Circle 2's Value");
- C2.setOnInputMethodTextChanged(textEntered);
- C2.setOnAction(textAction);
- C3 = new TextField();
- C3.setPrefColumnCount(20);
- C3.setPromptText("Set Circle 3's Value");
- C3.setOnInputMethodTextChanged(textEntered);
- C3.setOnAction(textAction);
- C4 = new TextField();
- C4.setPrefColumnCount(20);
- C4.setPromptText("Set Circle 4's Value");
- C4.setOnInputMethodTextChanged(textEntered);
- C4.setOnAction(textAction);
- Button buttonLF = new Button();
- buttonLF.setText("Load From File");
- buttonLF.setOnAction(e -> buttonLF_Click());
- buttonLF.setLayoutX(30);
- buttonLF.setLayoutY(100);
- buttonLF.setDisable(false);
- buttonOK = new Button();
- buttonOK.setText("Ok");
- buttonOK.setOnAction(e -> buttonOK_Click());
- buttonOK.setLayoutX(30);
- buttonOK.setLayoutY(30);
- buttonOK.setDisable(false);
- Button buttonB = new Button();
- buttonB.setText("Back");
- buttonB.setOnAction(e -> buttonB_Click());
- buttonB.setLayoutX(30);
- buttonB.setLayoutY(200);
- buttonB.setDisable(false);
- VBox pane2 = new VBox(20);
- editScene = new Scene(pane2,500,500);
- stage2 = new Stage();
- stage2.setScene(editScene);
- pane2.getChildren().addAll(C0,C1,C2,C3,C4,buttonOK,buttonB,buttonLF);
- stage2.show();
- }*/
- EventHandler< InputMethodEvent > textEntered =
- new EventHandler< InputMethodEvent >() {
- @Override
- public void handle(InputMethodEvent t) {
- boolean allDataEntered = true;
- // if (C0.getText().isEmpty())
- allDataEntered = false;
- // if (C1.getText().isEmpty())
- allDataEntered = false;
- // if (C2.getText().isEmpty())
- allDataEntered = false;
- // if (C3.getText().isEmpty())
- allDataEntered = false;
- // if (C4.getText().isEmpty())
- allDataEntered = false;
- System.out.println("Text Entered: " + Boolean.toString(allDataEntered));
- buttonOK.setDisable(!allDataEntered);
- }
- };
- EventHandler< ActionEvent > textAction =
- new EventHandler< ActionEvent >() {
- @Override
- public void handle(ActionEvent t) {
- boolean allDataEntered = true;
- // if (C0.getText().isEmpty())
- allDataEntered = false;
- // if (C1.getText().isEmpty())
- allDataEntered = false;
- // if (C2.getText().isEmpty())
- allDataEntered = false;
- // if (C3.getText().isEmpty())
- allDataEntered = false;
- // if (C4.getText().isEmpty())
- allDataEntered = false;
- System.out.println("Text Action: " + Boolean.toString(allDataEntered));
- buttonOK.setDisable(!allDataEntered);
- }
- };
- private void jepordyGame(){
- Rectangle r = new Rectangle();
- r.setX(0);
- r.setY(0);
- r.setWidth(50);
- r.setHeight(50);
- }
- private void createTimedBallGame(){
- listOfStrings = new ArrayList<String>();
- listOfStrings.add("1");
- listOfStrings.add("2");
- listOfStrings.add("3");
- listOfStrings.add("4");
- listOfStrings.add("5");
- if (countDown == true)
- clockTime = 20;
- else
- clockTime = 0;
- timerOn = true;
- Group group = new Group();
- Scene scene = new Scene(group);
- stage3.setHeight(WINDOW_HEIGHT);
- stage3.setWidth(WINDOW_WIDTH);
- stage3.setTitle("Line Up - Put the balls in order from smallest to largest");
- stage3.setResizable(false);
- Text texttime = new Text();
- Rectangle r = new Rectangle();
- r.setX(0);
- r.setY(0);
- r.setWidth(50);
- r.setHeight(50);
- texttime.setText("Start");
- texttime.setFill(Color.ORANGE);
- texttime.setFont(Font.font ("Verdana", 20));
- texttime.setX(10);
- texttime.setY(30);
- System.out.println("Text");
- texttime.setId("Timer");
- // primaryStage.initModality(Modality.APPLICATION_MODAL);
- KeyFrame k = new KeyFrame(Duration.seconds(1),
- e-> {
- if (timerOn == true){
- if (countDown)
- clockTime = clockTime - 1;
- else
- clockTime = clockTime + 1;
- if (countDown && clockTime <= 0){
- // game over
- clockTime = 0;
- timerOn = false;
- texttime.setText("Game Over");
- gameOver(group);
- }
- else {
- texttime.setText(Integer.toString(clockTime));
- }
- }
- });
- timerOn = true;
- timeLine = new Timeline(k);
- timeLine.setCycleCount(Timeline.INDEFINITE);
- timeLine.play();
- if (level_2 == true) {
- listOfStrings.clear();
- listOfStrings.add("1/2");
- listOfStrings.add("3/4");
- listOfStrings.add("5/7");
- listOfStrings.add("3/2");
- listOfStrings.add("7/4");
- }
- else if(level_3 == true){
- listOfStrings.clear();
- listOfStrings.add(".2");
- listOfStrings.add("1/2");
- listOfStrings.add(".6");
- listOfStrings.add("3/4");
- listOfStrings.add("4/5");
- }
- group.getChildren().addAll(texttime);
- for(int i=0; i < CIRCLES_NUMBER; i++){
- Circle circle = new Circle();
- Color c = new Color(Math.random(),Math.random(),Math.random(),1.0);
- circle.setFill(c);
- circle.setCenterX((WINDOW_WIDTH-LINE_OFFSET-2*CIRCLE_RADIUS)*Math.random()+CIRCLE_RADIUS);
- circle.setCenterY((WINDOW_HEIGHT-LINE_OFFSET-2*CIRCLE_RADIUS)*Math.random()+CIRCLE_RADIUS);
- circle.setRadius(CIRCLE_RADIUS);
- circle.setCursor(Cursor.HAND);
- circle.setOnMouseDragged(circleOnMouseDraggedEventHandler);
- circle.setOnDragDropped(circleOnDragDroppedEventHandler);
- circle.setId(CIRCLE_ID + Integer.toString(i));
- Text text = new Text();
- text.setText(listOfStrings.get(i));
- text.setFill(c.invert());
- text.setFont(Font.font ("Verdana", 20));
- text.setX(circle.getCenterX());
- text.setY(circle.getCenterY());
- text.setId(TEXT_ID + Integer.toString(i));
- group.getChildren().addAll(circle, text);
- System.out.println("Button: " + Integer.toString(i));
- System.out.println("Size: " + Integer.toString(group.getChildren().size()));
- }
- Line line = new Line();
- line.setStartX(LINE_OFFSET);
- line.setStartY(WINDOW_HEIGHT-LINE_OFFSET);
- line.setEndX(WINDOW_WIDTH-LINE_OFFSET);
- line.setEndY(WINDOW_HEIGHT-LINE_OFFSET);
- line.setStrokeWidth(15);
- line.setStroke(Color.AQUA);
- line.setCursor(Cursor.OPEN_HAND);
- line.setOnMouseDragReleased(lineOnMouseDragReleasedEventHandler);
- line.setOnDragDropped(lineOnDragDroppedEventHandler);
- line.setOnDragEntered(lineOnDragEnteredEventHandler);
- line.setId(LINE_ID);
- group.getChildren().add(line);
- group.setId("Scene Group");
- System.out.println("Final Size: " + Integer.toString(group.getChildren().size()));
- stage3.setScene(scene);
- stage3.show();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- public void buttoninstruc1_Click(){
- stage.close();
- Text instruc1 = new Text();
- instruc1.setText("STANDARD MODE\n"
- + "1.Add in desired sequence\n"
- + "2.put each cirlce in order\n"
- + "3.Press Play Again to play again or press quit to exit\n"
- + "\n"
- + "\n"
- + "COUNTDOWN MODE\n"
- + "1. Add in desired sequence\n"
- + "2.put each circle in order before time runs out\n"
- + "3.Press Play Again to play again or pres quit to exit the game\n"
- + "\n\n");
- Button buttonBack1 = new Button();
- buttonBack1.setText("Back");
- buttonBack1.setOnAction(e -> buttonBack1_Click());
- buttonBack1.setLayoutX(30);
- buttonBack1.setLayoutY(30);
- VBox pane3 = new VBox();
- instrucScene1 = new Scene(pane3, 500, 500);
- stage4 = new Stage();
- stage4.setScene(instrucScene1);
- pane3.getChildren().addAll(instruc1,buttonBack1);
- stage4.show();
- }
- public void buttonOK_Click(){
- stage2.close();
- createTimedBallGame();
- }
- public void buttonPlay_Click(){
- stage.close();
- countDown = false;
- createTimedBallGame();
- }
- public void buttonPlayC_Click(){
- stage.close();
- countDown = true;
- createTimedBallGame();
- }
- public void buttonBack1_Click(){
- stage4.close();
- startScreen();
- }
- public void buttonB_Click(){
- stage2.close();
- startScreen();
- }
- /* public void buttonLF_Click(){
- List<String> levels = new ArrayList<>();
- try {
- levels = Files.readAllLines(Paths.get("levels.txt"),Charset.defaultCharset());
- }
- catch(IOException e ) {
- e.printStackTrace();
- }
- C0.setText(C0);
- C1.setText("2");
- C2.setText(levels.get(2));
- C3.setText(levels.get(3));
- C4.setText(levels.get(4));
- }
- */
- EventHandler<MouseEvent> circleOnMouseDraggedEventHandler =
- new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent t) {
- if (!timerOn)
- return;
- double newX = t.getX();
- double newY = t.getY();
- Circle myCircle = (Circle)(t.getSource());
- Group myGroup = (Group)myCircle.getParent();
- //check for collsion with borders
- if (newX < myCircle.getRadius()) {
- newX = myCircle.getRadius();
- }
- if (newY < myCircle.getRadius()) {
- newY = myCircle.getRadius();
- }
- if (myCircle.getRadius() + newX > WINDOW_WIDTH) {
- newX = WINDOW_WIDTH - myCircle.getRadius();
- }
- // check for collision with line
- Line myLine = (Line)myGroup.lookup("#"+LINE_ID);
- if (myLine == null){
- System.out.println("Circle Dragged Event - Line Node not found! ");
- }
- else {
- // detect collision with line
- if (myCircle.getRadius() + newY > myLine.getEndY()) {
- System.out.println("Circle collision + with line!");
- newY = myLine.getEndY() - myCircle.getRadius();
- }
- }
- myCircle.setCenterX(newX);
- myCircle.setCenterY(newY);
- // find the text for the circle
- String myCircleId = myCircle.getId();
- String myTextId = TEXT_ID + myCircleId.substring(myCircleId.length()-1);
- Text myText = (Text)myGroup.lookup("#"+ myTextId);
- if (myText != null) {
- myText.setX(newX);
- myText.setY(newY);
- }
- else {
- System.out.println("Circle Dragged Event - Text Node not found: " + myTextId);
- }
- System.out.println("Circle Y Translate: " + Double.toString(newY));
- System.out.println("Circle Y: " + Double.toString(myCircle.getCenterY() + myCircle.getRadius()));
- System.out.println("Line Y: " + Double.toString(myLine.getEndY() - myLine.getStrokeWidth()));
- //System.out.println("Circle Dragged Event " + myCircleId);
- //System.out.println("Circle Dragged Event " + myTextId);
- //System.out.println("Circle Dragged Event " + myGroup.getId());
- //System.out.println("Group Size: " + Integer.toString(myGroup.getChildren().size()));
- //check if all balls are on the line
- boolean allOnLine = true;
- for (Node node: myGroup.getChildren()) {
- if (node != null) {
- if (node.getId() != null) {
- if (node.getId().startsWith(CIRCLE_ID)) {
- // see if circle is on the line
- myCircle = (Circle) node;
- if (myCircle.getRadius() + myCircle.getCenterY() >= myLine.getEndY()) {
- // on the line
- allOnLine = true;
- }
- else {
- allOnLine = false;
- break;
- }
- }
- }
- }
- }
- double CircleCX0 = 0;
- myCircle = (Circle)myGroup.lookup("#"+CIRCLE_ID + "0");
- if (myCircle == null){
- System.out.println("Could not find circle 0");
- }else{
- CircleCX0 = myCircle.getCenterX();
- }
- double CircleCX1 = 0;
- myCircle = (Circle)myGroup.lookup("#"+CIRCLE_ID + "1");
- if (myCircle == null){
- System.out.println("Could not find circle 1");
- }else{
- CircleCX1 = myCircle.getCenterX();
- }
- double CircleCX2 = 0;
- myCircle = (Circle)myGroup.lookup("#"+CIRCLE_ID + "2");
- if (myCircle == null){
- System.out.println("Could not find circle 2");
- }else{
- CircleCX2 = myCircle.getCenterX();
- }
- double CircleCX3 = 0;
- myCircle = (Circle)myGroup.lookup("#"+CIRCLE_ID + "3");
- if (myCircle == null){
- System.out.println("Could not find circle 3");
- }else{
- CircleCX3 = myCircle.getCenterX();
- }
- double CircleCX4 = 0;
- myCircle = (Circle)myGroup.lookup("#"+CIRCLE_ID + "4");
- if (myCircle == null){
- System.out.println("Could not find circle 4");
- }else{
- CircleCX4 = myCircle.getCenterX();
- }
- if (allOnLine){
- if (CircleCX0 < CircleCX1){
- if(CircleCX1 < CircleCX2){
- if(CircleCX2 < CircleCX3){
- if(CircleCX3 < CircleCX4){
- gameOver(myGroup);
- }
- }
- }
- }
- }
- }
- };
- public void gameOver(Group myGroup) {
- Text textComplete = new Text();
- textComplete.setText("");
- if (!countDown) {
- textComplete.setText("YOU DID IT!");
- textComplete.setFont(new Font("Guardians",36));
- textComplete.setX(50);
- textComplete.setY(50);
- System.out.println("You did it");
- myGroup.getChildren().addAll(textComplete);
- }
- timerOn = false;
- timeLine.stop();
- if(level_2 == true){
- Button buttonNextLevel = new Button();
- buttonNextLevel.setText("Next Level");
- buttonNextLevel.setOnAction(e -> buttonNextLevel_Click());
- buttonNextLevel.setLayoutX(30);
- buttonNextLevel.setLayoutY(100);
- Button buttonQuit = new Button();
- buttonQuit.setText("Quit");
- buttonQuit.setOnAction(e -> buttonQuit_Click());
- buttonQuit.setLayoutX(120);
- buttonQuit.setLayoutY(100);
- myGroup.getChildren().addAll(buttonNextLevel,buttonQuit);
- System.out.println("Level 2 to 3");
- }else if(level_1 == true){
- Button buttonNextLevel = new Button();
- buttonNextLevel.setText("Next Level");
- buttonNextLevel.setOnAction(e -> buttonNextLevel_Click());
- buttonNextLevel.setLayoutX(30);
- buttonNextLevel.setLayoutY(100);
- Button buttonQuit = new Button();
- buttonQuit.setText("Quit");
- buttonQuit.setOnAction(e -> buttonQuit_Click());
- buttonQuit.setLayoutX(120);
- buttonQuit.setLayoutY(100);
- myGroup.getChildren().addAll(buttonNextLevel,buttonQuit);
- System.out.println("Level 1 to 2");
- }
- else if (level_3 == true){
- Button buttonQuit = new Button();
- buttonQuit.setText("Quit");
- buttonQuit.setOnAction(e -> buttonQuit_Click());
- buttonQuit.setLayoutX(120);
- buttonQuit.setLayoutY(100);
- myGroup.getChildren().add(buttonQuit);
- System.out.println("End Game");
- }
- }
- public void buttonNextLevel_Click(){
- stage3.close();
- if(level_2 == true){
- level_3 = true;
- level_2 = false;
- level_1 = false;
- }
- else if(level_1 == true){
- level_2 = true;
- level_1 = false;
- level_3 = false;
- }
- createTimedBallGame();
- }
- public void buttonQuit_Click(){
- stage3.close();
- }
- EventHandler<DragEvent> circleOnDragDroppedEventHandler =
- new EventHandler<DragEvent>() {
- @Override
- public void handle(DragEvent t) {
- // ((Line)(t.getSource())).setStroke(Color.BROWN);
- System.out.println("Circle Drag Dropped");
- }
- };
- EventHandler<DragEvent> lineOnDragDroppedEventHandler =
- new EventHandler<DragEvent>() {
- @Override
- public void handle(DragEvent t) {
- // ((Line)(t.getSource())).setStroke(Color.BROWN);
- System.out.println("Line Drag Dropped");
- }
- };
- EventHandler<DragEvent> lineOnDragEnteredEventHandler =
- new EventHandler<DragEvent>() {
- @Override
- public void handle(DragEvent t) {
- // ((Line)(t.getSource())).setStroke(Color.BROWN);
- System.out.println("Line Drag Entered");
- }
- };
- EventHandler<MouseEvent> lineOnMouseDragReleasedEventHandler =
- new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent t) {
- ((Line)(t.getSource())).setStroke(Color.BROWN);
- System.out.println("Line Drag Release");
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement