jotto

Untitled

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