Advertisement
Nick-O-Rama

NewtonsApple

Oct 6th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package newtonsapple;
  7.  
  8. import java.util.Random;
  9. import javafx.animation.KeyFrame;
  10. import javafx.animation.Timeline;
  11. import javafx.application.Application;
  12. import javafx.event.ActionEvent;
  13. import javafx.event.EventHandler;
  14. import javafx.scene.Group;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Label;
  17. import javafx.scene.image.Image;
  18. import javafx.scene.image.ImageView;
  19. import javafx.scene.paint.Color;
  20. import javafx.scene.paint.ImagePattern;
  21. import javafx.scene.shape.Circle;
  22. import javafx.scene.shape.Rectangle;
  23. import javafx.stage.Stage;
  24. import javafx.util.Duration;
  25.  
  26. /**
  27.  *
  28.  * @author Nicholas Camillo
  29.  */
  30. public class NewtonsApple extends Application {
  31.     private Rectangle rect;
  32.     private Circle apple;
  33.     private Label title;
  34.     private Label droppedLabel;
  35.     private Label eatenLabel;
  36.     private Label divisorLabel;
  37.     private Label gameOverLabel = new Label("Game Over!");
  38.     private Label ScoreLabel = new Label("Your final score is: ");
  39.     private Label scorePercent;
  40.     private Random rand = new Random();
  41.     private int fallSpeed = 10;
  42.     private int applesDropped = 0;
  43.     private int applesEaten = 0;
  44.     private ImageView bg = new ImageView(new Image("file:forest.jpg"));
  45.     private Image newton = new Image("file:newton.gif");
  46.     @Override
  47.     public void start(Stage primaryStage) {
  48.        
  49.         Group root = new Group();
  50.         Scene scene = new Scene(root, 700, 500);
  51.         makeTitle();
  52.         makeScoreboard();
  53.         apple = new Circle(rand.nextInt(650), -25, 25);
  54.         apple.setFill(Color.RED);
  55.         ImagePattern imagePattern = new ImagePattern(newton);
  56.        
  57.         rect = new Rectangle(325, 400, 50, 50);
  58.         rect.setFill(imagePattern);
  59.  
  60.         root.getChildren().add(bg);
  61.         root.getChildren().add(title);
  62.         root.getChildren().add(eatenLabel);
  63.         root.getChildren().add(droppedLabel);
  64.         root.getChildren().add(divisorLabel);
  65.         root.getChildren().add(gameOverLabel);        
  66.         gameOverLabel.setVisible(false);
  67.         root.getChildren().add(rect);
  68.         root.getChildren().add(apple);
  69.         primaryStage.setTitle("Newton's Apple");
  70.         primaryStage.setScene(scene);
  71.         primaryStage.show();
  72.        
  73.         EventHandler<ActionEvent> eventHandler = e->{
  74.             if (applesDropped < 10){
  75.                 apple.setCenterY(apple.getCenterY() + fallSpeed);
  76.                 if (apple.getCenterX() >= rect.getX()-25 && apple.getCenterX() <= rect.getX()+75 &&
  77.                         apple.getCenterY() >= 375){
  78.                     apple.setCenterX(rand.nextInt(650));
  79.                     apple.setCenterY(0);
  80.                     applesEaten++;
  81.                     applesDropped++;
  82.                     eatenLabel.setText(Integer.toString(applesEaten));
  83.                     droppedLabel.setText(Integer.toString(applesDropped));
  84.                 }                
  85.                 if (apple.getCenterY() > 500){
  86.                     apple.setCenterY(0);
  87.                     apple.setCenterX(rand.nextInt(650));
  88.                     applesDropped++;
  89.                     droppedLabel.setText(Integer.toString(applesDropped));
  90.                 }
  91.  
  92.             }
  93.             else {
  94.                 apple.setVisible(false);
  95.                 gameOver();
  96.             }
  97.         };
  98.        
  99.         rect.setOnKeyPressed(e-> {
  100.             switch (e.getCode()){
  101.                 case LEFT:
  102.                     if (rect.getX() > 0)
  103.                         rect.setX(rect.getX() - 10); break;
  104.                 case RIGHT:
  105.                     if (rect.getX() < 650)
  106.                         rect.setX(rect.getX() + 10); break;
  107.             }
  108.         });
  109.        
  110.         rect.setOnMouseDragged(e->{
  111.            rect.setX(e.getX());
  112.         });
  113.         rect.requestFocus();
  114.         Timeline animation = new Timeline(new KeyFrame(Duration.millis(19), eventHandler));
  115.         animation.setCycleCount(Timeline.INDEFINITE);
  116.         animation.play();
  117.     }
  118.    
  119.     public void makeTitle(){
  120.         title = new Label("Newton's Apple");
  121.         title.setTranslateX(300);
  122.         title.setTranslateY(20);
  123.         title.setScaleX(2);
  124.         title.setScaleY(2);
  125.         title.setTextFill(Color.WHITE);
  126.     }
  127.    
  128.     public void makeScoreboard(){
  129.         eatenLabel = new Label("0");
  130.         droppedLabel = new Label("0");
  131.         divisorLabel = new Label("/");
  132.         eatenLabel.setScaleX(3);
  133.         eatenLabel.setScaleY(3);
  134.         eatenLabel.setTranslateX(25);
  135.         eatenLabel.setTranslateY(30);
  136.         eatenLabel.setTextFill(Color.CYAN);
  137.         droppedLabel.setScaleX(3);
  138.         droppedLabel.setScaleY(3);
  139.         droppedLabel.setTranslateX(82);
  140.         droppedLabel.setTranslateY(30);
  141.         droppedLabel.setTextFill(Color.CYAN);
  142.         divisorLabel.setScaleX(3);
  143.         divisorLabel.setScaleY(3);
  144.         divisorLabel.setTranslateX(56);
  145.         divisorLabel.setTranslateY(30);
  146.         divisorLabel.setTextFill(Color.CYAN);
  147.        
  148.     }
  149.  
  150.     public void gameOver(){
  151.         gameOverLabel.setTextFill(Color.RED);
  152.         gameOverLabel.setVisible(true);
  153.         gameOverLabel.setScaleX(4);
  154.         gameOverLabel.setScaleY(4);
  155.         gameOverLabel.setTranslateX(325);
  156.         gameOverLabel.setTranslateY(250);
  157.     }
  158.     /**
  159.      * @param args the command line arguments
  160.      */
  161.     public static void main(String[] args) {
  162.         launch(args);
  163.     }
  164.    
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement