-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.java
More file actions
178 lines (173 loc) · 4.7 KB
/
Sprite.java
File metadata and controls
178 lines (173 loc) · 4.7 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//imported to use images
import java.awt.*;
public class Sprite{
// Rectangle class whichholds width height and coordinates
Rectangle area;
Image img;
boolean visible = true;
//gets image from main program (constructor)
public Sprite(Image i){
//i = image from program
img = i;
//img.getWidth/Height Returns width/height of image
area = new Rectangle (1,1,img.getWidth(null),img.getHeight(null));
}
//overloaded constructor to give coordinates
public Sprite (Image i, int x, int y){
img = i;
area = new Rectangle (x, y, img.getWidth(null),img.getHeight(null));
}
public Sprite (Image i, int x, int y,int width, int height){
img = i;
area = new Rectangle (x, y, width, height);
}
//reduce x to move to left
public void moveLeft(){
area.x -= 1;
}
//reduce x by number of pixels to move to left
public void moveLeft(int px){
area.x -= px;
}
//increase x to move to right
public void moveRight(){
area.x += 1;
}
//increase x by number of pixels to move to right
public void moveRight(int px){
area.x += px;
}
//reduce y to move up
public void moveUp(){
area.y -= 1;
}
//reduce y by a number of pixels to move up
public void moveUp(int px){
area.y -= px;
}
//increase y to move down
public void moveDown(){
area.y += 1;
}
//increase y by a number of pixels to move down
public void moveDown(int px){
area.y += px;
}
//changes coordinates of image/char
public void moveTo(int x, int y){
area.x = x;
area.y = y;
}
//draws image/char on screen
public void paint(Graphics g){
if(visible == true){
g.drawImage(img, area.x, area.y, null);
}
}
//draws image in case of bounds for collision the rectangle is moved
public void paint(Graphics g, int x, int y){
if(visible == true){
g.drawImage(img, area.x - x, area.y - y, null);
}
}
//@Override
public void update(Graphics g){
paint(g);
}
//collision detection checks if areas overlap if collides returns true else false
public boolean hasCollidedWith(Sprite other){
return this.area.intersects (other.area);
}
//Collision detection with a rectangle
public boolean hasCollidedWith(Rectangle rect){
return this.area.intersects (rect);
}
//Collide with a point
public boolean hasCollidedWith(int x, int y){
Rectangle rect = new Rectangle(x, y, 1, 1);
return this.area.intersects (rect);
}
//Collision detection with a circle sprite
public boolean hasCollidedWith(CircleSprite circ){
if(circ.hasCollidedWith(area)){
return true;
}else{
return false;
}
}
//Get x coordinate
public int getX(){
return area.x;
}
//get y coordinate
public int getY(){
return area.y;
}
//set x coordinate
public void setX(int x){
area.x = x;
}
//set y coordinate
public void setY(int y){
area.y = y;
}
//set x and y coordinate
public void setPoints(int x, int y){
area.x = x;
area.y = y;
}
//get the width of the sprite
public int getWidth(){
return area.width;
}
//set the width of the sprite
public void setWidth(int w){
area.width = w;
}
//get the height of the sprite
public int getHeight(){
return area.height;
}
//set the height of the sprite
public void setHeight(int h){
area.height = h;
}
//get the X midpoint
public int getMidpointX(){
return area.x + area.width/2;
}
//get the y midpoint
public int getMidpointY(){
return area.y + area.height/2;
}
//Get the image
public Image getImage(){
return img;
}
//Replace image
public void setImage(Image newImage){
img = newImage;
}
//changes image of sprite
public void setImage2(Image newImage){
img = newImage;
//keep old values
int x = area.x;
int y = area.y;
//changes size
area = new Rectangle (x, y, img.getWidth(null), img.getHeight(null) );
}
//changes image of sprite and give it it's new width and height
public void setImage(Image newImage, int width, int height){
img = newImage;
//keep old values
int x = area.x;
int y = area.y;
//changes size
area = new Rectangle (x, y, width, height);
}
//makes sprite invisible/visible
public void setVisible(boolean v){
visible = v;
}
}