-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamePlayer.java
More file actions
186 lines (149 loc) · 3.31 KB
/
GamePlayer.java
File metadata and controls
186 lines (149 loc) · 3.31 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
179
180
181
182
183
184
185
186
// Alexandra Qin
package DoodleFall;
import java.awt.*;
public class GamePlayer
{
// initialize player speed
private int fall_y_speed = GC.PLAYER_START_FALL_SPEED;
private int move_x_speed = GC.PLAYER_MOVE_X_SPEED;
private int x_pos_left;
private int x_pos_right;
private int y_pos_up;
private int y_pos_down;
private boolean falling;
private boolean fallSet;
private boolean moving_left;
private boolean moving_right;
private boolean bonus_intersect;
private Image objectPic;
private Component parent;
public GamePlayer (int x, int y, Component parent)
{
//initialize player positions
x_pos_left = x;
x_pos_right = x + GC.PLAYER_IMAGE_WIDTH;
y_pos_up = y;
y_pos_down = y + GC.PLAYER_IMAGE_HEIGHT;
this.parent = parent;
}
public void setImages (Image objectPic)
{
this.objectPic = objectPic;
}
public int getXPosLeft()
{
return x_pos_left;
}
public int getXPosRight()
{
return x_pos_right;
}
public int getYPosUp()
{
return y_pos_up;
}
public int getYPosDown()
{
return y_pos_down;
}
public void setFallSpeed(int speed)
{
this.fall_y_speed = speed;
}
public int getFallSpeed()
{
return fall_y_speed;
}
public void hasSetFall(boolean value)
{
this.fallSet = value;
}
public boolean getFallSet()
{
return fallSet;
}
public void setFalling(boolean value)
{
this.falling = value;
}
public boolean getIsFalling()
{
return falling;
}
// returns true if player is out of bounds down
public boolean isOutDown()
{
return (y_pos_up > GC.APPLET_HEIGHT);
}
// returns true if player is out of bounds left
public boolean isOutLeft()
{
return (x_pos_right < 0);
}
// returns true if player is out of bounds right
public boolean isOutRight()
{
return (x_pos_left > GC.APPLET_WIDTH);
}
// resets player positions
public void resetPlayer(int x, int y)
{
x_pos_left = x;
x_pos_right = x + GC.PLAYER_IMAGE_WIDTH;
y_pos_up = y;
y_pos_down = y + GC.PLAYER_IMAGE_HEIGHT;
}
public void playerMoveLeft(boolean value)
{
moving_left = value;
}
public void playerMoveRight(boolean value)
{
moving_right = value;
}
// determines player's behavior is player is moving
public void playerMove()
{
if(moving_left)
{
x_pos_left -= move_x_speed;
x_pos_right -= move_x_speed;
}
else if(moving_right)
{
x_pos_left += move_x_speed;
x_pos_right += move_x_speed;
}
if(falling)
{
y_pos_up += fall_y_speed;
y_pos_down += fall_y_speed;
}
}
// returns true if player intersects enemy
public boolean intersects(GameEnemy enemy)
{
return ((x_pos_right > enemy.getXPosLeft() && (x_pos_left < enemy.getXPosRight())
&& (y_pos_down > enemy.getYPosUp() && y_pos_up < enemy.getYPosDown())));
}
// returns true if player intersects bonus
public boolean intersects(GameBonus bonus)
{
return ((x_pos_right >= bonus.getXPosLeft() && (x_pos_left <= bonus.getXPosRight())
&& (y_pos_down >= bonus.getYPosUp() && y_pos_up <= bonus.getYPosDown())));
}
// sets to true if player has intersected bonus
public void intersectsBonus(boolean value)
{
bonus_intersect = value;
}
// returns true if player has intersected bonus
public boolean hasIntersectedBonus()
{
return bonus_intersect;
}
public void paintPlayer(Graphics g)
{
g.drawImage(objectPic, x_pos_left, y_pos_up, parent);
}
}