jotto

Dwa strzały, już po kilka shotów jest

Jan 10th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.24 KB | None | 0 0
  1. package riverpuff.v3;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import javafx.animation.AnimationTimer;
  9. import javafx.animation.Timeline;
  10. import javafx.animation.TranslateTransition;
  11. import javafx.application.Application;
  12. import javafx.event.ActionEvent;
  13. import javafx.geometry.Pos;
  14. import javafx.scene.Group;
  15. import javafx.scene.Scene;
  16. import javafx.scene.canvas.Canvas;
  17. import javafx.scene.canvas.GraphicsContext;
  18. import javafx.scene.control.Button;
  19. import javafx.scene.control.TextField;
  20. import javafx.scene.image.Image;
  21. import javafx.scene.image.ImageView;
  22. import javafx.scene.input.KeyCode;
  23. import javafx.scene.input.KeyEvent;
  24. import javafx.scene.layout.GridPane;
  25. import javafx.scene.paint.Color;
  26. import javafx.scene.shape.Rectangle;
  27. import javafx.scene.text.Font;
  28. import javafx.scene.text.Text;
  29. import javafx.stage.Stage;
  30. import javafx.util.Duration;
  31.  
  32. /**
  33.  *
  34.  * @author Marek
  35.  */
  36. public class RiverPuffV3 extends Application {
  37.    
  38.     public String name = "";
  39.     public Rectangle shot = new Rectangle();
  40.     public double plane_pos;
  41.    
  42.     @Override
  43.     public void start(Stage primaryStage) {
  44.  
  45.         createMenu(primaryStage);
  46.                
  47.         primaryStage.show();
  48.         primaryStage.setResizable(false);
  49.         primaryStage.getIcons().
  50.                 add(new Image(getClass().getResourceAsStream("Icon.png")));
  51.     }
  52.    
  53.     private void createMenu(Stage primaryStage) {
  54.        
  55.         primaryStage.setScene(null);
  56.        
  57.         GridPane pane_menu = new GridPane();
  58.                
  59.         Button button_name = new Button("Name");
  60.         button_name.setMaxHeight(Double.MAX_VALUE);
  61.         button_name.setMaxWidth(Double.MAX_VALUE);
  62.         button_name.setOnAction(e -> {
  63.             createName(primaryStage);
  64.             /*try{        
  65.                 OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("log.txt", true), "UTF-8");
  66.                 BufferedWriter fbw = new BufferedWriter(writer);
  67.                 fbw.newLine();
  68.                 fbw.write("append txt...");
  69.                 fbw.newLine();
  70.                 fbw.close();
  71.             }
  72.             catch (Exception v) {
  73.             System.out.println("Error: " + v.getMessage());
  74.             }*/
  75.         });
  76.        
  77.         Button button_start = new Button("Start");
  78.         button_start.setMaxHeight(Double.MAX_VALUE);
  79.         button_start.setMaxWidth(Double.MAX_VALUE);
  80.         button_start.setOnAction(e -> {
  81.             drawGame(primaryStage);
  82.         });
  83.        
  84.         pane_menu.setHgap(10);
  85.         pane_menu.setVgap(10);
  86.         pane_menu.add(button_name,0,10,10,10);
  87.         pane_menu.add(button_start,15,10,10,10);
  88.        
  89.                                                                                 //reading name from a file
  90.         try {
  91.             String read_file = null;
  92.             BufferedReader in = new BufferedReader(new FileReader("log.txt"));
  93.             read_file = in.readLine();
  94.             Text text_name = new Text("Hello " + read_file);
  95.             pane_menu.add(text_name,5,5,10,5);
  96.         } catch (FileNotFoundException e) {
  97.             System.out.println("File not found!");
  98.             //throw new RuntimeException("File not found");
  99.         } catch (IOException e) {
  100.             System.out.println("IO Error occured");
  101.             //throw new RuntimeException("IO Error occured");
  102.         }
  103.        
  104.         Scene scene_menu = new Scene(pane_menu, 300, 500);
  105.        
  106.         primaryStage.setTitle("River Puff");
  107.         primaryStage.setScene(scene_menu);
  108.        
  109.     }
  110.    
  111.                                                                                 //save name to a file
  112.     private void createName (Stage primaryStage) {
  113.        
  114.         primaryStage.setScene(null);
  115.        
  116.         GridPane pane_name = new GridPane();
  117.        
  118.         TextField tf_name = new TextField();
  119.         tf_name.setMaxHeight(50);
  120.         tf_name.setMaxWidth(240);
  121.         tf_name.setAlignment(Pos.CENTER);
  122.         tf_name.setFont(Font.font("Verdana",25));
  123.         tf_name.setOnKeyPressed(ke -> {
  124.             if (ke.getCode() == KeyCode.ENTER) {
  125.                 name = tf_name.getText();
  126.                 if (!name.isEmpty()){
  127.                     MyFile myFile = new MyFile();
  128.                     myFile.writeTextFile("log.txt", name);
  129.                 }
  130.                 createMenu(primaryStage);
  131.             }
  132.         });
  133.         Button button_ok = new Button("OK");        
  134.         button_ok.setMaxHeight(30);
  135.         button_ok.setMaxWidth(80);
  136.         button_ok.setOnAction(e -> {
  137.             name = tf_name.getText();
  138.             if (!name.isEmpty()){
  139.                 MyFile myFile = new MyFile();
  140.                 myFile.writeTextFile("log.txt", name);
  141.             }
  142.             createMenu(primaryStage);
  143.         });
  144.        
  145.         Text text_name = new Text("What is your name?");
  146.         text_name.setFont(Font.font("Verdana",15));
  147.        
  148.         pane_name.setHgap(10);
  149.         pane_name.setVgap(10);
  150.        
  151.         pane_name.add(text_name,8,9,5,5);
  152.         pane_name.add(button_ok,11,22,8,3);
  153.         pane_name.add(tf_name,3,15,24,5);
  154.        
  155.         Scene scene_name = new Scene(pane_name, 300, 500);
  156.        
  157.         primaryStage.setTitle("River Puff - Name");
  158.         primaryStage.setScene(scene_name);
  159.                
  160.     }
  161.    
  162.     private void drawGame (Stage primaryStage) {
  163.        
  164.         primaryStage.setScene(null);
  165.        
  166.         final int H = 700;
  167.         final int W = 1000;
  168.        
  169.         Group root = new Group();
  170.         Scene scene_game = new Scene(root, W, H, Color.LIGHTBLUE);
  171.        
  172.         Button button_menu = new Button("Menu");
  173.         button_menu.setOnAction(e ->{
  174.             createMenu(primaryStage);
  175.         });
  176.        
  177.         Image ship = new Image((getClass().getResourceAsStream("ship.png")));   //loading images
  178.         Image plane = new Image((getClass().getResourceAsStream("Icon.png")));
  179.        
  180.         /**********************************************************************/
  181.                
  182.         Rectangle[] coastL = {
  183.             new Rectangle(), new Rectangle(),
  184.             new Rectangle(), new Rectangle(),
  185.             new Rectangle(), new Rectangle(),
  186.             new Rectangle(), new Rectangle()
  187.         };
  188.         Rectangle[] coastR = {            
  189.             new Rectangle(), new Rectangle(),
  190.             new Rectangle(), new Rectangle(),
  191.             new Rectangle(), new Rectangle(),
  192.             new Rectangle(), new Rectangle()
  193.         };
  194.        
  195.        
  196.         for (int i=0; i<8; i++) {
  197.             coastL[i].setFill(Color.FORESTGREEN);
  198.             coastL[i].setHeight(100);
  199.             coastR[i].setFill(Color.FORESTGREEN);
  200.             coastR[i].setHeight(100);
  201.         }
  202.        
  203.        
  204.         AnimationTimer timer = new AnimationTimer() {
  205.             int[] j = {0,0,0,0,0,0,0,0};
  206.            
  207.             @Override
  208.             public void handle(long now) {
  209.                
  210.                 for (int i=0; i<8; i++) if (j[i]==(i*100+800)) j[i]=i*100;
  211.                
  212.                 for (int i=1;i<9;i++) {                                         //creating coast
  213.                     coastL[i-1].setX(0);
  214.                     coastL[i-1].setY(j[i-1]-(i*100));
  215.                     coastL[i-1].setWidth(250+i*(i%3));
  216.                    
  217.                     coastR[i-1].setX(W-(250+i*(i%4)));
  218.                     coastR[i-1].setY(j[i-1]-(i*100));
  219.                     coastR[i-1].setWidth(250+i*(i%4));
  220.                 }
  221.                 for (int i=0;i<8;i++) j[i]++;
  222.             }
  223.         };
  224.         timer.start();
  225.        
  226.         ImageView iv_ship = new ImageView();
  227.         iv_ship.setImage(ship);
  228.         iv_ship.setFitWidth(150);
  229.         iv_ship.setFitHeight(45);
  230.         iv_ship.setX(300);
  231.         iv_ship.setY(0);
  232.        
  233.         TranslateTransition tt_shipX =
  234.                 new TranslateTransition(Duration.millis(2000), iv_ship);        //moving enemies
  235.         tt_shipX.setAutoReverse(true);
  236.         tt_shipX.setCycleCount(Timeline.INDEFINITE);        
  237.         tt_shipX.setByX(200f);
  238.         tt_shipX.play();    
  239.        
  240.         TranslateTransition tt_shipY =
  241.                 new TranslateTransition(Duration.millis(13000), iv_ship);
  242.         tt_shipY.setAutoReverse(false);
  243.         tt_shipY.setCycleCount(Timeline.INDEFINITE);
  244.         tt_shipY.setByY(800);
  245.         tt_shipY.play();        
  246.        
  247.         ImageView iv_plane = new ImageView();
  248.         iv_plane.setImage(plane);
  249.         iv_plane.setFitWidth(50);
  250.         iv_plane.setFitHeight(50);
  251.         iv_plane.setX(475);
  252.         iv_plane.setY(600);
  253.        
  254.         TranslateTransition tt_plane =
  255.                 new TranslateTransition(Duration.millis(1), iv_plane);          
  256.                
  257.         TranslateTransition tt_shot =
  258.                 new TranslateTransition(Duration.millis(4000), shot);
  259.         tt_shot.setAutoReverse(false);
  260.         tt_shot.setCycleCount(1);
  261.         TranslateTransition tt_shotB =
  262.                 new TranslateTransition(Duration.millis(0.5f), shot);
  263.         tt_shotB.setAutoReverse(false);
  264.         tt_shotB.setCycleCount(1);
  265.        
  266.        
  267.        
  268.         root.setOnKeyPressed((KeyEvent ke) -> {                                 //steering a plane            }
  269.             if (ke.getCode() == KeyCode.LEFT &&
  270.                     tt_plane.getNode().getTranslateX() > -475) {
  271.                 tt_plane.setByX(-5f);
  272.                 tt_plane.play();
  273.                 System.out.println(tt_plane.getNode().getTranslateX());
  274.             }
  275.             else if (ke.getCode() == KeyCode.RIGHT &&
  276.                     tt_plane.getNode().getTranslateX() < 475) {
  277.                 tt_plane.setByX(5f);
  278.                 tt_plane.play();
  279.                 System.out.println(tt_plane.getNode().getTranslateX());
  280.             }
  281.             else if (ke.getCode() == KeyCode.A) {
  282.                 shot(tt_plane.getNode().getTranslateX()+495);
  283.                 root.getChildren().add(shot(tt_plane.getNode().getTranslateX()+495));                        
  284.                
  285.                 /*
  286.                 shot.setFill(Color.BLACK);
  287.                 shot.setX(tt_plane.getNode().getTranslateX()+495);
  288.                 shot.setY(580);
  289.                 shot.setWidth(10);
  290.                 shot.setHeight(20);
  291.                
  292.                 tt_shot.setByY(-600);
  293.                 tt_shot.play();
  294.                 tt_shot.setOnFinished((ActionEvent arg0) -> {
  295.                     shot.setDisable(true);
  296.                     tt_shotB.setByY(600);
  297.                     tt_shotB.play();
  298.                 });*/
  299.             }
  300.         });
  301.        
  302.                
  303.         if (iv_ship.intersects(iv_plane.getBoundsInLocal())) System.out.println("xxxxxxxxx");
  304.         if (iv_plane.intersects(iv_ship.getBoundsInLocal())) System.out.println("zzzz");
  305.         if (iv_plane.getBoundsInLocal().intersects(iv_ship.getBoundsInLocal())) System.out.println("dupa");
  306.        
  307.         for (int i=0; i<8; i++) {
  308.             root.getChildren().add(coastL[i]);
  309.             root.getChildren().add(coastR[i]);
  310.         }
  311.         root.getChildren().add(iv_plane);
  312.         root.getChildren().add(iv_ship);
  313.         //root.getChildren().add(shot);
  314.        
  315.         root.getChildren().add(button_menu);
  316.         primaryStage.setScene(scene_game);
  317.     }
  318.    
  319.     private Rectangle shot(double pos) {
  320.                
  321.         int i=0;
  322.         ArrayList<Rectangle> al_shot = new ArrayList<>();
  323.        
  324.         al_shot.add(new Rectangle());
  325.         al_shot.get(i).setFill(Color.BLACK);
  326.         al_shot.get(i).setX(pos);
  327.         al_shot.get(i).setWidth(10);
  328.         al_shot.get(i).setHeight(20);
  329.        
  330.        
  331.         AnimationTimer timer = new AnimationTimer() {
  332.             int j=0;
  333.             @Override
  334.             public void handle(long now) {                
  335.                 al_shot.get(i).setY(580-j);
  336.                 j++;
  337.                 if (j==600) super.stop();
  338.             }
  339.         };
  340.         timer.start();
  341.         return al_shot.get(i);
  342.     }
  343.  
  344.     /**
  345.      * @param args the command line arguments
  346.      */
  347.     public static void main(String[] args) {
  348.         launch(args);
  349.     }
  350.    
  351. }
Advertisement
Add Comment
Please, Sign In to add comment