forked from shushmita-das/SpaceJammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Start.java
145 lines (117 loc) · 2.97 KB
/
Start.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
//Reference:
//https://github.com/daniel-chai/javafx-game/tree/master/src
//https://github.com/AlmasB/FXTutorials/tree/master/src/com/almasb
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
/*
* This is the start class, and it is extends the the abstract class FightObjuct
*
*/
public class Start extends FightObject implements IScene {
//
private Scene FightScene;
private Group enemies;
//Constructor for the Start class
public Start (SceneControl scene) {
this.scene = scene;
}
//
@Override
public Scene init(int width, int height) {
// TODO Auto-generated method stub
root = new Group();
FightScene = new Scene (root, width, height, Color.BLACK);
AddText(); // add the score
addhero(); // add hero recatangle
AddEnemies(); // add enemy recatangle
addheroBullet(); // add bullet
FightScene.setOnKeyPressed(e -> PressKey(e.getCode()));//eventhandle
return FightScene;
}
//add enemy
private void AddEnemies() {
// TODO Auto-generated method stub
enemies = new Group();
root.getChildren().add(enemies);
}
// Handle keyboard inputs that affect the bullet dirction
private void PressKey(KeyCode key) {
// TODO Auto-generated method stub
switch (key) {
case SPACE:
shoot("UP");
break;
case W:
shoot("UP");
break;
case A:
shoot("RIGHT");
break;
case D:
shoot("LEFT");
break;
case Q:
Restart();
break;
default:
heroObject.handlePlayerKey(key);
}
}
public void point (double sce) {
if (counter % EnemyShowRate() == 0) {
createEnemy();
}
EnemyMove(); // move enemy
MoveBullet(); // move bullet
ShootEnemy(); // check the
AddScoreText(); // update the score
EnemyHitHero(); //lose when enemy hit hero
MissEnemy(); // lose when the enemy hit the botton of the scene
counter++;
}
private void AddScoreText() {
UppdateScore("Score: " , super.Score);
}
private void EnemyMove() {
for (Node enemyNode : enemies.getChildren()){
Rectangle enemy = (Rectangle) enemyNode;
enemy.setY(enemy.getY() + EnemyspeedRate());
}
}
private void ShootEnemy() {
removeEnemy(enemies, true);
}
private void EnemyHitHero() {
for (Node enemyNode : enemies.getChildren()) {
Rectangle enemy = (Rectangle) enemyNode;
EnemyHitHero(enemy);
}
}
private void MissEnemy() {
for (Node enemyNode : enemies.getChildren()) {
Rectangle enemy = (Rectangle) enemyNode;
if (enemy.getY() + Enemy.HEIGHT > Main.SIZE) {
lost();
}
}
}
private void createEnemy() {
// TODO Auto-generated method stub
Enemy enemyObject = new Enemy();
Rectangle enemy = enemyObject.GetEnemy();
enemies.getChildren().add(enemy);
}
private int EnemyShowRate() {
// TODO Auto-generated method stub
int showRate = 150;
return showRate;
}
private double EnemyspeedRate() {
double speedRate = 2;
return speedRate;
}
}