-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlien.java
More file actions
170 lines (141 loc) · 4.93 KB
/
Copy pathAlien.java
File metadata and controls
170 lines (141 loc) · 4.93 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
import org.w3c.dom.css.Rect;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
public class Alien {
private int x, y;
private Timer shootTimer;
private int bulletDelay;
public static final int alienWidth = 50;
public static final int alienHeight = 50;
public static double alienSpeed = 2.0;
private int health = 3;
public ImageIcon alienImage;
public static List<ImageCoordinator> alienImages;
public List<Rectangle> alienBullets;
private static final Random random = new Random();
int alienImageIndex;
static {
alienImages = new ArrayList<>();
alienImages.add(new ImageCoordinator("images/alien_1.png", "images/alien_2.png"));
alienImages.add(new ImageCoordinator("images/alien2_1.png", "images/alien2_2.png"));
alienImages.add(new ImageCoordinator("images/alien3_1.png","images/alien3_2.png" ));
alienImages.add(new ImageCoordinator("images/alien4_1.png", "images/alien4_2.png"));
alienImages.add(new ImageCoordinator("images/alien8_1.png", "images/alien8_2.png"));
alienImages.add(new ImageCoordinator("images/alien9_1.png", "images/alien9_2.png"));
alienImages.add(new ImageCoordinator("images/alien10_1.png", "images/alien10_2.png"));
alienImages.add(new ImageCoordinator("images/alien11_1.png", "images/alien11_2.png"));
}
public Alien() {
x = (int) (Math.random() * (GamePanel.PANEL_WIDTH - Alien.alienWidth));
y = -alienHeight;
alienBullets = new ArrayList<>();
alienImageIndex = getRandomAlienImageIndex();
alienImage = alienImages.get(alienImageIndex).getImage();
bulletDelay = random.nextInt(10000);
shootTimer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!GamePanel.gameOver) {
shootBullet();
}
}
});
TimerTask task = new TimerTask() {
@Override
public void run() {
shootBullet();
shootTimer.start();
}
};
new java.util.Timer().schedule(task, bulletDelay);
}
public void stop() {
shootTimer.stop();
alienBullets.clear();
}
public boolean hasCollision(Rectangle rect) {
Iterator<Rectangle> alienBulletIterator = alienBullets.iterator();
while (alienBulletIterator.hasNext()) {
Rectangle bullet = alienBulletIterator.next();
if (bullet.intersects(rect)) {
alienBulletIterator.remove();
return true;
}
}
return false;
}
//public Rectangle getAlien(){return alien;}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHealth() {
return health;
}
public void takeDamage() {
health--;
}
public boolean isDestroyed() {
return health <= 0;
}
public void draw(Graphics g) {
int drawX = x + (Alien.alienWidth - alienImage.getIconWidth()) / 2;
int drawY = y + (Alien.alienHeight - alienImage.getIconHeight()) / 2;
alienImage.paintIcon(null, g, drawX, drawY);
//alien bullets
g.setColor(Color.PINK);
for (Rectangle bullet : alienBullets) {
g.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
}
}
private void shootBullet() {
int bulletX = x + alienWidth / 2 - 2;
int bulletY = y + alienHeight;
Rectangle bullet = new Rectangle(bulletX, bulletY, 4, 10);
alienBullets.add(bullet);
}
int k = 0;
int xDirecton;
public void updateAlienPosition() {
y += alienSpeed;
k++;
if(k % 50 == 0)
{
alienImage = alienImages.get(this.alienImageIndex).switchImage();//to make aliens' arms up and down
}
if (k > 100) {
k = 0;
if (random.nextInt(10) > 5) {
xDirecton = 1;
} else
xDirecton = -1;
}
x = x + xDirecton;
if (y > GamePanel.PANEL_HEIGHT) {
health = 0;
}
updateAlienBulletsPosition();
}
private void updateAlienBulletsPosition() {
Iterator<Rectangle> bulletIterator = alienBullets.iterator();
while (bulletIterator.hasNext()) {
Rectangle bullet = bulletIterator.next();
bullet.y += 5;
if (bullet.y + bullet.height > GamePanel.PANEL_HEIGHT) { // Remove bullets that go off the bottom of the panel
bulletIterator.remove();
}
}
}
private static int getRandomAlienImageIndex() {
Random random = new Random();
int index = random.nextInt(alienImages.size());
return index;
}
}