-
Notifications
You must be signed in to change notification settings - Fork 3
/
p2.html
268 lines (201 loc) · 5.35 KB
/
p2.html
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>RetroPython-p2</title>
<!-- // <script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>
// <script type="text/javascript" src="http://cdn.bootcss.com/socket.io/1.4.8/socket.io.min.js"></script> -->
<!-- // <script src="js/jquery.min.js"></script> -->
<script src="/static/socket.io.min.js"></script>
<script src="/static/jquery.min.js"></script>
<style>
p{
font-size: 50px!important;
}
</style>
<script>
var sendToSever = 'p2_to_p1'
function deepCopy(p, c) {
var c = c || {};
for (var i in p) {
if (typeof p[i] === 'object') {
c[i] = (p[i].constructor === Array) ? [] : {};
deepCopy(p[i], c[i]);
} else {
c[i] = p[i];
}
}
return c;
}
int = parseInt
float = parseFloat
log = console.log
debug = console.debug
info = console.info
table = console.table
len=(x)=>x.length
rn = (x)=> int(x*Math.random())
function range (f,n,max) {
// 1.Python 用法
// 2.执行 f(n['property']) 并返回新的 [] 或 {}
// range(10) =>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// range(x=>x*2,5,10) =>[10, 12, 14, 16, 18]
// range(x=>x+x,['a', 'b', 'c'])
// =>["aa", "bb", "cc"]
var flag = (typeof f == "number")
if(flag){
max =(typeof n == "number")?n:max
n = f
f = x=>x
}
var items = n
var obj = (n.constructor === Array)?[]:{}
if(typeof n == "number"){
obj = []
items = []
var i=max?n:0
max=max||n
while(i!=max){items.push(i);i++}
}
for (var i in items) {
obj[i]=f(items[i])
}
return obj
}
sum = (arr)=>{
var summ = 0
for (var i in arr){
summ += arr[i]
}
return summ
}
var prot = 80
var ad ='http://' + document.domain + ':'+prot+'/game'
log(ad)
var socket = io.connect(ad);
socket.on('end', function(p) {
var r = [
"<br><br><h1>Die together!,no winer! </h1><br><h2>平局,同归于尽!</h2>",
"<br><br><h1>player "+p+" win!</h1><br><h2>真遗憾,你输了!</h2>",
"<br><br><h1>player "+p+" win!</h1><br><h2>恭喜恭喜,你赢了!</h2>"]
if(sendToSever!='p2_to_p1'){
r=[r[0],r[2],r[1]]
}
var html = r[p]
$('#show').html('<br><hr><br>刚才比赛的结果:' + html )
})
socket.on('response', function(msg) {
log('response:' + msg.data);
})
socket.on('testLine', function(msg) {
log('testLine:' + msg.data);
})
socket.emit('testLine',{data:'p2 successed to pi!'})
</script>
</head>
<body>
<h2>Player 2 info:</h2>
<p>前后倾角:<span id="x">0</span></p>
<p>左右倾角:<span id="y">0</span></p>
<p>是否加速:<span id="z">0</span></p>
<div id='intro'>
<h2>
说明:<br>将手机横着放,水平于地平面 <br>
朝前后左右方向倾斜 以控制小蛇 <br>
点击屏幕获得加速 <br><br>
<small>RetroPython code by UCE 小磊</small>
</h2>
</div>
<div id='show'>
</div>
<div id="null" style="width:100%;position:fixed;top:0px;left:0px;height:100%;z-index=2" ></div>
</body>
<script>
// var ctx = document.getElementById('canvas').getContext('2d');
var player1 = [0,0,0]
function emit () {
// body...
$('#x').html(window.player1[1])
$('#y').html(window.player1[0])
$('#z').html(window.player1[2]?true:false)
socket.emit(window.sendToSever, window.player1)
socket.emit(window.sendToSever, window.player1)
}
window.addEventListener('touchstart',function(e){
window.player1[2] = 1
window.emit()
})
window.addEventListener('touchend',function(e){
window.player1[2] = 0
window.emit()
})
$(document).keydown(function(event){
if([38,39,40,37,32].indexOf(event.keyCode)!=-1){
event.preventDefault()
if(event.keyCode==38){window.player1[1]= 1}
if(event.keyCode==39){window.player1[0]= 1}
if(event.keyCode==40){window.player1[1]=-1}
if(event.keyCode==37){window.player1[0]=-1}
if(event.keyCode==32){window.player1[2]= 1}
window.emit()
}
});
$(document).keyup(function(event){
if([38,39,40,37,32].indexOf(event.keyCode)!=-1){
event.preventDefault()
if(event.keyCode==38){window.player1[1]= 0}
if(event.keyCode==39){window.player1[0]= 0}
if(event.keyCode==40){window.player1[1]= 0}
if(event.keyCode==37){window.player1[0]= 0}
if(event.keyCode==32){window.player1[2]= 0}
window.emit()
}
});
var dealControl=(x)=>{
if(x>20){return 1}
if(x<-20){return -1}
return 0
}
function orientationHandler(event) {
var control=[
dealControl(event.beta),
dealControl(event.gamma),
]
var changeTag = 0
if(control[0]!=window.player1[0]){
window.player1[0]=control[0]
changeTag = 1
}
if(control[1]!=window.player1[1]){
window.player1[1]=control[1]
changeTag = 1
}
if(changeTag==1){
window.emit()
}
}
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", orientationHandler, true);
} else {
document.body.innerHTML = "Can't found window.DeviceOrientationEvent, Maybe need one of them: \n1) using 127.0.0.1 url \n2)using fire fox \n3) hosted over https.";
}
function controlToButton(control,py){
py=py||0
if(control[2]== 1){py.speed=1}
if(control[2]== 0){py.speed=0}
if(control[0]== 1){py.button='r';return}
if(control[0]==-1){py.button='l';return}
if(control[1]== 1){py.button='u';return}
if(control[1]==-1){py.button='d';return}
}
// map.init(1)
// py = map.pythons[0]
// canvas.render()
// setInterval(()=>{
// controlToButton(window.player1,map.pythons[0])
// map.pythons[0].move()
// // socket.emit('buffer', map.code())
// canvas.render()
// },100)
</script>
</html>