-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsteroidsApp.java
272 lines (231 loc) · 9.62 KB
/
AsteroidsApp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package asteroidsApp;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.*;
public class AsteroidsApp extends Application {
private Pane root;
private List<GameObject> bullets = new ArrayList<>();
private List<GameObject> enemies = new ArrayList<>();
private List<GameObject> speedBoosts = new ArrayList<>();
private Set<String> pressed = new HashSet<String>();
private GameObject player;
private AnimationTimer timer;
private Double bulletTime = 0.0;
private Double bulletFinal = 0.0;
private Double lambdaBulletTime = (0.0) * 1000000000; // Change this to alter hwo fast you can shoot bullets. The number before * is seconds
private double playerX;
private double playerY;
private int livesLeft = 5;
private int score = 0;
private Text livesText;
private Text scoreText;
public final int WIDTH = 1280;
public final int HEIGHT = 720;
private Parent createContent() {
root = new Pane();
root.setPrefSize(WIDTH, HEIGHT);
player = new Player();
player.setVelocity(new Point2D(1, 0));
addGameObject(player, 300, 300);
Image img = new Image("/Images/Background/ovalOffice.jpg");
BackgroundImage bgImg = new BackgroundImage(img,
BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT,
BackgroundPosition.DEFAULT,
new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false));
root.setBackground(new Background(bgImg));
timer = new AnimationTimer() {
@Override
public void handle(long now) {
onUpdate();
}
};
timer.start();
livesText = new Text(WIDTH - 135, 20, "Lives left: " + livesLeft);
livesText.setFill(Color.BLACK);
livesText.setStyle("-fx-font-size: 18px;");
scoreText = new Text(WIDTH - 135, 40, "Score: " + score);
scoreText.setFill(Color.BLACK);
scoreText.setStyle("-fx-font-size: 18px;");
root.getChildren().addAll(livesText, scoreText);
return root;
}
private void addBullet(GameObject bullet, double x, double y) {
bullets.add(bullet);
addGameObject(bullet, x, y);
}
private void addEnemy(GameObject enemy, double x, double y) {
enemies.add(enemy);
addGameObject(enemy, x, y);
}
private void addSpeedBoost(GameObject speedBoost, double x, double y){
speedBoosts.add(speedBoost);
addGameObject(speedBoost, x, y);
}
private void addGameObject(GameObject object, double x, double y) {
object.getView().setTranslateX(x);
object.getView().setTranslateY(y);
root.getChildren().add(object.getView());
}
private void setLambdaBulletTime(Double seconds){
lambdaBulletTime = seconds * 1000000000;
}
private void shootBullet(){
bulletTime = (double) System.nanoTime();
if(bulletTime > bulletFinal) {
bulletFinal = bulletTime + lambdaBulletTime;
Bullet bullet = new Bullet();
bullet.setVelocity(player.getVelocity().normalize().multiply(5));
addBullet(bullet, player.getView().getTranslateX(), player.getView().getTranslateY());
}
}
private void gameOver(){
Text gameOverText = new Text((WIDTH / 2) - 170, HEIGHT / 2, "Impeached!");
Rectangle gameOverBox = new Rectangle((WIDTH / 2) - 220, (HEIGHT / 2) - 100, 450, 250);
int highScore = Integer.parseInt(GameObject.readHighScore());
if(score > highScore){
GameObject.writeHighScore(score);
}
ImageView gameOverImage = new ImageView(new Image("/Images/Background/trumpHead.jpg", 200, 100, false, true));
gameOverText.setFill(Color.WHITE);
gameOverText.setStyle("-fx-font-size: 60");
gameOverBox.setFill(Color.BLACK);
root.getChildren().addAll(gameOverBox, gameOverText);
timer.stop();
}
private void onUpdate() {
//Game over
if(livesLeft < 0){
gameOver();
}
//Managing buttons
Iterator<String> it = pressed.iterator();
while(it.hasNext()){
String current = it.next();
if("A".equals(current) || "LEFT".equals(current)){
player.rotateLeft();
}
if("D".equals(current) || "RIGHT".equals(current)){
player.rotateRight();
}
if("SPACE".equals(current)){
shootBullet();
}
if("W".equals(current)){
//player.setVelocity(new Point2D(player.getView().getTranslateX() + 5, player.getView().getTranslateY() + 5));
player.setVelocity(new Point2D(player.velocity.getX() - 3, player.velocity.getY() + 3));
}
}
//Managing killing enemies and bullets
for (GameObject bullet : bullets) {
for (GameObject enemy : enemies) {
if (bullet.isColliding(enemy)) {
bullet.setAlive(false);
enemy.setAlive(false);
root.getChildren().removeAll(bullet.getView(), enemy.getView());
score += 10;
scoreText.setText("Score: " + score);
}
}
}
for(GameObject enemy: enemies) {
if (player.isColliding(enemy)) {
enemy.setAlive(false);
livesLeft--;
livesText.setText("Lifes left: " + livesLeft);
root.getChildren().remove(enemy.getView());
}
}
//Manage speed boosts
for(GameObject speedBoost: speedBoosts){
if(player.isColliding(speedBoost)){
speedBoost.setAlive(false);
root.getChildren().remove(speedBoost.getView());
//player.setVelocity(new Point2D(player.getVelocity().getX() + 1, player.getVelocity().getY() + 1));
}
}
bullets.removeIf(GameObject::isDead);
enemies.removeIf(GameObject::isDead);
bullets.forEach(GameObject::update);
enemies.forEach(GameObject::update);
//Manage leaving the screen
playerX = player.getView().getTranslateX();
playerY = player.getView().getTranslateY();
if(playerY < -20){
player.getView().setTranslateY(HEIGHT);
} else if(playerY > HEIGHT){
player.getView().setTranslateY(-20);
}
if(playerX < -20){
player.getView().setTranslateX(WIDTH);
} else if(playerX > WIDTH){
player.getView().setTranslateX(-20);
}
player.update();
if (Math.random() < 0.02) {//Make sure enemy does not spawn directly on player
double randomX = Math.random() * root.getPrefWidth();
double randomY = Math.random() * root.getPrefHeight(); //TODO make sure bullets don't spawn on lives/score text
while(!(Math.abs(randomX - player.getView().getTranslateX()) > 50) && !(Math.abs(randomY - player.getView().getTranslateY()) > 50)){
randomX = Math.random() * root.getPrefWidth();
randomY = Math.random() * root.getPrefHeight();
}
addEnemy(new Enemy(), randomX, randomY);
}
if (Math.random() < 0.01) {
double randomX = Math.random() * root.getPrefWidth();
double randomY = Math.random() * root.getPrefHeight(); //TODO make sure bullets don't spawn on lives/score text
while(!(Math.abs(randomX - player.getView().getTranslateX()) > 50) && !(Math.abs(randomY - player.getView().getTranslateY()) > 50)){
randomX = Math.random() * root.getPrefWidth();
randomY = Math.random() * root.getPrefHeight();
}
addSpeedBoost(new SpeedBoost(), randomX, randomY);
}
}
private static class Player extends GameObject {
Player() {
super(new ImageView(new Image("/Images/Background/trumpHead.jpg", 100, 100, false, true)));
}
}
private static class Enemy extends GameObject {
Enemy() {
super(new Circle(15, 15, 15, Color.RED));
}
}
private static class Bullet extends GameObject {
Bullet() {
super(new Circle(5, 5, 5, Color.BROWN));
}
}
private static class SpeedBoost extends GameObject {
SpeedBoost() {super(new Circle(10, 10, 10, Color.GREEN)); }
}
public Scene setEventListeners(Scene scene){
scene.setOnKeyPressed(e -> {
pressed.add(e.getCode().toString());
});
scene.setOnKeyReleased(e -> {
pressed.remove(e.getCode().toString());
});
return scene;
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.setScene(setEventListeners(stage.getScene()));
stage.show();
GameObject.readHighScore();
}
public static void main(String[] args) {
launch(args);
}
}