-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGamesetup
181 lines (152 loc) · 5.09 KB
/
Gamesetup
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
import javafx.application.Application;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Gamesetup extends Application {
//variables
private static final Random RAND = new Random();
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int PLAYER_SIZE = 60;
static final Image PLAYER_IMG = new Image("file:///C:/Users/alias/Downloads/Compressed/player.png");
static final Image EXPLOSION_IMG = new Image("file:///C:/Users/alias/Downloads/Compressed/explosion.png");
static final int EXPLOSION_W = 128;
static final int EXPLOSION_ROWS = 3;
static final int EXPLOSION_COL = 3;
static final int EXPLOSION_H = 128;
static final int EXPLOSION_STEPS = 15;
static final Image BOMBS_IMG[] = {
new Image("file:///C:/Users/alias/Downloads/Compressed/1.png"),
new Image("file:///C:/Users/alias/Downloads/Compressed/1.png"),
new Image("file:///C:/Users/alias/Downloads/Compressed/1.png"),
new Image("file:///C:/Users/alias/Downloads/Compressed/1.png"),
new Image("file:/images/5.png"),
new Image("file:/images/6.png"),
new Image("file:/images/7.png"),
new Image("file:/images/8.png"),
new Image("file:/images/9.png"),
new Image("file:/images/10.png"),
};
final int MAX_BOMBS = 10, MAX_SHOTS = MAX_BOMBS * 2;
boolean gameOver = false;
private GraphicsContext gc;
Rocket player;
List<Shot> shots;
// List<Universe> univ;
List<Bombs> Bombs;
private double mouseX;
private int score;
//start
public void start(Stage stage) throws Exception {
Canvas canvas = new Canvas(WIDTH, HEIGHT);
gc = canvas.getGraphicsContext2D();
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(100), e -> run(gc)));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
canvas.setCursor(Cursor.MOVE);
canvas.setOnMouseMoved(e -> mouseX = e.getX());
canvas.setOnMouseClicked(e -> {
if(shots.size() < MAX_SHOTS) shots.add(player.shoot());
if(gameOver) {
gameOver = false;
setup();
}
});
setup();
stage.setScene(new Scene(new StackPane(canvas)));
stage.setTitle("Space Invaders");
stage.show();
}
//setup the game
private void setup() {
//univ = new ArrayList<>();
shots = new ArrayList<>();
Bombs = new ArrayList<>();
player = new Rocket(WIDTH / 2, HEIGHT - PLAYER_SIZE, PLAYER_SIZE, PLAYER_IMG);
score = 0;
IntStream.range(0, MAX_BOMBS).mapToObj(i -> this.newBomb()).forEach(Bombs::add);
}
//run Graphics
private void run(GraphicsContext gc) {
gc.setFill(Color.grayRgb(20));
gc.fillRect(0, 0, WIDTH, HEIGHT);
gc.setTextAlign(TextAlignment.CENTER);
gc.setFont(Font.font(20));
gc.setFill(Color.WHITE);
gc.fillText("Score: " + score, 60, 20);
if(gameOver) {
gc.setFont(Font.font(35));
gc.setFill(Color.YELLOW);
gc.fillText("Game Over \n Your Score is: " + score + " \n Click to play again", WIDTH / 2, HEIGHT /2.5);
}
//univ.forEach(Universe::draw);
player.update();
player.draw();
player.posX = (int) mouseX;
Bombs.stream().peek(Rocket::update).peek(Rocket::draw).forEach(e->{
if(player.colide(e) && !player.exploding) {
player.explode();
}
});
// Bombs.stream().peek(Rocket::update).peek(Rocket::draw).forEach(e -> {
// if(player.colide(e) && !player.exploding) {
// player.explode();
// }
// });
//
for (int i = shots.size() - 1; i >=0 ; i--) {
Shot shot = shots.get(i);
if(shot.posY < 0 || shot.toRemove) {
shots.remove(i);
continue;
}
shot.update();
shot.draw();
for (Bombs bomb : Bombs) {
if(shot.colide(bomb) && !bomb.exploding) {
score++;
bomb.explode();
shot.toRemove = true;
}
}
}
for (int i = Bombs.size() - 1; i >= 0; i--){
if(Bombs.get(i).destroyed) {
Bombs.set(i, newBomb());
}
}
gameOver = player.destroyed;
// if(RAND.nextInt(10) > 2) {
// univ.add(new Universe());
// }
// for (int i = 0; i < univ.size(); i++) {
// if(univ.get(i).posY > HEIGHT)
// univ.remove(i);
// }
}
Bombs newBomb() {
return new Bombs(50 + RAND.nextInt(WIDTH-100),0,PLAYER_SIZE,BOMBS_IMG[RAND.nextInt(BOMBS_IMG.length)]);
}
int distance(int x1,int y1,int x2,int y2){
return(int)Math.sqrt(Math.pow((x1 - x2),2)+ Math.pow((y1-y2),2));
}
public static void main(String[] args) {
launch();
}
}