Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package newtonsapple;
- import java.util.Random;
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Label;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.paint.Color;
- import javafx.scene.paint.ImagePattern;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- /**
- *
- * @author Nicholas Camillo
- */
- public class NewtonsApple extends Application {
- private Rectangle rect;
- private Circle apple;
- private Label title;
- private Label droppedLabel;
- private Label eatenLabel;
- private Label divisorLabel;
- private Label gameOverLabel = new Label("Game Over!");
- private Label ScoreLabel = new Label("Your final score is: ");
- private Label scorePercent;
- private Random rand = new Random();
- private int fallSpeed = 10;
- private int applesDropped = 0;
- private int applesEaten = 0;
- private ImageView bg = new ImageView(new Image("file:forest.jpg"));
- private Image newton = new Image("file:newton.gif");
- @Override
- public void start(Stage primaryStage) {
- Group root = new Group();
- Scene scene = new Scene(root, 700, 500);
- makeTitle();
- makeScoreboard();
- apple = new Circle(rand.nextInt(650), -25, 25);
- apple.setFill(Color.RED);
- ImagePattern imagePattern = new ImagePattern(newton);
- rect = new Rectangle(325, 400, 50, 50);
- rect.setFill(imagePattern);
- root.getChildren().add(bg);
- root.getChildren().add(title);
- root.getChildren().add(eatenLabel);
- root.getChildren().add(droppedLabel);
- root.getChildren().add(divisorLabel);
- root.getChildren().add(gameOverLabel);
- gameOverLabel.setVisible(false);
- root.getChildren().add(rect);
- root.getChildren().add(apple);
- primaryStage.setTitle("Newton's Apple");
- primaryStage.setScene(scene);
- primaryStage.show();
- EventHandler<ActionEvent> eventHandler = e->{
- if (applesDropped < 10){
- apple.setCenterY(apple.getCenterY() + fallSpeed);
- if (apple.getCenterX() >= rect.getX()-25 && apple.getCenterX() <= rect.getX()+75 &&
- apple.getCenterY() >= 375){
- apple.setCenterX(rand.nextInt(650));
- apple.setCenterY(0);
- applesEaten++;
- applesDropped++;
- eatenLabel.setText(Integer.toString(applesEaten));
- droppedLabel.setText(Integer.toString(applesDropped));
- }
- if (apple.getCenterY() > 500){
- apple.setCenterY(0);
- apple.setCenterX(rand.nextInt(650));
- applesDropped++;
- droppedLabel.setText(Integer.toString(applesDropped));
- }
- }
- else {
- apple.setVisible(false);
- gameOver();
- }
- };
- rect.setOnKeyPressed(e-> {
- switch (e.getCode()){
- case LEFT:
- if (rect.getX() > 0)
- rect.setX(rect.getX() - 10); break;
- case RIGHT:
- if (rect.getX() < 650)
- rect.setX(rect.getX() + 10); break;
- }
- });
- rect.setOnMouseDragged(e->{
- rect.setX(e.getX());
- });
- rect.requestFocus();
- Timeline animation = new Timeline(new KeyFrame(Duration.millis(19), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- animation.play();
- }
- public void makeTitle(){
- title = new Label("Newton's Apple");
- title.setTranslateX(300);
- title.setTranslateY(20);
- title.setScaleX(2);
- title.setScaleY(2);
- title.setTextFill(Color.WHITE);
- }
- public void makeScoreboard(){
- eatenLabel = new Label("0");
- droppedLabel = new Label("0");
- divisorLabel = new Label("/");
- eatenLabel.setScaleX(3);
- eatenLabel.setScaleY(3);
- eatenLabel.setTranslateX(25);
- eatenLabel.setTranslateY(30);
- eatenLabel.setTextFill(Color.CYAN);
- droppedLabel.setScaleX(3);
- droppedLabel.setScaleY(3);
- droppedLabel.setTranslateX(82);
- droppedLabel.setTranslateY(30);
- droppedLabel.setTextFill(Color.CYAN);
- divisorLabel.setScaleX(3);
- divisorLabel.setScaleY(3);
- divisorLabel.setTranslateX(56);
- divisorLabel.setTranslateY(30);
- divisorLabel.setTextFill(Color.CYAN);
- }
- public void gameOver(){
- gameOverLabel.setTextFill(Color.RED);
- gameOverLabel.setVisible(true);
- gameOverLabel.setScaleX(4);
- gameOverLabel.setScaleY(4);
- gameOverLabel.setTranslateX(325);
- gameOverLabel.setTranslateY(250);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement