jotto

warunki

Jan 11th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.64 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 static javafx.application.Platform.exit;
  14. import javafx.geometry.Bounds;
  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.layout.GridPane;
  24. import javafx.scene.media.Media;
  25. import javafx.scene.media.MediaPlayer;
  26. import javafx.scene.paint.Color;
  27. import javafx.scene.shape.Rectangle;
  28. import javafx.scene.text.Font;
  29. import javafx.scene.text.Text;
  30. import javafx.stage.Stage;
  31. import javafx.util.Duration;
  32.  
  33. /**
  34.  * Main class of game
  35.  * @author Marek
  36.  */
  37. public class RiverPuffV3 extends Application {
  38.    
  39.     public String name = "";
  40.     public double plane_pos;
  41.     public int i_shot = 0;
  42.     public TranslateTransition tt_shipY;
  43.     public boolean isOver = false;
  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.      * Creates menu window with buttons and greetings
  57.      * @param primaryStage
  58.      */
  59.     private void createMenu(Stage primaryStage) {
  60.        
  61.         primaryStage.setScene(null);
  62.        
  63.         GridPane pane_menu = new GridPane();
  64.                
  65.         Button button_name = new Button("Name");
  66.         button_name.setMaxHeight(Double.MAX_VALUE);
  67.         button_name.setMaxWidth(Double.MAX_VALUE);
  68.         button_name.setOnAction(e -> {
  69.             createName(primaryStage);
  70.         });
  71.        
  72.         Button button_start = new Button("Start");
  73.         button_start.setMaxHeight(Double.MAX_VALUE);
  74.         button_start.setMaxWidth(Double.MAX_VALUE);
  75.         button_start.setOnAction(e -> {
  76.             drawGame(primaryStage);
  77.         });
  78.        
  79.         Button button_exit = new Button("Exit");
  80.         button_exit.setMaxHeight(Double.MAX_VALUE);
  81.         button_exit.setMaxWidth(Double.MAX_VALUE);
  82.         button_exit.setOnAction(e -> {
  83.             exit();
  84.         });
  85.        
  86.         pane_menu.setHgap(10);
  87.         pane_menu.setVgap(10);
  88.         pane_menu.add(button_name,11,10,10,10);
  89.         pane_menu.add(button_start,11,20,10,10);
  90.         pane_menu.add(button_exit,11,30,10,10);
  91.        
  92.         if (isOver){
  93.             isOver = false;
  94.             Text text_over = new Text("Game Over");
  95.             pane_menu.add(text_over, 12, 40, 10, 5);
  96.         }
  97.                                                                                 //reading name from a file
  98.         try {
  99.             String read_file = null;
  100.             BufferedReader in = new BufferedReader(new FileReader("log.txt"));
  101.             read_file = in.readLine();
  102.             Text text_name = new Text("Hello " + read_file);
  103.             pane_menu.add(text_name,12,5,10,5);
  104.         } catch (FileNotFoundException e) {
  105.             System.out.println("File not found!");
  106.             //throw new RuntimeException("File not found");
  107.         } catch (IOException e) {
  108.             System.out.println("IO Error occured");
  109.             //throw new RuntimeException("IO Error occured");
  110.         }
  111.        
  112.         Scene scene_menu = new Scene(pane_menu, 300, 500);
  113.        
  114.         primaryStage.setTitle("River Puff");
  115.         primaryStage.setScene(scene_menu);
  116.        
  117.     }
  118.    
  119.     /**
  120.      * Creates window for name provide and saves it to a file
  121.      * @param primaryStage
  122.      */
  123.     private void createName (Stage primaryStage) {
  124.        
  125.         primaryStage.setScene(null);
  126.        
  127.         GridPane pane_name = new GridPane();
  128.        
  129.         TextField tf_name = new TextField();
  130.         tf_name.setMaxHeight(50);
  131.         tf_name.setMaxWidth(240);
  132.         tf_name.setAlignment(Pos.CENTER);
  133.         tf_name.setFont(Font.font("Verdana",25));
  134.         tf_name.setOnKeyPressed(ke -> {
  135.             if (ke.getCode() == KeyCode.ENTER) {
  136.                 name = tf_name.getText();
  137.                 if (!name.isEmpty()){
  138.                     MyFile myFile = new MyFile();
  139.                     myFile.writeTextFile("log.txt", name);
  140.                 }
  141.                 createMenu(primaryStage);
  142.             }
  143.         });
  144.         Button button_ok = new Button("OK");        
  145.         button_ok.setMaxHeight(30);
  146.         button_ok.setMaxWidth(80);
  147.         button_ok.setOnAction(e -> {
  148.             name = tf_name.getText();
  149.             if (!name.isEmpty()){
  150.                 MyFile myFile = new MyFile();
  151.                 myFile.writeTextFile("log.txt", name);
  152.             }
  153.             createMenu(primaryStage);
  154.         });
  155.        
  156.         Text text_name = new Text("What is your name?");
  157.         text_name.setFont(Font.font("Verdana",15));
  158.        
  159.         pane_name.setHgap(10);
  160.         pane_name.setVgap(10);
  161.        
  162.         pane_name.add(text_name,8,9,5,5);
  163.         pane_name.add(button_ok,11,22,8,3);
  164.         pane_name.add(tf_name,3,15,24,5);
  165.        
  166.         Scene scene_name = new Scene(pane_name, 300, 500);
  167.        
  168.         primaryStage.setTitle("River Puff - Name");
  169.         primaryStage.setScene(scene_name);
  170.                
  171.     }
  172.    
  173.     /**
  174.      * Draws a game with main game loop, collision detection, playing sounds
  175.      * @param primaryStage
  176.      */
  177.     private void drawGame (Stage primaryStage) {
  178.        
  179.         final int H = 700;
  180.         final int W = 1000;
  181.        
  182.         primaryStage.setScene(null);
  183.         Group root = new Group();
  184.         Scene scene_game = new Scene(root, W, H, Color.LIGHTBLUE);
  185.        
  186.         Button button_menu = new Button("Menu");
  187.  
  188.        
  189.         Image ship = new Image((getClass().getResourceAsStream("ship.png")));   //loading images
  190.         Image plane = new Image((getClass().getResourceAsStream("Icon.png")));
  191.                
  192.         Rectangle[] coastL = {
  193.             new Rectangle(), new Rectangle(),
  194.             new Rectangle(), new Rectangle(),
  195.             new Rectangle(), new Rectangle(),
  196.             new Rectangle(), new Rectangle()
  197.         };
  198.         Rectangle[] coastR = {            
  199.             new Rectangle(), new Rectangle(),
  200.             new Rectangle(), new Rectangle(),
  201.             new Rectangle(), new Rectangle(),
  202.             new Rectangle(), new Rectangle()
  203.         };
  204.        
  205.         for (int i=0; i<8; i++) {
  206.             coastL[i].setFill(Color.FORESTGREEN);
  207.             coastL[i].setHeight(100);
  208.             coastR[i].setFill(Color.FORESTGREEN);
  209.             coastR[i].setHeight(100);
  210.         }
  211.                
  212.         ImageView iv_ship = new ImageView();
  213.         iv_ship.setImage(ship);
  214.         iv_ship.setFitWidth(150);
  215.         iv_ship.setFitHeight(45);
  216.         iv_ship.setX(300);
  217.         iv_ship.setY(0);
  218.        
  219.         ImageView iv_plane = new ImageView();
  220.         iv_plane.setImage(plane);
  221.         iv_plane.setFitWidth(50);
  222.         iv_plane.setFitHeight(50);
  223.         iv_plane.setX(475);
  224.         iv_plane.setY(600);
  225.        
  226.         TranslateTransition tt_plane =
  227.                 new TranslateTransition(Duration.millis(1), iv_plane);
  228.        
  229.         TranslateTransition tt_shipX =
  230.                 new TranslateTransition(Duration.millis(2000), iv_ship);
  231.         tt_shipX.setAutoReverse(true);
  232.         tt_shipX.setCycleCount(Timeline.INDEFINITE);        
  233.         tt_shipX.setByX(200f);
  234.         tt_shipX.play();    
  235.        
  236.         tt_shipY = new TranslateTransition(Duration.millis(13000), iv_ship);
  237.         tt_shipY.setAutoReverse(false);
  238.         tt_shipY.setCycleCount(Timeline.INDEFINITE);
  239.         tt_shipY.setByY(800);
  240.         tt_shipY.play();
  241.         tt_shipY.setOnFinished(arg0 -> {
  242.             tt_shipY.getNode().setVisible(true);
  243.         });
  244.        
  245.         Media hit = new Media(new File("bomba.mp3").toURI().toString());
  246.         MediaPlayer MP = new MediaPlayer(hit);
  247.        
  248.         Bounds planeBoundsInParent = tt_plane.getNode().getBoundsInParent();
  249.         Bounds shipBoundsInParent = tt_shipY.getNode().getBoundsInParent();
  250.        
  251.         ArrayList<Bounds> clBIP = new ArrayList<>();
  252.         ArrayList<Bounds> crBIP = new ArrayList<>();
  253.        
  254.         for (int i=0;i<8;i++) {
  255.             clBIP.add(coastL[i].getBoundsInParent());
  256.             crBIP.add(coastR[i].getBoundsInParent());
  257.         }
  258.        
  259.         AnimationTimer timer = new AnimationTimer() {
  260.             int[] j = {0,0,0,0,0,0,0,0,0};
  261.            
  262.             @Override
  263.             public void handle(long now) {
  264.                
  265.                 for (int i=0; i<8; i++) if (j[i]==(i*100+800)) j[i]=i*100;                
  266.                
  267.                 for (int i=1;i<9;i++) {                                         //creating coast
  268.                     coastL[i-1].setX(0);
  269.                     coastL[i-1].setY(j[i-1]-(i*100));
  270.                     coastL[i-1].setWidth(250+i*(i%3));
  271.                    
  272.                     coastR[i-1].setX(W-(250+i*(i%4)));
  273.                     coastR[i-1].setY(j[i-1]-(i*100));
  274.                     coastR[i-1].setWidth(250+i*(i%4));
  275.                 }
  276.                 for (int i=0;i<9;i++) j[i]++; //inkrementacja wszystkich j
  277.                                
  278.                 for (int i=0;i<8;i++) {
  279.                     if (tt_plane.getNode()
  280.                                 .getBoundsInParent()
  281.                                 .intersects(tt_shipY.getNode()
  282.                                         .getBoundsInParent()) ||
  283.                         tt_plane.getNode()
  284.                                 .getBoundsInParent()
  285.                                 .intersects(coastL[i]
  286.                                         .getBoundsInParent()) ||
  287.                         tt_plane.getNode()
  288.                                 .getBoundsInParent()
  289.                                 .intersects(coastR[i]
  290.                                         .getBoundsInParent())) {
  291.                         MP.play();
  292.                         isOver = true;
  293.                         super.stop();
  294.                         tt_plane.stop();
  295.                         tt_shipX.stop();
  296.                         tt_shipY.stop();
  297.                         createMenu(primaryStage);
  298.                     }
  299.                     else MP.stop();
  300.                 }
  301.             }
  302.         };
  303.         timer.start();
  304.        
  305.         root.setOnKeyPressed(ke -> {                                            //steering a plane
  306.             if (ke.getCode() == KeyCode.LEFT &&
  307.                     tt_plane.getNode().getTranslateX() > -475) {
  308.                 tt_plane.setByX(-5f);
  309.                 tt_plane.play();
  310.                 System.out.println(tt_plane.getNode().getTranslateX());
  311.             }
  312.             else if (ke.getCode() == KeyCode.RIGHT &&
  313.                     tt_plane.getNode().getTranslateX() < 475) {
  314.                 tt_plane.setByX(5f);
  315.                 tt_plane.play();
  316.                 System.out.println(tt_plane.getNode().getTranslateX());
  317.             }
  318.             else if (ke.getCode() == KeyCode.A) {
  319.                 shot(tt_plane.getNode().getTranslateX()+495);
  320.                 root.getChildren()
  321.                         .add(shot(tt_plane.getNode().getTranslateX()+495));
  322.             }
  323.         });
  324.        
  325.         button_menu.setOnAction(e ->{
  326.             timer.stop();
  327.             tt_plane.stop();
  328.             tt_shipX.stop();
  329.             tt_shipY.stop();
  330.             createMenu(primaryStage);
  331.         });
  332.        
  333.         for (int i=0; i<8; i++) {
  334.             root.getChildren().add(coastL[i]);
  335.             root.getChildren().add(coastR[i]);
  336.         }
  337.         root.getChildren().add(iv_plane);
  338.         root.getChildren().add(iv_ship);
  339.        
  340.         root.getChildren().add(button_menu);
  341.         primaryStage.setScene(scene_game);
  342.     }
  343.    
  344.     /**
  345.      * Creates a shot in front of plane ane moves it forward
  346.      * @param pos
  347.      * @return
  348.      */
  349.     private Rectangle shot(double pos) {
  350.        
  351.         ArrayList<Rectangle> al_shot = new ArrayList<>();
  352.        
  353.         Media hit = new Media(new File("bomba.mp3").toURI().toString());
  354.         MediaPlayer MP = new MediaPlayer(hit);
  355.        
  356.         al_shot.add(new Rectangle());
  357.         al_shot.get(i_shot).setFill(Color.BLACK);
  358.         al_shot.get(i_shot).setX(pos);
  359.         al_shot.get(i_shot).setWidth(10);
  360.         al_shot.get(i_shot).setHeight(20);
  361.        
  362.        
  363.         AnimationTimer timer = new AnimationTimer() {
  364.             int j=0;
  365.             @Override
  366.             public void handle(long now) {                
  367.                 al_shot.get(i_shot).setY(580-j);
  368.                
  369.                 if (tt_shipY.getNode().getBoundsInParent()
  370.                         .intersects(al_shot.get(i_shot)
  371.                                 .getBoundsInParent())) {
  372.                     MP.play();
  373.                     al_shot.get(i_shot).setFill(Color.TRANSPARENT);
  374.                     tt_shipY.getNode().setVisible(false);
  375.                 }
  376.                 else MP.stop();
  377.                
  378.                 j++;
  379.                 if (j==600) super.stop();
  380.             }
  381.         };
  382.         timer.start();
  383.         return al_shot.get(i_shot);
  384.     }
  385.  
  386.     /**
  387.      * @param args the command line arguments
  388.      */
  389.     public static void main(String[] args) {
  390.         launch(args);
  391.     }
  392.    
  393. }
Advertisement
Add Comment
Please, Sign In to add comment