Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uiRender;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.Group;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Button;
- import javafx.scene.image.Image;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Font;
- import javafx.scene.text.FontWeight;
- import javafx.animation.Timeline;
- import javafx.scene.shape.Line;
- import java.util.ArrayList;
- import javafx.animation.KeyFrame;
- import javafx.util.Duration;
- import logic.*;
- import logic.GameManager;
- import javafx.event.EventHandler;
- import javafx.event.ActionEvent;
- public class userInterface extends Application {
- public static void main(String[] args) {
- launch(args);
- }
- GameManager gm;
- public void enemyMove(int t) {
- for (Enemy e : gm.enemies) {
- e.x = e.xcenter + 140 * Math.cos(t);
- e.y = e.ycenter + 30 * Math.sin(t);
- }
- }
- @Override
- public void start(Stage theStage) {
- GameManager gm = new GameManager();
- // boss end
- theStage.setTitle("2110215 PROG METH Midterm | 2016-1");
- Group root = new Group();
- Scene theScene = new Scene(root);
- theStage.setScene(theScene);
- Canvas canvas = new Canvas(1024, 768);
- root.getChildren().add(canvas);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- Circle targetBuy1 = new Circle(50, 18, 40);
- Circle targetBuy2 = new Circle(50 + 100, 18, 40);
- Circle targetBuy3 = new Circle(50 + 200, 18, 40);
- Image enermy = new Image("enermy.png");
- Image tools1 = new Image("tools1.png");
- Image tools2 = new Image("tools2.png");
- Image tools3 = new Image("tools3.png");
- Image buytools1 = new Image("tools1.png");
- Image buytools2 = new Image("tools2.png");
- Image buytools3 = new Image("tools3.png");
- Image moneyS = new Image("money.png");
- Image space = new Image("bgF.png");
- // gc.drawImage(space, 0, 0);
- IntValue time = new IntValue(0);
- IntValue time2 = new IntValue(0);
- IntValue time3 = new IntValue(0);
- IntValue timeBuy1 = new IntValue(1);
- IntValue timeBuy2 = new IntValue(1);
- IntValue timeBuy3 = new IntValue(1);
- Timeline gameLoop = new Timeline();
- gameLoop.setCycleCount(Timeline.INDEFINITE);
- final long timeStart = System.currentTimeMillis();
- KeyFrame kf = new KeyFrame(Duration.seconds(0.017), // 60 FPS
- new EventHandler<ActionEvent>() {
- public void handle(ActionEvent ae) {
- int i = 0;
- double t = (System.currentTimeMillis() - timeStart) / 1000.0;
- boolean isNextTurn = false;
- if ((int) t > time.value)
- isNextTurn = true;
- time.value = (int) t;
- time2.value = time.value + 2;
- time3.value = 0;
- // boss
- if (isNextTurn)
- gm.turn();
- /*
- * for(Tool tool : gm.tools){ //Drawtool(t.x,t.y)
- * System.out.println(tool.x +""+ tool.y); }
- */
- // boss
- // Clear the canvas
- gc.clearRect(0, 0, 1024, 768);
- // background image clears canvas
- gc.drawImage(space, 0, 0);
- if (time.value == 1 | time.value == 2 | time.value == 3) {
- String resourceText = "Start Game";
- gc.fillText(resourceText, 1024 / 2 - 120, 768 / 2);
- gc.strokeText(resourceText, 1024 / 2 - 120, 768 / 2);
- }
- // gc.drawImage(tools1, 370, 700);
- // gc.drawImage(tools1, 370, 700 - 70);
- // gc.drawImage(tools1, 370, 700 - 70 - 70);
- // gc.drawImage(tools1, 370, 700 - 70 - 70 - 70);
- // gc.drawImage(tools1, 370, 700 - 70 - 70 - 70 - 70);
- // gc.drawImage(tools2, 470, 700);
- // gc.drawImage(tools2, 470, 700 - 70);
- // gc.drawImage(tools2, 470, 700 - 70 - 70);
- // gc.drawImage(tools2, 470, 700 - 70 - 70 - 70);
- // gc.drawImage(tools2, 470, 700 - 70 - 70 - 70 - 70);
- // gc.drawImage(tools3, 570, 700);
- // gc.drawImage(tools3, 570, 700 - 70);
- // gc.drawImage(tools3, 570, 700 - 70 - 70);
- // gc.drawImage(tools3, 570, 700 - 70 - 70 - 70);
- // gc.drawImage(tools3, 570, 700 - 70 - 70 - 70 - 70);
- gc.drawImage(buytools1, 10, 10);
- gc.drawImage(moneyS, 40, 18);
- gc.drawImage(buytools2, 110, 10);
- gc.drawImage(moneyS, 40 + 100, 18);
- gc.drawImage(buytools3, 210, 10);
- gc.drawImage(moneyS, 40 + 200, 18);
- // gc.strokeOval(60, 60, 30, 30);
- for (Tool tool : gm.tools) {
- if (tool instanceof VictoryTool) {
- gc.drawImage(tools3, tool.x, tool.y);
- }
- if (tool instanceof AttackTool) {
- gc.drawImage(tools1, tool.x, tool.y);
- }
- if (tool instanceof WorkerTool) {
- gc.drawImage(tools2, tool.x, tool.y);
- }
- }
- theScene.setOnMouseClicked(new EventHandler<MouseEvent>() {
- public void handle(MouseEvent e) {
- // int itemOne;
- if (targetBuy1.containsPoint(e.getX(), e.getY())) {
- timeBuy1.value++;
- if (timeBuy1.value == 2) {
- gm.buyTool(new AttackTool(370, 700 - 70));
- }
- if (timeBuy1.value == 3) {
- gm.buyTool(new AttackTool(370, 700 - 70 - 70));
- }
- if (timeBuy1.value == 4) {
- gm.buyTool(new AttackTool(370, 700 - 70 - 70 - 70));
- }
- if (timeBuy1.value == 5) {
- gm.buyTool(new AttackTool(370, 700 - 70 - 70 - 70 - 70));
- }
- // gc.drawImage(tools1, 570, 700 - 70);
- /*
- * if(gm.buyTool(new AttackTool(1,2))==1){
- * // can buy // }else{ // }
- */
- // gc.drawImage(tools1, 370, 700 - 70);
- }
- if (timeBuy1.value == 6) {
- timeBuy1.value = 1;
- }
- if (targetBuy2.containsPoint(e.getX(), e.getY())) {
- timeBuy2.value++;
- if (timeBuy2.value == 2) {
- gm.buyTool(new WorkerTool(470, 700 - 70));
- }
- if (timeBuy2.value == 3) {
- gm.buyTool(new WorkerTool(470, 700 - 70 - 70));
- }
- if (timeBuy2.value == 4) {
- gm.buyTool(new WorkerTool(470, 700 - 70 - 70 - 70));
- }
- if (timeBuy2.value == 5) {
- gm.buyTool(new WorkerTool(470, 700 - 70 - 70 - 70 - 70));
- }
- // gm.buyTool(new VictoryTool(570, 700 -
- // 70));
- // gc.drawImage(tools1, 370, 700 - 70);
- }
- if (timeBuy2.value == 6) {
- timeBuy2.value = 1;
- }
- if (targetBuy3.containsPoint(e.getX(), e.getY())) {
- timeBuy3.value++;
- if (timeBuy3.value == 2) {
- gm.buyTool(new VictoryTool(570, 700 - 70));
- }
- if (timeBuy3.value == 3) {
- gm.buyTool(new VictoryTool(570, 700 - 70 - 70));
- }
- if (timeBuy3.value == 4) {
- gm.buyTool(new VictoryTool(570, 700 - 70 - 70 - 70));
- }
- if (timeBuy3.value == 5) {
- gm.buyTool(new VictoryTool(570, 700 - 70 - 70 - 70 - 70));
- }
- // if(timeBuy3.value==5){gm.buyTool(new
- // VictoryTool(570, 700 - 70 - 70 -70 -70
- // -70));}
- // gc.drawImage(tools1, 370, 700 - 70);
- }
- if (timeBuy3.value == 6) {
- timeBuy3.value = 1;
- }
- /*
- * System.out.println("You buy tools1 = " +
- * timeBuy1.value);
- * System.out.println("You buy tools2 = " +
- * timeBuy2.value);
- * System.out.println("You buy tools3 = " +
- * timeBuy3.value);
- */
- }
- });
- // System.out.println(timeBuy1.value);
- if (time.value > 1) {
- double x = (200) + 140 * Math.sin(t);
- double y = (140 + (i + 1) * 2) + 30 * Math.cos(t);
- gc.drawImage(enermy, x, y);
- if (time.value > 3) {
- gc.setStroke(Color.RED);
- gc.setLineWidth(5);
- gc.strokeLine(x + 30, y + 20, 370 + 50, 700 + 40);
- }
- }
- if (time.value > 4) {
- double x2 = (600) + 140 * Math.sin(t);
- double y2 = (140 + (i + 1) * 2) + 30 * Math.cos(t);
- gc.drawImage(enermy, x2, y2);
- if (time.value > 6) {
- gc.setStroke(Color.GREEN);
- gc.setLineWidth(5);
- gc.strokeLine(x2 + 30, y2 + 20, 370 + 50, 700 + 40);
- }
- }
- if (time.value > 7) {
- double x3 = (600 + 55) + 140 * Math.cos(t);
- double y3 = (140 + (i + 1) * 2) + 30 * Math.sin(t);
- gc.drawImage(enermy, x3, y3);
- if (time.value > 9) {
- gc.setStroke(Color.BLUE);
- gc.setLineWidth(5);
- gc.strokeLine(x3 + 30, y3 + 20, 370 + 50, 700 + 40);
- }
- }
- if (time.value > 10) {
- double x4 = (200 + 55) + 140 * Math.cos(t);
- double y4 = (140 + (i + 1) * 2) + 30 * Math.sin(t);
- gc.drawImage(enermy, x4, y4);
- if (time.value > 11) {
- gc.setStroke(Color.PINK);
- gc.setLineWidth(5);
- gc.strokeLine(x4 + 30, y4 + 20, 370 + 50, 700 + 40);
- }
- }
- if (time.value > 20) {
- String resourceText = " You Lose";
- gc.fillText(resourceText, 1024 / 2 - 120, 768 / 2 - 80);
- gc.strokeText(resourceText, 1024 / 2 - 120, 768 / 2 - 80);
- resourceText = "Game Over";
- gc.fillText(resourceText, 1024 / 2 - 120, 768 / 2);
- gc.strokeText(resourceText, 1024 / 2 - 120, 768 / 2);
- time.value = time3.value;
- time2.value = time3.value;
- }
- Font theFont = Font.font("Helvetica", FontWeight.BOLD, 50);
- gc.setFont(theFont);
- gc.setStroke(Color.BLACK);
- gc.setLineWidth(1);
- gc.setFill(Color.RED);
- String resourceText = "Money: " + time2.value;
- gc.fillText(resourceText, 750 - 30, 56);
- gc.strokeText(resourceText, 750 - 30, 56);
- String timeText = "Time: " + time.value;
- gc.fillText(timeText, 490 - 30, 56);
- gc.strokeText(timeText, 490 - 30, 56);
- /*
- * gc.setStroke(Color.RED); gc.setLineWidth(5);
- * gc.strokeLine(x + 30, y + 20, 370 + 50, 700 + 40);
- *
- * gc.setStroke(Color.GREEN); gc.setLineWidth(5);
- * gc.strokeLine(x2 + 30, y2 + 20, 370 + 50, 700 + 40);
- *
- * gc.setStroke(Color.BLUE); gc.setLineWidth(5);
- * gc.strokeLine(x3 + 30, y3 + 20, 370 + 50, 700 + 40);
- *
- * gc.setStroke(Color.PINK); gc.setLineWidth(5);
- * gc.strokeLine(x4 + 30, y4 + 20, 370 + 50, 700 + 40);
- */
- }
- });
- gameLoop.getKeyFrames().add(kf);
- gameLoop.play();
- theStage.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement