forked from kdurand/space-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.java
43 lines (31 loc) · 856 Bytes
/
Bullet.java
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
import java.awt.*;
public class Bullet extends GameEntity{
double angle;
int speed = 10;
public String getImg(){
return "images/shot.gif";
}
public Bullet(int x,int y, double angle){
super(x,y);
setImage();
this.angle = angle * (Math.PI/180);
}
public void update(Space space) {
// Kill and get out of dodge if off screen
if(y<=0 || y>=space.HEIGHT || x<=0 || x>=space.WIDTH){
space.bullets.remove(this);
return;
}
x_vel = speed * Math.sin(angle);
y_vel = speed * Math.cos(angle);
x += x_vel;
y -= y_vel;
bounds.setLocation(getX(),getY());
}
public void draw(Graphics2D g) {
Graphics2D graphics = (Graphics2D)g.create();
graphics.rotate(angle,getx() + getWidth()/2,gety() + getHeight()/2);
graphics.drawImage(img,getX(),getY(),null);
graphics.dispose();
}
}