-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBullet.java
More file actions
104 lines (85 loc) · 2.03 KB
/
Bullet.java
File metadata and controls
104 lines (85 loc) · 2.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
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Bullet {
private TwoPlayerShooterGamee game;
private Character shooter;
public final int WIDTH = 10;
public final int HEIGHT = 50;
int x = 0;
int xa = 0;
int y = 0;
int ya = 0;
int speed = 3;
File imgSource1 = new File("res/Bullet.png");
File imgSource2 = new File("res/BossBullet.png");
BufferedImage bullet;
boolean playerBullet = false;
public Bullet(TwoPlayerShooterGamee game, Character shooter) {
this.game = game;
this.shooter = shooter;
if(shooter.getClass().equals(new Player(game).getClass())){
playerBullet = true;
try {
bullet = ImageIO.read(this.getClass().getResource("/res/Bullet.png"));
} catch (IOException e) {
e.printStackTrace();
}
game.addPlayerBullet(this);
}
else{
playerBullet = false;
try {
bullet = ImageIO.read(this.getClass().getResource("/res/BossBullet.png"));
} catch (IOException e) {
e.printStackTrace();
}
game.addBossBullet(this);
}
}
void move() {
// x = x + xa;
if(!collision()){
if(playerBullet)
y = y - speed;
else
y = y + speed;
}
//delete bullets that went off map
if(y < 0 - HEIGHT || y > game.WINDOW_HEIGHT)
delete();
}
public void delete(){
game.playerBulletList.remove(this);
game.bossBulletList.remove(this);
}
private boolean collision() {
if(game.player.getBounds().intersects(getBounds())){
game.player.shot(this);
return true;
}
else if(game.boss.getBounds().intersects(getBounds())){
game.boss.shot(this);
return true;
}
else
return false;
}
public void paint(Graphics2D g) {
g.drawImage(bullet, x, y, WIDTH, HEIGHT, game);
}
public Rectangle getBounds() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public void reset(){
y = game.WINDOW_HEIGHT - 75;
x = game.WINDOW_WIDTH / 2 - (WIDTH / 2);
xa = 0;
ya = 0;
speed = 1;
}
}