Advertisement
ETeggs

Asteroids

Nov 27th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. /**
  2. * Created by 40465 on 11/27/14.
  3. */
  4.  
  5. import javafx.animation.Animation;
  6. import javafx.animation.KeyFrame;
  7. import javafx.animation.Timeline;
  8. import javafx.application.Application;
  9. import javafx.scene.Scene;
  10. import javafx.scene.input.KeyCode;
  11. import javafx.scene.layout.AnchorPane;
  12. import javafx.scene.layout.BorderPane;
  13. import javafx.scene.layout.StackPane;
  14. import javafx.scene.paint.Color;
  15. import javafx.scene.shape.Circle;
  16. import javafx.scene.shape.Rectangle;
  17. import javafx.stage.Stage;
  18. import javafx.util.Duration;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. public class Asteroids extends Application {
  23. public static final int SPACE_WIDTH = 750, SPACE_HEIGHT = 550, STAR_TOTAL = 1000, INITIAL_ASTEROIDS = 7, INITIAL_ASTEROIDS_SIZE = 25;
  24.  
  25. private BorderPane root;
  26. private Stage stage;
  27. private Scene scene;
  28.  
  29. private AnchorPane spacePane;
  30. private StackPane stackPane;
  31. private SpaceShip ship;
  32. private Timeline timeline;
  33. private ArrayList<Asteroid> asteroids;
  34. private ArrayList<Bullet> bullets;
  35.  
  36. @Override
  37. public void start(Stage primaryStage) {
  38. stage = primaryStage;
  39. root = new BorderPane();
  40. scene = new Scene(root, 800, 600); //width and height of application
  41. stage.setScene(scene);
  42. stage.setTitle("Unit 4 Application"); //text for the title bar of the window
  43.  
  44. stage.setResizable(false);
  45. scene.getStylesheets().add("asteroids.css");
  46.  
  47. //Initializations
  48. spacePane = new AnchorPane();
  49. stackPane = new StackPane(spacePane);
  50. ship = new SpaceShip(SPACE_WIDTH / 2, SPACE_HEIGHT / 2);
  51.  
  52.  
  53. asteroids = new ArrayList<Asteroid>();
  54. bullets = new ArrayList<Bullet>();
  55.  
  56.  
  57. for (int i = 0; i < INITIAL_ASTEROIDS; i++) {
  58. asteroids.add(new Asteroid((int) (Math.random() * SPACE_WIDTH), (int) (Math.random() * SPACE_HEIGHT), Math.random() * INITIAL_ASTEROIDS_SIZE + INITIAL_ASTEROIDS_SIZE));
  59. }
  60.  
  61. timeline = new Timeline(new KeyFrame(new Duration(30), event -> {
  62. ship.move();
  63.  
  64.  
  65. for (int i = 0; i < asteroids.size(); i++) {
  66. asteroids.get(i).move();
  67.  
  68.  
  69. if (asteroids.get(i).getLayoutX() > SPACE_WIDTH) {
  70. asteroids.get(i).setLayoutX(0);
  71. } else if (asteroids.get(i).getLayoutX() < 0) {
  72. asteroids.get(i).setLayoutX(SPACE_WIDTH);
  73. }
  74.  
  75. if (asteroids.get(i).getLayoutY() > SPACE_HEIGHT) {
  76. asteroids.get(i).setLayoutY(0);
  77. } else if (asteroids.get(i).getLayoutY() < 0) {
  78. asteroids.get(i).setLayoutY(SPACE_HEIGHT);
  79. }
  80. }
  81.  
  82. if (ship.getLayoutX() > SPACE_WIDTH) {
  83. ship.setLayoutX(0);
  84. } else if (ship.getLayoutX() < 0) {
  85. ship.setLayoutX(SPACE_WIDTH);
  86. }
  87.  
  88. if (ship.getLayoutY() > SPACE_HEIGHT) {
  89. ship.setLayoutY(0);
  90. } else if (ship.getLayoutY() < 0) {
  91. ship.setLayoutY(SPACE_HEIGHT);
  92. }
  93.  
  94.  
  95. for (int i = 0; i < bullets.size(); i++) {
  96. bullets.get(i).move();
  97. }
  98.  
  99.  
  100. }));
  101. timeline.setCycleCount(Animation.INDEFINITE); //or setCycleCount(-1);
  102.  
  103.  
  104. spacePane.setClip(new Rectangle(0, 0, SPACE_WIDTH, SPACE_HEIGHT));
  105.  
  106.  
  107. scene.setOnKeyPressed(event -> {
  108. if(event.getCode() == KeyCode.UP) {
  109. ship.setSpeed(10);
  110. }
  111. if(event.getCode() == KeyCode.DOWN) {
  112. ship.setSpeed(-5);
  113. }
  114. if(event.getCode() == KeyCode.LEFT) {
  115. ship.setdRotate(-5);
  116. }
  117. if(event.getCode() == KeyCode.RIGHT) {
  118. ship.setdRotate(5);
  119. }
  120. if(event.getCode() == KeyCode.SPACE) {
  121. System.out.println("Fire");
  122. bullets.add(new Bullet(ship.gun, ship.getRotate()));
  123. spacePane.getChildren().add(bullets.get(bullets.size() - 1));
  124. }
  125.  
  126.  
  127. });
  128.  
  129. //Layout
  130. root.setCenter(stackPane);
  131.  
  132.  
  133. addStars();
  134. for (int i = 0; i < asteroids.size(); i++) {
  135. spacePane.getChildren().add(asteroids.get(i));
  136. }
  137. spacePane.getChildren().add(ship);
  138.  
  139.  
  140. //spacePane.getChildren().add(new Asteroid(300, 100, 50));
  141.  
  142.  
  143. timeline.play();
  144.  
  145.  
  146. stage.show();
  147. }
  148.  
  149. private void addStars() {
  150. for (int i = 0; i < STAR_TOTAL; i++) {
  151. Circle star = new Circle(Math.random() * SPACE_WIDTH, Math.random() * SPACE_HEIGHT, Math.random() * 3, Color.rgb(255, 255, 255, Math.random()));
  152. spacePane.getChildren().add(star);
  153.  
  154. }
  155. }
  156.  
  157. public static void main(String[] args) {
  158. launch(args);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement