-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball2_1.m
229 lines (183 loc) · 7.2 KB
/
ball2_1.m
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
function ball2_1()
[yy, fss]=audioread('戰鬥音效.mp3'); % 讀取音訊檔
yy = repmat(yy, 10, 1);
sound(yy, fss);% 播放音訊
fig = figure('KeyPressFcn',@kpfcn,'KeyReleaseFcn',@krfcn);
set(gcf,'unit','normalized','position',[0.2,0.2,0.64,0.64])
set (gca,'position',[0.05,0.1,0.6,0.8]);
ball = scatter(1,1,200,'r','filled');
hold on;
ball2 = scatter(2,1,200,'y','filled');
boss = scatter(2,1,300,'b','p');
bossposition = [boss.XData boss.YData];
axis([0 3 0 3])
ispress = struct('uparrow',false,'downarrow',false,'rightarrow',false,'leftarrow',false,'space',false,'d',false,'a',false,'w',false,'s',false,'x',false);
game = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',@gamefcn);
game2 = timer('ExecutionMode','Fixedspacing','Period',5,'TimerFcn',@bossfcn);
start(game)
start(game2)
dir = [0 0];
game3 = timer('ExecutionMode','Fixedspacing','Period',0.1,'TimerFcn',@skill);
start(game3)
game4 = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',@gamefcn2);
start(game4)
game5 = timer('ExecutionMode','Fixedspacing','Period',0.1,'TimerFcn',@skill2);
start(game5)
h=uicontrol('style','push','position',[1000 40 80 30],'string','存活');
uicontrol('style','text','position',[900 300 110 30],'string','玩家1(紅色)移動方式');
uicontrol('style','text','position',[900 280 80 30],'string','上');
uicontrol('style','text','position',[900 260 80 30],'string','左下右');
uicontrol('style','text','position',[900 220 80 30],'string','攻擊方式');
uicontrol('style','text','position',[900 200 80 30],'string','space');
uicontrol('style','text','position',[1100 300 110 30],'string','玩家2(黃色)移動方式');
uicontrol('style','text','position',[1100 280 80 30],'string','W');
uicontrol('style','text','position',[1100 260 80 30],'string','ASD');
uicontrol('style','text','position',[1100 220 80 30],'string','攻擊方式');
uicontrol('style','text','position',[1100 200 80 30],'string','X');
uicontrol('style','text','position',[1000 70 100 30],'string','任務目標:擊中星星');
uicontrol('style','text','position',[950 330 80 30],'string','boss的HP:');
j=uicontrol('style','text','position',[1020 330 80 30],'string','100');
uicontrol('style','text','position',[950 360 80 30],'string','玩家的HP:');
k=uicontrol('style','text','position',[1020 360 80 30],'string','100');
function gamefcn(obj,event)
ball.XData(ball.XData > 3 ) = ball.XData(ball.XData > 3) - 3;
ball.XData(ball.XData < 0 ) = ball.XData(ball.XData < 0) + 3;
ball.YData(ball.YData > 3 ) = ball.YData(ball.YData > 3) - 3;
ball.YData(ball.YData < 0 ) = ball.YData(ball.YData < 0) + 3;
if ispress.uparrow
ball.YData = ball.YData + 0.1;
dir = [0 0.1];
value = 1;
else
value = 0;
end
if ispress.downarrow
ball.YData = ball.YData - 0.1;
dir = [0 -0.1];
end
if ispress.rightarrow
ball.XData = ball.XData + 0.1;
dir = [0.1 0];
end
if ispress.leftarrow
ball.XData = ball.XData - 0.1;
dir = [-0.1 0];
end
if abs(ball.XData-boss.XData)<0.1 && abs(ball.YData-boss.YData)<0.1
player_HP = str2num(get(k,'string'));
player_HP = player_HP - 20;
set(k,'string',player_HP);
if str2num(get(k,'string')) == 0
clear sound;
delete(boss);
[y, fs]=audioread('瑪莉歐死掉音效.mp3'); % 讀取音訊檔
sound(y, fs); % 播放音訊
set(h,'string','擊殺');
close
game_lose
end
end
end
function skill(obj,event)
if ispress.space
[y, fs]=audioread('fart.wav'); % 讀取音訊檔
sound(y, fs); % 播放音訊
ski = scatter(ball.XData,ball.YData,100,'g','filled');
for ii = 1:15
if dir(1) == 0
ski.YData = ski.YData + dir(2)*2;
elseif dir (2) == 0
ski.XData = ski.XData + dir(1)*2;
end
drawnow;
if abs(ski.XData-boss.XData)<0.1 && abs(ski.YData-boss.YData)<0.1
HP=str2num(get(j,'string'));
HP=HP-10;
set(j,'string',HP);
if str2num(get(j,'string')) <= 20
clear sound;
delete(boss);
set(h,'string','擊中');
close
game2_in_2_1
end
end
end
%skiposition = [ski.XData ski.YData];
end
end
function krfcn(obj,event)
ispress.(event.Key) = false;
end
function kpfcn(obj,event)
ispress.(event.Key) = true;
end
function bossfcn(obj,event)
boss.XData = rand+rand+rand;
boss.YData = rand+rand+rand;
end
function gamefcn2(obj,event)
ball2.XData(ball2.XData > 3 ) = ball2.XData(ball2.XData > 3) - 3;
ball2.XData(ball2.XData < 0 ) = ball2.XData(ball2.XData < 0) + 3;
ball2.YData(ball2.YData > 3 ) = ball2.YData(ball2.YData > 3) - 3;
ball2.YData(ball2.YData < 0 ) = ball2.YData(ball2.YData < 0) + 3;
if ispress.w
ball2.YData = ball2.YData + 0.1;
dir = [0 0.1];
value = 1;
else
value = 0;
end
if ispress.s
ball2.YData = ball2.YData - 0.1;
dir = [0 -0.1];
end
if ispress.d
ball2.XData = ball2.XData + 0.1;
dir = [0.1 0];
end
if ispress.a
ball2.XData = ball2.XData - 0.1;
dir = [-0.1 0];
end
if (abs(ball.XData-boss.XData)<0.1 && abs(ball.YData-boss.YData)<0.1) || (abs(ball2.XData-boss.XData)<0.1 && abs(ball2.YData-boss.YData)<0.1)
player_HP = str2num(get(k,'string'));
player_HP = player_HP - 20;
set(k,'string',player_HP);
if str2num(get(k,'string')) == 0
clear sound;
delete(boss);
[y, fs]=audioread('瑪莉歐死掉音效.mp3'); % 讀取音訊檔
sound(y, fs); % 播放音訊
set(h,'string','擊中');
close
game_lose
end
end
end
function skill2(obj,event)
if ispress.x
[y, fs]=audioread('fart2.wav'); % 讀取音訊檔
sound(y, fs); % 播放音訊
ski = scatter(ball2.XData,ball2.YData,100,'b','filled');
for ii = 1:15
if dir(1) == 0
ski.YData = ski.YData + dir(2)*2;
elseif dir (2) == 0
ski.XData = ski.XData + dir(1)*2;
end
drawnow;
if abs(ski.XData-boss.XData)<0.1 && abs(ski.YData-boss.YData)<0.1
HP=str2num(get(j,'string'));
HP=HP-10;
set(j,'string',HP);
if str2num(get(j,'string')) <= 20
close
game2_in_2_1
end
end
end
%skiposition = [ski.XData ski.YData];
end
end
end