-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.m
153 lines (111 loc) · 4.41 KB
/
ball.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
function ball()
[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;
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);
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)
h=uicontrol('style','push','position',[1000 40 80 30],'string','存活');
uicontrol('style','text','position',[1000 300 80 30],'string','移動方式');
uicontrol('style','text','position',[1000 280 80 30],'string','上');
uicontrol('style','text','position',[1000 260 80 30],'string','左下右');
uicontrol('style','text','position',[1000 220 80 30],'string','攻擊方式');
uicontrol('style','text','position',[1000 200 80 30],'string','space');
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('發射.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-100;
set(j,'string',HP);
if str2num(get(j,'string')) == 0
clear sound;
delete(boss);
[y, fs]=audioread('小朋友下樓梯音效.mp3'); % 讀取音訊檔
sound(y, fs); % 播放音訊
set(h,'string','擊中');
close;
game_win;
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
end