-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
409 lines (332 loc) · 8.03 KB
/
main.java
File metadata and controls
409 lines (332 loc) · 8.03 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package VKBot.Bot;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.swing.*;
import VKBot.Bot.Object;
public class App extends JPanel implements Runnable
{
JFrame frame;
private static final int DELAY = 17;
private final int RECT_HEIGHT = 10;
private final int RECT_WIDTH = 50;
private final int ALIEN_H = 20;
private final int ALIEN_W = 40;
public int RECT_X = 250;
public int RECT_Y = 500;
public Vector<Shot> shots;
public Player player;
public Vector<Alien> aliens;
public boolean ingame = true;
public boolean isDefeated = false;
public boolean isWin = false;
public Thread animator;
BufferedImage image;
BufferedImage bgImage;
public App()
{
gameInit();
initApp();
}
public void initApp()
{
frame = new JFrame("Space Invaders");
try
{
bgImage = ImageIO.read(new File("C:\\Users\\maxim\\Desktop\\bgImage.jpg"));
}catch(IOException e)
{
System.out.println("Can't download background image");
}
frame.setSize(600, 600);
frame.setTitle("Points");
frame.addKeyListener(new MyKeyAdapter());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(this);
}
// public void startScreen()
// {
// frame.setLayout(new FlowLayout());
// levels = new JButton[2];
//
// levels[0] = new JButton("First");
// levels[0].addActionListener(this);
//
// levels[1] = new JButton("Second");
// levels[1].addActionListener(this);
//
// }
//add aliens here
public void gameInit()
{
shots = new Vector();
aliens = new Vector();
player = new Player(RECT_X, RECT_Y, RECT_WIDTH, RECT_HEIGHT);
fillAliens();
animator = new Thread(this);
animator.start();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(ingame)
{
g.drawImage(bgImage, 0, 0, 600, 600, this);
drawPlayer(g);
drawAliens(g);
drawShot(g);
//g.drawImage(player.getImage(), (int)player.getX(), (int)player.getY(), player.getWidth(), player.getHeight(), this);
}
else if(isDefeated)
{
defeat();
//draw loser screen or image
try
{
image = ImageIO.read(new File("C:\\Users\\maxim\\Desktop\\loser.jpg"));
g.drawImage(image, 0, 0, 600, 600, this);
}catch(IOException ex)
{
}
}
else if(isWin)
{
victory();
try
{
image = ImageIO.read(new File("C:\\Users\\maxim\\Desktop\\winner.jpg"));
g.drawImage(image, 0, 0, 600, 600, this);
}catch(IOException ex)
{
}
}
}
public void run()
{
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while(ingame)
{
repaint();
//animation();
for(int i = 0; i < shots.size(); i++)
shots.elementAt(i).addToY(-10);
for(int j = 0; j < aliens.size(); j++)
{
if(aliens.elementAt(j).getY() >= player.getY() - 2*RECT_HEIGHT)
{
isDefeated = true;
ingame = false;
break;
}
aliens.elementAt(j).addToY(aliens.elementAt(j).getSpeed()); //+= ALIEN_SPEED;
collide();
}
if(aliens.size() == 0)
{
ingame = false;
isWin = true;
}
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if(sleep < 0)
{
sleep = 4;
}
try
{
Thread.sleep(sleep);
}catch(InterruptedException e)
{
System.out.println("Interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
public void fillAliens()
{
trapezeAliens();
}
//Forms of waves of aliens
public void squareAliens()
{
int x = 190, y = 50;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
x += 50;
}
x = 190;
y += 30;
}
}
public void trapezeAliens()
{
int x = 100, y = 50;
for(int i = 0; i < 7; i++)
{
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
x += 50;
}
//left side
x = 130;
y += 30;
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
x = 160;
y += 30;
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
//right side
x = 370;
y = 80;
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
x = 340;
y += 30;
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
x = 200;
y += 30;
for(int i = 0; i < 3; i++)
{
aliens.add(new Alien(x, y, ALIEN_W, ALIEN_H));
x += 50;
}
}
public void drawShot(Graphics g)
{
g.setColor(Color.ORANGE);
if(ingame)
{
for(int i = 0; i < shots.size(); i++)
{
g.fillRect((int)shots.elementAt(i).getX()+ 15, (int)shots.elementAt(i).getY(), 6, 10);
}
}
}
public void drawAliens(Graphics g)
{
g.setColor(Color.GREEN);
for(int i = 0; i < aliens.size(); i++)
{
g.fillRect((int)aliens.elementAt(i).getX(), (int)aliens.elementAt(i).getY(), ALIEN_W, ALIEN_H);
}
}
public void drawPlayer(Graphics g)
{
g.setColor(Color.WHITE);
g.fillRect((int)player.getX(), (int)player.getY(), player.getWidth(), player.getHeight());
}
public void defeat()
{
System.out.println("You lose");
}
public void victory()
{
System.out.println("You win!");
}
//check collision
public void collide()
{
for(int i = 0; i < shots.size(); i++)
{
if(shots.elementAt(i).getY() < 0)
{
shots.remove(i);
}
for(int j = 0; j < aliens.size(); j++)
{
if(shots.size() == 0)
break;
//shots.elementAt(i).y >= aliens.elementAt(j).y mistake somewhere here
if(shots.elementAt(i).getX() < aliens.elementAt(j).getX() + ALIEN_W/2 && shots.elementAt(i).getX() > aliens.elementAt(j).getX() - ALIEN_W/2
&& shots.elementAt(i).getY() >= aliens.elementAt(j).getY() && shots.elementAt(i).getY() <= aliens.elementAt(j).getY() + ALIEN_H)
{
shots.remove(i);
aliens.remove(j);
}
}
}
}
public static void main(String[] args) {
App a = new App();
}
class MyKeyAdapter extends KeyAdapter
{
public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_RIGHT)
{
if((player.getX() + RECT_WIDTH) < getSize().width - 1)
{
player.addToX(20);
repaint();
}
}
else if(ke.getKeyCode() == KeyEvent.VK_LEFT)
{
if(player.getX() > 0)
{
player.addToX(-20);
repaint();
}
}
else if(ke.getKeyCode() == KeyEvent.VK_SPACE)
{
shots.add(new Shot(player.getX(), player.getY()));
}
}
}
}
//TODO: with abstract class contains(coords, state, image)
class Alien extends Object
{
//change all members to private
private double ALIEN_SPEED = 0.5;
Alien(int x ,int y, int w, int h)
{
super(x, y, w, h);
}
public double getSpeed()
{
return ALIEN_SPEED;
}
}
class Shot
{
//change all members to private
private static int S_WIDTH = 6;
private static int S_HEIGHT = 10;
private double x;
private double y;
public Shot(double x, double y)
{
this.x = x + S_WIDTH;
this.y = y - S_HEIGHT;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public void addToY(double y)
{
this.y += y;
}
}
class Player extends Object
{
Player(int x, int y, int w, int h)
{
super(x, y, w, h);
}
}