Advertisement
pongfactory

Midterm_Java_v2

Sep 8th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.42 KB | None | 0 0
  1. package uiRender;
  2.  
  3. import javafx.application.Application;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.Group;
  7. import javafx.scene.canvas.Canvas;
  8. import javafx.scene.canvas.GraphicsContext;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.image.Image;
  11. import javafx.scene.input.MouseEvent;
  12. import javafx.scene.layout.StackPane;
  13. import javafx.scene.paint.Color;
  14. import javafx.scene.text.Font;
  15. import javafx.scene.text.FontWeight;
  16. import javafx.animation.Timeline;
  17.  
  18. import javafx.scene.shape.Line;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. import javafx.animation.KeyFrame;
  23. import javafx.util.Duration;
  24. import logic.*;
  25. import logic.GameManager;
  26. import javafx.event.EventHandler;
  27. import javafx.event.ActionEvent;
  28.  
  29. public class userInterface extends Application {
  30.     public static void main(String[] args) {
  31.         launch(args);
  32.     }
  33.  
  34.     GameManager gm;
  35.  
  36.     public void enemyMove(int t) {
  37.         for (Enemy e : gm.enemies) {
  38.             e.x = e.xcenter + 140 * Math.cos(t);
  39.             e.y = e.ycenter + 30 * Math.sin(t);
  40.         }
  41.     }
  42.  
  43.     @Override
  44.     public void start(Stage theStage) {
  45.         GameManager gm = new GameManager();
  46.  
  47.         // boss end
  48.         theStage.setTitle("2110215 PROG METH Midterm | 2016-1");
  49.  
  50.         Group root = new Group();
  51.         Scene theScene = new Scene(root);
  52.         theStage.setScene(theScene);
  53.  
  54.         Canvas canvas = new Canvas(1024, 768);
  55.         root.getChildren().add(canvas);
  56.  
  57.         GraphicsContext gc = canvas.getGraphicsContext2D();
  58.         Circle targetBuy1 = new Circle(50, 18, 40);
  59.         Circle targetBuy2 = new Circle(50 + 100, 18, 40);
  60.         Circle targetBuy3 = new Circle(50 + 200, 18, 40);
  61.  
  62.         Image enermy = new Image("enermy.png");
  63.         Image tools1 = new Image("tools1.png");
  64.         Image tools2 = new Image("tools2.png");
  65.         Image tools3 = new Image("tools3.png");
  66.         Image buytools1 = new Image("tools1.png");
  67.         Image buytools2 = new Image("tools2.png");
  68.         Image buytools3 = new Image("tools3.png");
  69.         Image moneyS = new Image("money.png");
  70.         Image space = new Image("bgF.png");
  71.         // gc.drawImage(space, 0, 0);
  72.  
  73.         IntValue time = new IntValue(0);
  74.         IntValue time2 = new IntValue(0);
  75.         IntValue time3 = new IntValue(0);
  76.         IntValue timeBuy1 = new IntValue(1);
  77.         IntValue timeBuy2 = new IntValue(1);
  78.         IntValue timeBuy3 = new IntValue(1);
  79.  
  80.         Timeline gameLoop = new Timeline();
  81.         gameLoop.setCycleCount(Timeline.INDEFINITE);
  82.  
  83.         final long timeStart = System.currentTimeMillis();
  84.  
  85.         KeyFrame kf = new KeyFrame(Duration.seconds(0.017), // 60 FPS
  86.                 new EventHandler<ActionEvent>() {
  87.                     public void handle(ActionEvent ae) {
  88.  
  89.                         int i = 0;
  90.                         double t = (System.currentTimeMillis() - timeStart) / 1000.0;
  91.                         boolean isNextTurn = false;
  92.                         if ((int) t > time.value)
  93.                             isNextTurn = true;
  94.                         time.value = (int) t;
  95.                         time2.value = time.value + 2;
  96.                         time3.value = 0;
  97.                         // boss
  98.                         if (isNextTurn)
  99.                             gm.turn();
  100.  
  101.                         /*
  102.                          * for(Tool tool : gm.tools){ //Drawtool(t.x,t.y)
  103.                          * System.out.println(tool.x +""+ tool.y); }
  104.                          */
  105.                         // boss
  106.  
  107.                         // Clear the canvas
  108.                         gc.clearRect(0, 0, 1024, 768);
  109.  
  110.                         // background image clears canvas
  111.                         gc.drawImage(space, 0, 0);
  112.  
  113.                         if (time.value == 1 | time.value == 2 | time.value == 3) {
  114.                             String resourceText = "Start Game";
  115.                             gc.fillText(resourceText, 1024 / 2 - 120, 768 / 2);
  116.                             gc.strokeText(resourceText, 1024 / 2 - 120, 768 / 2);
  117.                         }
  118.  
  119.                         // gc.drawImage(tools1, 370, 700);
  120.                         // gc.drawImage(tools1, 370, 700 - 70);
  121.                         // gc.drawImage(tools1, 370, 700 - 70 - 70);
  122.                         // gc.drawImage(tools1, 370, 700 - 70 - 70 - 70);
  123.                         // gc.drawImage(tools1, 370, 700 - 70 - 70 - 70 - 70);
  124.  
  125.                         // gc.drawImage(tools2, 470, 700);
  126.                         // gc.drawImage(tools2, 470, 700 - 70);
  127.                         // gc.drawImage(tools2, 470, 700 - 70 - 70);
  128.                         // gc.drawImage(tools2, 470, 700 - 70 - 70 - 70);
  129.                         // gc.drawImage(tools2, 470, 700 - 70 - 70 - 70 - 70);
  130.  
  131.                         // gc.drawImage(tools3, 570, 700);
  132.                         // gc.drawImage(tools3, 570, 700 - 70);
  133.                         // gc.drawImage(tools3, 570, 700 - 70 - 70);
  134.                         // gc.drawImage(tools3, 570, 700 - 70 - 70 - 70);
  135.                         // gc.drawImage(tools3, 570, 700 - 70 - 70 - 70 - 70);
  136.  
  137.                         gc.drawImage(buytools1, 10, 10);
  138.                         gc.drawImage(moneyS, 40, 18);
  139.                         gc.drawImage(buytools2, 110, 10);
  140.                         gc.drawImage(moneyS, 40 + 100, 18);
  141.                         gc.drawImage(buytools3, 210, 10);
  142.                         gc.drawImage(moneyS, 40 + 200, 18);
  143.  
  144.                         // gc.strokeOval(60, 60, 30, 30);
  145.  
  146.                         for (Tool tool : gm.tools) {
  147.                             if (tool instanceof VictoryTool) {
  148.                                 gc.drawImage(tools3, tool.x, tool.y);
  149.                             }
  150.                             if (tool instanceof AttackTool) {
  151.                                 gc.drawImage(tools1, tool.x, tool.y);
  152.                             }
  153.                             if (tool instanceof WorkerTool) {
  154.                                 gc.drawImage(tools2, tool.x, tool.y);
  155.                             }
  156.                         }
  157.  
  158.                         theScene.setOnMouseClicked(new EventHandler<MouseEvent>() {
  159.                             public void handle(MouseEvent e) {
  160.                                 // int itemOne;
  161.                                 if (targetBuy1.containsPoint(e.getX(), e.getY())) {
  162.                                     timeBuy1.value++;
  163.                                     if (timeBuy1.value == 2) {
  164.                                         gm.buyTool(new AttackTool(370, 700 - 70));
  165.                                     }
  166.                                     if (timeBuy1.value == 3) {
  167.                                         gm.buyTool(new AttackTool(370, 700 - 70 - 70));
  168.                                     }
  169.                                     if (timeBuy1.value == 4) {
  170.                                         gm.buyTool(new AttackTool(370, 700 - 70 - 70 - 70));
  171.                                     }
  172.                                     if (timeBuy1.value == 5) {
  173.                                         gm.buyTool(new AttackTool(370, 700 - 70 - 70 - 70 - 70));
  174.                                     }
  175.                                     // gc.drawImage(tools1, 570, 700 - 70);
  176.                                     /*
  177.                                      * if(gm.buyTool(new AttackTool(1,2))==1){
  178.                                      * // can buy // }else{ // }
  179.                                      */
  180.  
  181.                                     // gc.drawImage(tools1, 370, 700 - 70);
  182.                                 }
  183.                                 if (timeBuy1.value == 6) {
  184.                                     timeBuy1.value = 1;
  185.                                 }
  186.                                 if (targetBuy2.containsPoint(e.getX(), e.getY())) {
  187.                                     timeBuy2.value++;
  188.                                     if (timeBuy2.value == 2) {
  189.                                         gm.buyTool(new WorkerTool(470, 700 - 70));
  190.                                     }
  191.                                     if (timeBuy2.value == 3) {
  192.                                         gm.buyTool(new WorkerTool(470, 700 - 70 - 70));
  193.                                     }
  194.                                     if (timeBuy2.value == 4) {
  195.                                         gm.buyTool(new WorkerTool(470, 700 - 70 - 70 - 70));
  196.                                     }
  197.                                     if (timeBuy2.value == 5) {
  198.                                         gm.buyTool(new WorkerTool(470, 700 - 70 - 70 - 70 - 70));
  199.                                     }
  200.                                     // gm.buyTool(new VictoryTool(570, 700 -
  201.                                     // 70));
  202.                                     // gc.drawImage(tools1, 370, 700 - 70);
  203.                                 }
  204.                                 if (timeBuy2.value == 6) {
  205.                                     timeBuy2.value = 1;
  206.                                 }
  207.                                 if (targetBuy3.containsPoint(e.getX(), e.getY())) {
  208.                                     timeBuy3.value++;
  209.                                     if (timeBuy3.value == 2) {
  210.                                         gm.buyTool(new VictoryTool(570, 700 - 70));
  211.                                     }
  212.                                     if (timeBuy3.value == 3) {
  213.                                         gm.buyTool(new VictoryTool(570, 700 - 70 - 70));
  214.                                     }
  215.                                     if (timeBuy3.value == 4) {
  216.                                         gm.buyTool(new VictoryTool(570, 700 - 70 - 70 - 70));
  217.                                     }
  218.                                     if (timeBuy3.value == 5) {
  219.                                         gm.buyTool(new VictoryTool(570, 700 - 70 - 70 - 70 - 70));
  220.                                     }
  221.                                     // if(timeBuy3.value==5){gm.buyTool(new
  222.                                     // VictoryTool(570, 700 - 70 - 70 -70 -70
  223.                                     // -70));}
  224.                                     // gc.drawImage(tools1, 370, 700 - 70);
  225.                                 }
  226.                                 if (timeBuy3.value == 6) {
  227.                                     timeBuy3.value = 1;
  228.                                 }
  229.                                 /*
  230.                                  * System.out.println("You buy tools1 = " +
  231.                                  * timeBuy1.value);
  232.                                  * System.out.println("You buy tools2 = " +
  233.                                  * timeBuy2.value);
  234.                                  * System.out.println("You buy tools3 = " +
  235.                                  * timeBuy3.value);
  236.                                  */
  237.                             }
  238.                         });
  239.                         // System.out.println(timeBuy1.value);
  240.  
  241.                         if (time.value > 1) {
  242.                             double x = (200) + 140 * Math.sin(t);
  243.                             double y = (140 + (i + 1) * 2) + 30 * Math.cos(t);
  244.                             gc.drawImage(enermy, x, y);
  245.                             if (time.value > 3) {
  246.                                 gc.setStroke(Color.RED);
  247.                                 gc.setLineWidth(5);
  248.                                 gc.strokeLine(x + 30, y + 20, 370 + 50, 700 + 40);
  249.                             }
  250.                         }
  251.  
  252.                         if (time.value > 4) {
  253.                             double x2 = (600) + 140 * Math.sin(t);
  254.                             double y2 = (140 + (i + 1) * 2) + 30 * Math.cos(t);
  255.                             gc.drawImage(enermy, x2, y2);
  256.                             if (time.value > 6) {
  257.                                 gc.setStroke(Color.GREEN);
  258.                                 gc.setLineWidth(5);
  259.                                 gc.strokeLine(x2 + 30, y2 + 20, 370 + 50, 700 + 40);
  260.                             }
  261.                         }
  262.  
  263.                         if (time.value > 7) {
  264.                             double x3 = (600 + 55) + 140 * Math.cos(t);
  265.                             double y3 = (140 + (i + 1) * 2) + 30 * Math.sin(t);
  266.                             gc.drawImage(enermy, x3, y3);
  267.                             if (time.value > 9) {
  268.                                 gc.setStroke(Color.BLUE);
  269.                                 gc.setLineWidth(5);
  270.                                 gc.strokeLine(x3 + 30, y3 + 20, 370 + 50, 700 + 40);
  271.                             }
  272.                         }
  273.  
  274.                         if (time.value > 10) {
  275.                             double x4 = (200 + 55) + 140 * Math.cos(t);
  276.                             double y4 = (140 + (i + 1) * 2) + 30 * Math.sin(t);
  277.                             gc.drawImage(enermy, x4, y4);
  278.                             if (time.value > 11) {
  279.                                 gc.setStroke(Color.PINK);
  280.                                 gc.setLineWidth(5);
  281.                                 gc.strokeLine(x4 + 30, y4 + 20, 370 + 50, 700 + 40);
  282.                             }
  283.                         }
  284.  
  285.                         if (time.value > 20) {
  286.                             String resourceText = " You Lose";
  287.                             gc.fillText(resourceText, 1024 / 2 - 120, 768 / 2 - 80);
  288.                             gc.strokeText(resourceText, 1024 / 2 - 120, 768 / 2 - 80);
  289.                             resourceText = "Game Over";
  290.                             gc.fillText(resourceText, 1024 / 2 - 120, 768 / 2);
  291.                             gc.strokeText(resourceText, 1024 / 2 - 120, 768 / 2);
  292.                             time.value = time3.value;
  293.                             time2.value = time3.value;
  294.                         }
  295.  
  296.                         Font theFont = Font.font("Helvetica", FontWeight.BOLD, 50);
  297.                         gc.setFont(theFont);
  298.                         gc.setStroke(Color.BLACK);
  299.                         gc.setLineWidth(1);
  300.                         gc.setFill(Color.RED);
  301.  
  302.                         String resourceText = "Money: " + time2.value;
  303.                         gc.fillText(resourceText, 750 - 30, 56);
  304.                         gc.strokeText(resourceText, 750 - 30, 56);
  305.  
  306.                         String timeText = "Time: " + time.value;
  307.                         gc.fillText(timeText, 490 - 30, 56);
  308.                         gc.strokeText(timeText, 490 - 30, 56);
  309.  
  310.                         /*
  311.                          * gc.setStroke(Color.RED); gc.setLineWidth(5);
  312.                          * gc.strokeLine(x + 30, y + 20, 370 + 50, 700 + 40);
  313.                          *
  314.                          * gc.setStroke(Color.GREEN); gc.setLineWidth(5);
  315.                          * gc.strokeLine(x2 + 30, y2 + 20, 370 + 50, 700 + 40);
  316.                          *
  317.                          * gc.setStroke(Color.BLUE); gc.setLineWidth(5);
  318.                          * gc.strokeLine(x3 + 30, y3 + 20, 370 + 50, 700 + 40);
  319.                          *
  320.                          * gc.setStroke(Color.PINK); gc.setLineWidth(5);
  321.                          * gc.strokeLine(x4 + 30, y4 + 20, 370 + 50, 700 + 40);
  322.                          */
  323.  
  324.                     }
  325.  
  326.                 });
  327.  
  328.         gameLoop.getKeyFrames().add(kf);
  329.         gameLoop.play();
  330.  
  331.         theStage.show();
  332.     }
  333.  
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement