Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.stage.Stage;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.*;
  5. import javafx.scene.layout.Pane;
  6. import javafx.scene.shape.Circle;
  7. import javafx.animation.*;
  8. import javafx.util.Duration;
  9. public class functionalShooter extends Application {
  10.     private String currentDirection = "W";
  11.     public static void main(String[] args){ launch(args);   }
  12.     @Override public void start(Stage stage) throws Exception{
  13.         Pane root = new Pane();
  14.         Scene scene = new Scene(root, 600, 400);
  15.         stage.setScene(scene);
  16.         stage.setResizable(false);
  17.         stage.show();
  18.        
  19.         Circle player = new Circle(10);
  20.         player.setLayoutX(300);
  21.         player.setLayoutY(200);
  22.        
  23.         Circle other = new Circle(100, 100, 10);
  24.        
  25.         root.getChildren().addAll(player, other);
  26.        
  27.         //key events
  28.         scene.setOnKeyPressed(event -> {
  29.             switch(event.getCode()){
  30.                 case A:
  31.                     player.setLayoutX(player.getLayoutX()-10);
  32.                     currentDirection = "A";
  33.                     break;
  34.                 case D:
  35.                     player.setLayoutX(player.getLayoutX()+10);
  36.                     currentDirection = "D";
  37.                     break;
  38.                 case W:
  39.                     player.setLayoutY(player.getLayoutY()-10);
  40.                     currentDirection = "W";
  41.                     break;
  42.                 case S:
  43.                     player.setLayoutY(player.getLayoutY()+10);
  44.                     currentDirection = "S";
  45.                     break;
  46.                 case SPACE:
  47.                     if (currentDirection.equals("D")){
  48.                         Circle newCircle = new Circle(player.getLayoutX()+10, player.getLayoutY(), 5);
  49.                         root.getChildren().add(newCircle);
  50.                         shoot(newCircle);
  51.                     }
  52.                     else if (currentDirection.equals("A")){
  53.                         Circle newCircle = new Circle(player.getLayoutX()-10, player.getLayoutY(), 5);
  54.                         root.getChildren().add(newCircle);
  55.                         shoot(newCircle);
  56.                     }
  57.                     else if (currentDirection.equals("S")){
  58.                         Circle newCircle = new Circle(player.getLayoutX(), player.getLayoutY()+10, 5);
  59.                         root.getChildren().add(newCircle);
  60.                         shoot(newCircle);
  61.                     }
  62.                     else {
  63.                         Circle newCircle = new Circle(player.getLayoutX(), player.getLayoutY()-10, 5);
  64.                         root.getChildren().add(newCircle);
  65.                         shoot(newCircle);
  66.                     }
  67.                     break;
  68.             }
  69.         });
  70.     }
  71.     private void shoot(Circle bullet){
  72.         Timeline timeline = new Timeline();
  73.         if (currentDirection.equals("D")){
  74.             KeyValue start = new KeyValue(bullet.translateXProperty(), 0);
  75.             KeyValue end = new KeyValue(bullet.translateXProperty(), 800);
  76.             KeyFrame startF = new KeyFrame(Duration.ZERO, start);
  77.             KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
  78.             timeline.getKeyFrames().addAll(startF, endF);
  79.         }
  80.         else if (currentDirection.equals("A")){
  81.             KeyValue start = new KeyValue(bullet.translateXProperty(), 0);
  82.             KeyValue end = new KeyValue(bullet.translateXProperty(), -800);
  83.             KeyFrame startF = new KeyFrame(Duration.ZERO, start);
  84.             KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
  85.             timeline.getKeyFrames().addAll(startF, endF);
  86.         }
  87.         else if (currentDirection.equals("S")){
  88.             KeyValue start = new KeyValue(bullet.translateYProperty(), 0);
  89.             KeyValue end = new KeyValue(bullet.translateYProperty(), 800);
  90.             KeyFrame startF = new KeyFrame(Duration.ZERO, start);
  91.             KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
  92.             timeline.getKeyFrames().addAll(startF, endF);
  93.         }
  94.         else{
  95.             KeyValue start = new KeyValue(bullet.translateYProperty(), 0);
  96.             KeyValue end = new KeyValue(bullet.translateYProperty(), -800);
  97.             KeyFrame startF = new KeyFrame(Duration.ZERO, start);
  98.             KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
  99.             timeline.getKeyFrames().addAll(startF, endF);
  100.         }
  101.         timeline.setAutoReverse(false);
  102.         timeline.setCycleCount(1);
  103.         timeline.play();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement