Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by 40465 on 11/27/14.
- */
- import javafx.animation.Animation;
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.input.KeyCode;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- import java.util.ArrayList;
- public class Asteroids extends Application {
- public static final int SPACE_WIDTH = 750, SPACE_HEIGHT = 550, STAR_TOTAL = 1000, INITIAL_ASTEROIDS = 7, INITIAL_ASTEROIDS_SIZE = 25;
- private BorderPane root;
- private Stage stage;
- private Scene scene;
- private AnchorPane spacePane;
- private StackPane stackPane;
- private SpaceShip ship;
- private Timeline timeline;
- private ArrayList<Asteroid> asteroids;
- private ArrayList<Bullet> bullets;
- @Override
- public void start(Stage primaryStage) {
- stage = primaryStage;
- root = new BorderPane();
- scene = new Scene(root, 800, 600); //width and height of application
- stage.setScene(scene);
- stage.setTitle("Unit 4 Application"); //text for the title bar of the window
- stage.setResizable(false);
- scene.getStylesheets().add("asteroids.css");
- //Initializations
- spacePane = new AnchorPane();
- stackPane = new StackPane(spacePane);
- ship = new SpaceShip(SPACE_WIDTH / 2, SPACE_HEIGHT / 2);
- asteroids = new ArrayList<Asteroid>();
- bullets = new ArrayList<Bullet>();
- for (int i = 0; i < INITIAL_ASTEROIDS; i++) {
- asteroids.add(new Asteroid((int) (Math.random() * SPACE_WIDTH), (int) (Math.random() * SPACE_HEIGHT), Math.random() * INITIAL_ASTEROIDS_SIZE + INITIAL_ASTEROIDS_SIZE));
- }
- timeline = new Timeline(new KeyFrame(new Duration(30), event -> {
- ship.move();
- for (int i = 0; i < asteroids.size(); i++) {
- asteroids.get(i).move();
- if (asteroids.get(i).getLayoutX() > SPACE_WIDTH) {
- asteroids.get(i).setLayoutX(0);
- } else if (asteroids.get(i).getLayoutX() < 0) {
- asteroids.get(i).setLayoutX(SPACE_WIDTH);
- }
- if (asteroids.get(i).getLayoutY() > SPACE_HEIGHT) {
- asteroids.get(i).setLayoutY(0);
- } else if (asteroids.get(i).getLayoutY() < 0) {
- asteroids.get(i).setLayoutY(SPACE_HEIGHT);
- }
- }
- if (ship.getLayoutX() > SPACE_WIDTH) {
- ship.setLayoutX(0);
- } else if (ship.getLayoutX() < 0) {
- ship.setLayoutX(SPACE_WIDTH);
- }
- if (ship.getLayoutY() > SPACE_HEIGHT) {
- ship.setLayoutY(0);
- } else if (ship.getLayoutY() < 0) {
- ship.setLayoutY(SPACE_HEIGHT);
- }
- for (int i = 0; i < bullets.size(); i++) {
- bullets.get(i).move();
- }
- }));
- timeline.setCycleCount(Animation.INDEFINITE); //or setCycleCount(-1);
- spacePane.setClip(new Rectangle(0, 0, SPACE_WIDTH, SPACE_HEIGHT));
- scene.setOnKeyPressed(event -> {
- if(event.getCode() == KeyCode.UP) {
- ship.setSpeed(10);
- }
- if(event.getCode() == KeyCode.DOWN) {
- ship.setSpeed(-5);
- }
- if(event.getCode() == KeyCode.LEFT) {
- ship.setdRotate(-5);
- }
- if(event.getCode() == KeyCode.RIGHT) {
- ship.setdRotate(5);
- }
- if(event.getCode() == KeyCode.SPACE) {
- System.out.println("Fire");
- bullets.add(new Bullet(ship.gun, ship.getRotate()));
- spacePane.getChildren().add(bullets.get(bullets.size() - 1));
- }
- });
- //Layout
- root.setCenter(stackPane);
- addStars();
- for (int i = 0; i < asteroids.size(); i++) {
- spacePane.getChildren().add(asteroids.get(i));
- }
- spacePane.getChildren().add(ship);
- //spacePane.getChildren().add(new Asteroid(300, 100, 50));
- timeline.play();
- stage.show();
- }
- private void addStars() {
- for (int i = 0; i < STAR_TOTAL; i++) {
- Circle star = new Circle(Math.random() * SPACE_WIDTH, Math.random() * SPACE_HEIGHT, Math.random() * 3, Color.rgb(255, 255, 255, Math.random()));
- spacePane.getChildren().add(star);
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement