-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullet.java
More file actions
69 lines (55 loc) · 1.47 KB
/
Bullet.java
File metadata and controls
69 lines (55 loc) · 1.47 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
import java.awt.*;
/**
* Represents a bullet in the Space Invaders game.
* This class manages the properties and behaviors of bullets being fired both
* from aliens and player
*/
public final class Bullet {
private int x;
private int y;
private int direction;
public static final int BULLET_SIZE = 5;
public static final int BULLET_SPEED = 5;
/**
* Constructs a new Bullet with specified coordinates and direction.
*
* @param x The x-coordinate of the bullet.
* @param y The y-coordinate of the bullet.
* @param direction The direction of the bullet's movement.
*/
public Bullet(int x, int y, int direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
/**
* Updates the bullet's position based on its direction and speed.
*/
public final void update() {
y += direction * BULLET_SPEED;
}
/**
* Renders the bullet on the game panel.
*
* @param g2d The Graphics2D object used for drawing.
*/
public final void render(Graphics2D g2d) {
// Set the color to red
g2d.setColor(Color.RED);
g2d.fillRect(x, y, BULLET_SIZE, BULLET_SIZE);
}
/**
*
* @return y The y-coordinate of the bullet.
*/
public final int getY() {
return y;
}
/**
*
* @return x The x-coordinate of the bullet.
*/
public final int getX() {
return x;
}
}