-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mathematica.nb
234 lines (198 loc) · 7.99 KB
/
Mathematica.nb
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
ClearAll["Global`*"]
ClearSystemCache[]
RemoveScheduledTask[ScheduledTasks[]];
DynamicModule[
{
army = {},
bullet = {},
missle = {},
direct = {},
reach = 0,
score = 0,
toClean,
num = 5,
pos,
(*reach=0,*)
reachNum = 10,
RestartAll,
pausing = True,
stop = True,
rand,
least,
launcher,
target,
hit,
explosion = 10,
crash = 10,
bulletSpeed = .6,
enermySpeed = .1,
jet = Graphics[
{Darker@Blue, Polygon[{{0, 0}, {1, 0}, {1, .1}, {0, .1}}],
Polygon[{{0.5, 2}, {.4, .6}, {.6, .6}}],
Polygon[{{-1, 1}, {2, 1}, {.5, 1.2}}],
Thickness -> .02, Arrow[{{.2, .1}, {.2, 1.5}}],
Arrow[{{.8, .1}, {.8, 1.5}}]}, ImageSize -> Tiny],
enermy = Graphics[
{Gray, Polygon[{{.1, 2}, {.9, 2}, {.9, 1.9}, {.1, 1.9}}],
Polygon[{{.5, 0}, {.4, 1.9}, {.6, 1.9}}],
Polygon[{{-1, 1.1}, {2, 1.1}, {.5, .9}}],
Thickness -> .02, Arrow[{{.2, 1.4}, {.2, 0.6}}],
Arrow[{{.8, 1.4}, {.8, .6}}]}, ImageSize -> Tiny]
},
least[bullets_,
army_] :=(*Get the local (pseduo) minimum distance between bullets \
and enermy army*)
Module[{len1 = Length@bullets, len2 = Length@army, frontList,
postList, dist, loc, short, long, locShort, locLong},
If[len1 >= len2, frontList = bullets; postList = army,
frontList = army; postList = bullets];
dist = (EuclideanDistance @@@
Partition[Riffle[frontList, postList], 2, 1]);
loc = First@Flatten@Position[dist, Min[dist], 1(*,1*)];
locShort =
Mod[Mod[loc, 2] + Quotient[loc, 2], Length@postList, 1];
locLong = Quotient[loc, 2] + 1;
short = postList[[locShort]];
long = frontList[[locLong]];
Return[
If[len1 >= len2, {locShort, locLong, Min@dist}, {locLong,
locShort, Min@dist}]]
];
rand[] := {RandomReal[{-30, 40}],
160};(*Randomly generate enermy flights*)
RestartAll[] := (RemoveScheduledTask[ScheduledTasks[]];
RunScheduledTask[
If[! pausing,
If[(*Randomly generate enermy flights*)
MemberQ[
RandomChoice[
{(Length@army)/num,
1 - (Length@army)/num} -> {False, True},
1
],
True],
AppendTo[army, {enermy, rand[]}]
];
If[(*Handle the situate of collision between the jet and enermy \
army*)
MemberQ[
Norm[
#[[2]] - MousePosition["Graphics", {0, 0}]
] < crash & /@ army,
True],
(*Speak["Bang"];*)
RemoveScheduledTask[ScheduledTasks[]];
CreateDialog[{TextCell["You Crash!", "Title"],
Column@{Row@{"Score: ", score}, Row@{"Passed:", reach}},
DefaultButton[]}]
];
If[(*Handle the situate of enermy army be hit by our bullets*)
Length@army > 0 \[And] Length@bullet > 0,
hit = least[bullet, army[[All, 2]]];
(*Print[hit];*)
If[
Last@hit < explosion,
score += 10;
army = Delete[army, First@hit];
bullet = Delete[bullet, hit[[2]] ]
]
];
If[(*enermy gives out missles*)
Length@army > 0 && RandomReal[] > .05,
launcher = RandomChoice@army[[All, 2]];
(AppendTo[missle, launcher];
AppendTo[direct,
Normalize[-launcher + MousePosition["Graphics", {0, 0}]]]);
];
If[(*Handle the situate of being hit*)
MemberQ[
Norm[
# - MousePosition["Graphics", {0, 0}]
] < crash & /@ missle,
True],
RemoveScheduledTask[ScheduledTasks[]];
CreateDialog[{TextCell["You are hit!", "Title"],
Column@{Row@{"Score: ", score}, Row@{"Passed:", reach}},
DefaultButton[]}]
];
If[(*If more than reachNum of enermies reach the bottom,
lose the game*)
reach >= reachNum,
RemoveScheduledTask[ScheduledTasks[]];
CreateDialog[{TextCell["You Lost the Base!", "Title"],
Column@{Row@{"Score: ", score}, Row@{"Passed:", reach}},
DefaultButton[]}]
];
];
1
]);
Panel[ Column@{
Column@{Row@{Text[Style["score: ", Black, 18]],
Text[Style[Dynamic[score], Orange, 16]]},
Row@{Text[Style["passed:", Black, 18]],
Text[Style[Dynamic[reach], Orange, 16]],
Text[Style["/", Orange, 16]],
Text[Style[reachNum, Orange, 16]]},
Button[
Dynamic[If[! stop, "Restart", "Start"]],
RestartAll[];
stop = pausing = False;
score = 0; reach = 0],
Button[
Dynamic[If[! pausing, "Cease", "Resume"]],
If[pausing, pausing = False, pausing = True],
Enabled -> Dynamic[! stop]],
Dynamic[Text[
Style[If[! stop, If[! pausing, "fighting", "pausing"],
"Stop"], Black, 16]]]},
MouseAppearance[
EventHandler[
Labeled[Deploy@Framed@Graphics[{
PointSize[Large],
Blue,
Dynamic[
Point[(bullet[[All, 2]] += bulletSpeed(*.5*);
bullet = Select[bullet, #[[2]] < 150 &]; bullet)],
TrackedSymbols :> {bullet}(*Handle the bullets*)
],
Red,
Dynamic[
Point[
(
pos = Position[missle, #] & /@
Select[missle, #[[1]] < -50 || #[[1]] >
50 || #[[2]] > 150 || #[[2]] < 10 &, 1];
If[Length@pos > 0,
toClean = # & @@ pos;
(*Print[toClean];*)
missle = Delete[missle, toClean];
direct = Delete[direct, toClean]];
missle += .5 direct)
],
TrackedSymbols :> {missle}(*Handle the missles*)
],
Dynamic[Thread[Inset[(*Handle the enermy army*)
Magnify[enermy, .5], #
] &[
If[! pausing,
reach += Length@Select[army, #[[2, 2]] < 5 &]];
army = Select[army, #[[2, 2]] > 5 &];
army[[All, 2, 2]] -=(*.4*)enermySpeed;
army[[All, 2]]
]],
TrackedSymbols :> {army}]
},
PlotRange -> {{-50, 50}, {0, 150}},
ImageSize -> 300
],
Text[Style["Thunder & Lighting", Darker@Green, Italic, 24]],
Top],
{"MouseClicked" :> {(*Speak["Shu"];*)
AppendTo[bullet,
MousePosition[
"Graphics", {0, 0}]]}}(*Shoot on Mouse Click Action*)
],
Magnify[jet, .5]
]
}]
]