-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerShip.java
More file actions
90 lines (61 loc) · 1.58 KB
/
Copy pathPlayerShip.java
File metadata and controls
90 lines (61 loc) · 1.58 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
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class PlayerShip {
private JPanel panel;
private int x;
private int y;
private int width;
private int height;
private int dx;
private int dy;
private Rectangle2D.Double ship;
private Dimension dimension;
private Image shipImage;
public PlayerShip (JPanel p, int xPos, int yPos) {
panel = p;
dimension = panel.getSize();
x = xPos;
y = yPos;
dx = 50; // make bigger (smaller) to increase (decrease) speed
dy = 0; // no movement along y-axis allowed (i.e., move left to right only)
width = 50;
height = 30;
shipImage = ImageManager.loadImage("playerShip.png");
}
public void draw (Graphics2D g2) {
g2.drawImage(shipImage, x, y, width, height, null);
}
public void move (int direction) {
if (!panel.isVisible ()) return;
dimension = panel.getSize();
if (direction == 1) { // move left
x = x - dx;
if (x < 0)
x = 0;
}
else
if (direction == 2) { // move right
x = x + dx;
if (x + width > dimension.width)
x = dimension.width - width;
}
}
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, width, height);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}