-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.java
More file actions
110 lines (77 loc) · 2.22 KB
/
Copy pathBullet.java
File metadata and controls
110 lines (77 loc) · 2.22 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class Bullet extends Thread {
private JPanel panel;
private int x;
private int y;
private int width;
private int height;
private int dy; // increment to move along y-axis
private Dimension dimension;
private boolean isPlayerBullet; // true if fired by player, false if fired by alien
private boolean isActive; // true if bullet is still on screen
private Rectangle2D.Double bullet;
public Bullet (JPanel p, int xPos, int yPos, boolean playerBullet) {
panel = p;
dimension = panel.getSize();
x = xPos;
y = yPos;
width = 4;
height = 12;
isPlayerBullet = playerBullet;
isActive = true;
if (isPlayerBullet) {
dy = -10; // player bullets move upward
}
else {
dy = 10; // alien bullets move downward
}
}
public void draw (Graphics2D g2) {
// draw bullet as a yellow rectangle
g2.setColor(Color.YELLOW);
bullet = new Rectangle2D.Double(x, y, width, height);
g2.fill(bullet);
}
public void move() {
if (!panel.isVisible ()) return;
y = y + dy;
}
public void run () {
isRunning = true;
try {
while (isRunning && isActive) {
move();
// check if bullet is off screen
if (y < 0 || y > panel.getHeight()) {
isActive = false;
}
// Request repaint from GamePanel
if (panel instanceof GamePanel) {
((GamePanel) panel).repaint();
}
sleep (30); // sleep time controls bullet speed
}
}
catch(InterruptedException e) {}
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, width, height);
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean active) {
isActive = active;
}
public boolean isPlayerBullet() {
return isPlayerBullet;
}
boolean isRunning;
public void stopRunning() {
isRunning = false;
}
}