jotto

znika statek i pocisk, ale dupy nie urywa bo kolizja z plane

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