jotto

Działa wszystko

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