-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatching.m
More file actions
237 lines (201 loc) · 6.98 KB
/
Copy pathcatching.m
File metadata and controls
237 lines (201 loc) · 6.98 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
% Chih Jung Lee
% Exercise 7: Mini Project
% A MATLAB 2 Player Catching Game
% Clean up before game begins
clear all
close all
clc
% Initialise key states for Player and Catcher
pressedKey1 = 0;
pressedKey2 = 0;
% Obtain figure dimensions
screensize = get(groot,'Screensize');
dimen = [(screensize(3) - screensize(4))/2 0 screensize(4) screensize(4)];
maxSize = 30;
% Initialise game variables
playerPos = [-(maxSize/2 - 5); 0];
playerMove = [0 0];
catcherPos = [maxSize/2 - 5; 0];
catcherMove = [0 0];
speed = 0.5;
[foodX1 foodY1] = foodSpawn(maxSize);
[foodX2 foodY2] = foodSpawn(maxSize);
[foodX3 foodY3] = foodSpawn(maxSize);
score = 0;
% Plot Player
scatter(playerPos(1),playerPos(2),100,'filled','b');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
% Plot Enemy
scatter(catcherPos(1),catcherPos(2),100,'filled','r');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
% Plot Food Items
scatter(foodX1,foodY1,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(foodX2,foodY2,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(foodX3,foodY3,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
set(gcf,'position',dimen);
set(gcf,'color','cyan');
set(gcf,'KeyPressFcn',@stroke)
% Game Stops when Catcher catches the Player
while ~checkcollision(playerPos(1,1),playerPos(2,1),catcherPos(1,1),catcherPos(2,1))
% Game Stops when Player exits the Game Area
if outofboundscheck(playerPos(1,1),playerPos(2,1),maxSize)
break;
end
pressedKey1 = get(gcf,'CurrentKey');
switch pressedKey1
% Player controls
case 'w'
playerMove(1) = 0;
if playerMove(2) == -speed
playerMove(2) = speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
end
playerMove(2) = speed;
playerPos(2,1) = playerPos(2,1) + playerMove(2);
case 's'
playerMove(1) = 0;
if playerMove(2) == speed
playerMove(2) = -speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
end
playerMove(2) = -speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
case 'd'
playerMove(2) = 0;
if playerMove(1) == -speed
playerMove(1) = speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
end
playerMove(1) = speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
case 'a'
playerMove(2) = 0;
if playerMove(1) == speed
playerMove(1) = -speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
end
playerMove(1) = -speed;
playerPos = translate(playerPos,playerMove(1),playerMove(2));
% keep Player inertia if another key is pressed
otherwise
playerPos = translate(playerPos,playerMove(1),playerMove(2));
end
pressedKey2 = get(gcf,'CurrentKey');
switch pressedKey2
% Catcher controls
case 'uparrow'
catcherMove(1) = 0;
if catcherMove(2) == -speed
catcherMove(2) = speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
end
catcherMove(2) = speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
case 'downarrow'
catcherMove(1) = 0;
if catcherMove(2) == speed;
catcherMove(2) = -speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
end
catcherMove(2) = -speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
case 'rightarrow'
catcherMove(2) = 0;
if catcherMove(1) == -speed
catcherMove(1) = speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
end
catcherMove(1) = speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
case 'leftarrow'
catcherMove(2) = 0;
if catcherMove(1) == speed
catcherMove(1) = -speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
end
catcherMove(1) = -speed;
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
% keep Catcher inertia if another key is pressed
otherwise
catcherPos = translate(catcherPos,catcherMove(1),catcherMove(2));
end
% Updating Player score
if [playerPos(1,1) playerPos(2,1)] == [foodX1 foodY1]
[foodX1 foodY1] = foodSpawn(maxSize);
score = score + 1;
elseif [playerPos(1,1) playerPos(2,1)] == [foodX2 foodY2]
[foodX2 foodY2] = foodSpawn(maxSize);
score = score + 1;
elseif [playerPos(1,1) playerPos(2,1)] == [foodX3 foodY3]
[foodX3 foodY3] = foodSpawn(maxSize);
score = score + 1;
end
clf
% Continuously plot all items
plot([-maxSize/2 -maxSize/2 maxSize/2 maxSize/2 -maxSize/2],...
[maxSize/2 -maxSize/2 -maxSize/2 maxSize/2 maxSize/2],'k','LineWidth',4)
hold on
scatter(foodX1,foodY1,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(foodX2,foodY2,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(foodX3,foodY3,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(playerPos(1,1),playerPos(2,1),100,'filled','b');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(catcherPos(1,1),catcherPos(2,1),100,'filled','r');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
drawnow
end
% plots remain after game ends
plot([-maxSize/2 -maxSize/2 maxSize/2 maxSize/2 -maxSize/2],...
[maxSize/2 -maxSize/2 -maxSize/2 maxSize/2 maxSize/2],'k','LineWidth',4)
hold on
scatter(foodX1,foodY1,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(foodX2,foodY2,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(foodX3,foodY3,50,'filled','magenta');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(playerPos(1,1),playerPos(2,1),100,'filled','b');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
scatter(catcherPos(1,1),catcherPos(2,1),100,'filled','r');
xlim([-maxSize/2 maxSize/2]);
ylim([-maxSize/2 maxSize/2]);
hold on
% Show Player score after game ends
msgbox(strcat('Score: ',num2str(score)));
hold on
function dir = stroke(src, event)
end