-
Notifications
You must be signed in to change notification settings - Fork 0
/
TankWar.java
138 lines (111 loc) · 4.34 KB
/
TankWar.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
import javafx.scene.input.*;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.animation.*;
import javafx.util.*;
import java.util.EventListener;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.scene.input.KeyCode;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import java.util.Random;
import java.util.ArrayList;
public class TankWar extends Application {
// This is the class to execute the game
Timeline timeline;
int count = 0;
public int count() {
return count ++;
}
public void start(Stage stage){
final Group root = new Group();
final Scene scene = new Scene(root, 520, 520);
scene.setFill(Color.BLACK);
stage.setTitle("Battle City!");
stage.setScene(scene);
stage.show();
BasicBlock[][] maps = Data.getData().map;
for (int row = 0; row < 13; row++) { // ADD TO ROOT.
for (int col = 0; col < 13; col++) {
root.getChildren().add(maps[row][col].iv);
}
}
// DEFINING THE PLAYERS
Player player = Data.getData().player;
root.getChildren().add(player.iv);
root.getChildren().add(player.missile.iv);
stage.addEventHandler(KeyEvent.KEY_PRESSED, player);
root.getChildren().add(Data.getData().powerup.iv);
// ------------- END.
// DEFINING ENEMIES ---------------
ArrayList<Enemy> enem = Data.getData().enem;
for (int tv = 0; tv < enem.size(); tv++){
root.getChildren().add(enem.get(tv).iv);
root.getChildren().add(enem.get(tv).missile.iv);
}
final Duration oneFrameAmt = Duration.millis(1500/60);
final KeyFrame oneFrame = new KeyFrame(oneFrameAmt, // MAIN HANDLE.
new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
if (player.helmet) {
if (player.h_count >= 700) { // CHANGE HELMET.
player.iv.setImage(new Image("player_left.png"));
player.h_count = 0;
Data.getData().powerup.on = false;
player.helmet = false;
}
player.h_count++;
}
if (count()%300 == 0 && Data.getData().powerup.on == false) {
System.out.println("NOW EXECUTE");
Location loce = new Location(80,240);
Enemy xy = new Enemy(loce);
ArrayList<Enemy> enem1 = Data.getData().enem;
enem1.add(xy);
root.getChildren().add(enem1.get(enem1.size()-1).iv);
root.getChildren().add(enem1.get(enem1.size()-1).missile.iv);
}
player.doAction();
if (Data.getData().timefreeze(0) != 0) {
if (Data.getData().timefreeze(0) >= 700) { // CHANGE TIMEFREEZE VALUE HERE.
Data.getData().powerup.on = false;
Data.getData().timefreeze= 0;
System.out.println("THIS SHOULD BE ZERO" + Data.getData().timefreeze(0));
} else {
System.out.println(Data.getData().timefreeze(1));
}
} else {
for (int tv = 0; tv < enem.size(); tv++) {
enem.get(tv).doAction();
}
}
if (Data.getData().player.killed || Data.getData().eagle.killed) { // STOPP.
root.getChildren().add(new ImageView(new Image("gameover.png")));
System.out.println("GAMEOVER");
timeline.stop();
}
if (count() > 2000) { // STOPP yo!!
root.getChildren().add(new ImageView(new Image("winner.png")));
System.out.println("GAMEOVER");
timeline.stop();
}
if (new Random().nextInt() > 0 && count()%350 == 0 && Data.getData().powerup.avail == false) { // half probability
System.out.println("executing probability");
Data.getData().makePowerup();
}
}
});
timeline = new Timeline(oneFrame);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
public static void main(String[] args) {
Application.launch(args); //to launch the startMethod
}
}