Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package riverpuff.v3;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import javafx.animation.AnimationTimer;
- import javafx.animation.Timeline;
- import javafx.animation.TranslateTransition;
- import javafx.application.Application;
- import static javafx.application.Platform.exit;
- import javafx.geometry.Bounds;
- import javafx.geometry.Pos;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.GridPane;
- import javafx.scene.media.Media;
- import javafx.scene.media.MediaPlayer;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- /**
- * Main class of game
- * @author Marek
- */
- public class RiverPuffV3 extends Application {
- public static String name = "";
- //public double plane_pos;
- private final int i_shot = 0;
- private TranslateTransition tt_shipY;
- public static boolean isOver = false;
- public static int points = 0;
- public static double tempPoints = 0;
- public static Group root;
- @Override
- public void start(Stage primaryStage) {
- createMenu(primaryStage);
- primaryStage.show();
- primaryStage.setResizable(false);
- primaryStage.getIcons().
- add(new Image(getClass().getResourceAsStream("Icon.png")));
- }
- /**
- * Creates menu window with buttons and greetings
- * @param primaryStage
- */
- public void createMenu(Stage primaryStage) {
- primaryStage.setScene(null);
- GridPane pane_menu = new GridPane();
- Button button_name = new Button("Name");
- button_name.setMaxHeight(Double.MAX_VALUE);
- button_name.setMaxWidth(Double.MAX_VALUE);
- button_name.setOnAction(e -> {
- createName(primaryStage);
- });
- Button button_start = new Button("Start");
- button_start.setMaxHeight(Double.MAX_VALUE);
- button_start.setMaxWidth(Double.MAX_VALUE);
- button_start.setOnAction(e -> {
- drawGame(primaryStage);
- });
- Button button_exit = new Button("Exit");
- button_exit.setMaxHeight(Double.MAX_VALUE);
- button_exit.setMaxWidth(Double.MAX_VALUE);
- button_exit.setOnAction(e -> {
- exit();
- });
- pane_menu.setHgap(10);
- pane_menu.setVgap(10);
- pane_menu.add(button_name,11,10,10,10);
- pane_menu.add(button_start,11,20,10,10);
- pane_menu.add(button_exit,11,30,10,10);
- if (isOver){
- isOver = false;
- Text text_over = new Text("Game Over");
- pane_menu.add(text_over, 12, 40, 10, 5);
- }
- //reading name from a file
- try {
- String read_file = null;
- BufferedReader in = new BufferedReader(new FileReader("log.txt"));
- read_file = in.readLine();
- name = read_file;
- Text text_name = new Text("Hello \n" + read_file);
- text_name.setId("menutext");
- pane_menu.add(text_name,12,2,10,5);
- } catch (FileNotFoundException e) {
- System.out.println("File not found!");
- //throw new RuntimeException("File not found");
- } catch (IOException e) {
- System.out.println("IO Error occured");
- //throw new RuntimeException("IO Error occured");
- }
- Scene scene_menu = new Scene(pane_menu, 300, 500);
- scene_menu.getStylesheets().add(riverpuff.v3.RiverPuffV3
- .class.getResource("stylesheet.css").toExternalForm());
- primaryStage.setTitle("River Puff");
- primaryStage.setScene(scene_menu);
- }
- /**
- * Creates window for name provide and saves it to a file
- * @param primaryStage
- */
- private void createName (Stage primaryStage) {
- primaryStage.setScene(null);
- GridPane pane_name = new GridPane();
- TextField tf_name = new TextField();
- tf_name.setMaxHeight(50);
- tf_name.setMaxWidth(240);
- tf_name.setAlignment(Pos.CENTER);
- tf_name.setFont(Font.font("Verdana",25));
- tf_name.setOnKeyPressed(ke -> {
- if (ke.getCode() == KeyCode.ENTER) {
- name = tf_name.getText();
- if (!name.isEmpty()){
- MyFile myFile = new MyFile();
- myFile.writeTextFile("log.txt", name);
- }
- createMenu(primaryStage);
- }
- });
- Button button_ok = new Button("OK");
- button_ok.setMaxHeight(30);
- button_ok.setMaxWidth(80);
- button_ok.setOnAction(e -> {
- name = tf_name.getText();
- if (!name.isEmpty()){
- MyFile myFile = new MyFile();
- myFile.writeTextFile("log.txt", name);
- }
- createMenu(primaryStage);
- });
- Text text_name = new Text("What is your name?");
- text_name.setFont(Font.font("Verdana",15));
- pane_name.setHgap(10);
- pane_name.setVgap(10);
- pane_name.add(text_name,8,9,5,5);
- pane_name.add(button_ok,11,22,8,3);
- pane_name.add(tf_name,3,15,24,5);
- Scene scene_name = new Scene(pane_name, 300, 500);
- primaryStage.setTitle("River Puff - Name");
- primaryStage.setScene(scene_name);
- }
- /**
- * Draws a game with main game loop, collision detection, playing sounds
- * @param primaryStage
- */
- private void drawGame (Stage primaryStage) {
- final int H = 700;
- final int W = 1000;
- primaryStage.setScene(null);
- root = new Group();
- Scene scene_game = new Scene(root, W, H, Color.LIGHTBLUE);
- File f = new File("stylesheet.css");
- scene_game.getStylesheets().add(riverpuff.v3.RiverPuffV3
- .class.getResource("stylesheet.css").toExternalForm()); //import css
- Button button_menu = new Button("Menu");
- Image ship = new Image((getClass().getResourceAsStream("ship.png"))); //loading images
- Image plane = new Image((getClass().getResourceAsStream("Icon.png")));
- tempPoints = 0; //zerowanie punktów
- Rectangle[] coastL = {
- new Rectangle(), new Rectangle(),
- new Rectangle(), new Rectangle(),
- new Rectangle(), new Rectangle(),
- new Rectangle(), new Rectangle()
- };
- Rectangle[] coastR = {
- new Rectangle(), new Rectangle(),
- new Rectangle(), new Rectangle(),
- new Rectangle(), new Rectangle(),
- new Rectangle(), new Rectangle()
- };
- for (int i=0; i<8; i++) {
- coastL[i].setFill(Color.FORESTGREEN);
- coastL[i].setHeight(100);
- coastR[i].setFill(Color.FORESTGREEN);
- coastR[i].setHeight(100);
- }
- ImageView iv_ship = new ImageView();
- iv_ship.setImage(ship);
- iv_ship.setFitWidth(150);
- iv_ship.setFitHeight(45);
- iv_ship.setX(300);
- iv_ship.setY(0);
- ImageView iv_plane = new ImageView();
- iv_plane.setImage(plane);
- iv_plane.setFitWidth(50);
- iv_plane.setFitHeight(50);
- iv_plane.setX(475);
- iv_plane.setY(600);
- TranslateTransition tt_plane =
- new TranslateTransition(Duration.millis(1), iv_plane);
- TranslateTransition tt_shipX =
- new TranslateTransition(Duration.millis(2000), iv_ship);
- tt_shipX.setAutoReverse(true);
- tt_shipX.setCycleCount(Timeline.INDEFINITE);
- tt_shipX.setByX(200f);
- tt_shipX.play();
- tt_shipY = new TranslateTransition(Duration.millis(13000), iv_ship);
- tt_shipY.setAutoReverse(false);
- tt_shipY.setCycleCount(Timeline.INDEFINITE);
- tt_shipY.setByY(800);
- tt_shipY.play();
- tt_shipY.setOnFinished(arg0 -> {
- tt_shipY.getNode().setVisible(true);
- System.out.println("hehe");
- });
- ArrayList<Bounds> clBIP = new ArrayList<>();
- ArrayList<Bounds> crBIP = new ArrayList<>();
- mainLoop gameLoop = new mainLoop(
- coastL,
- coastR,
- tt_plane,
- tt_shipY,
- tt_shipX,
- clBIP,
- crBIP,
- primaryStage,
- W);
- gameLoop.start();
- root.setOnKeyPressed(ke -> { //steering a plane
- if (ke.getCode() == KeyCode.LEFT &&
- tt_plane.getNode().getTranslateX() > -475) {
- tt_plane.setByX(-5f);
- tt_plane.play();
- System.out.println(tt_plane.getNode().getTranslateX());
- }
- else if (ke.getCode() == KeyCode.RIGHT &&
- tt_plane.getNode().getTranslateX() < 475) {
- tt_plane.setByX(5f);
- tt_plane.play();
- System.out.println(tt_plane.getNode().getTranslateX());
- }
- else if (ke.getCode() == KeyCode.A) {
- shot(tt_plane.getNode().getTranslateX()+495);
- root.getChildren()
- .add(shot(tt_plane.getNode().getTranslateX()+495));
- }
- });
- button_menu.setOnAction(e ->{
- gameLoop.stop();
- tt_plane.stop();
- tt_shipX.stop();
- tt_shipY.stop();
- createMenu(primaryStage);
- });
- for (int i=0; i<8; i++) {
- root.getChildren().add(coastL[i]);
- root.getChildren().add(coastR[i]);
- }
- root.getChildren().add(iv_plane);
- root.getChildren().add(iv_ship);
- root.getChildren().add(mainLoop.tPoints);
- root.getChildren().add(button_menu);
- primaryStage.setScene(scene_game);
- }
- /**
- * Creates a shot in front of plane ane moves it forward
- * @param pos
- * @return
- */
- private Rectangle shot(double pos) {
- ArrayList<Rectangle> al_shot = new ArrayList<>();
- Media hit = new Media(new File("bomba.mp3").toURI().toString());
- MediaPlayer MP = new MediaPlayer(hit);
- al_shot.add(new Rectangle());
- al_shot.get(i_shot).setFill(Color.BLACK);
- al_shot.get(i_shot).setX(pos);
- al_shot.get(i_shot).setWidth(10);
- al_shot.get(i_shot).setHeight(20);
- AnimationTimer timer = new AnimationTimer() {
- int j=0;
- @Override
- public void handle(long now) {
- al_shot.get(i_shot).setY(580-j);
- Bounds shipBoundsInParent =
- tt_shipY.getNode().getBoundsInParent();
- Bounds shotBoundsInParent =
- al_shot.get(i_shot).getBoundsInParent();
- if (shipBoundsInParent.intersects(shotBoundsInParent)) {
- MP.play();
- al_shot.get(i_shot).setFill(Color.TRANSPARENT);
- tt_shipY.getNode().setVisible(false);
- tempPoints += 1;
- }
- else {
- MP.stop();
- }
- j++;
- if (j==600) super.stop();
- }
- };
- timer.start();
- return al_shot.get(i_shot);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment