-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
112 lines (89 loc) · 2.79 KB
/
logic.js
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
var socket;
function init() {
socket = io.connect("http://localhost:1337");
socket.on('news', function (data) {
console.log(data);
//socket.emit('checkCoordinates', { x: 3333, y: 4444 });
});
}
var lastElement = null;
var timer = null;
var h = 0;
var w = 0;
highlight('h0_w0');
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function highlight(classname){
var randomColor = getRandomColor();
$('.'+lastElement).empty();
$('.'+lastElement).css('background-color', randomColor);
//$('.'+classname).css('background-color', 'red');
pulsate(classname);
timer = setTimeout(function(){
for (i=100; i >= 0; i--){
if(i>0){
if(i%2==0){
$('.'+classname).animate({ backgroundColor:'red' }, i);
$('.'+classname).empty().append('<img src="skull.png" style="width:100%;height:100%;"/>');
}else{
$('.'+classname).animate({ backgroundColor:'white' }, i);
}
}else{
clearTimeout(timer);
// $('#main').append('<img src="skull.png" style="width:100%;height:100%;position:absolute;left:0px;top:0px;"/>');
alert('you just died of boredom');
}
}
// Call reset timer here if you want the timer to start again
// when the alert is closed
// Otherwise, if you want to stop the timer forever,
// remove the event handler from document.onkeydown
}, 5000);
lastElement = classname;
}
function pulsate(classname){
for (i=0; i < 5; i++){
$('.'+classname).animate({ backgroundColor:getRandomColor() }, 500);
}
}
$(document).keydown(function(e) {
clearTimeout(timer);
if(h<=40&&h>=0&&w<=40&&w>=0){
switch(e.which) {
case 37: // left
w = w-1;
highlight('h'+h+'_w'+w);
checkCoordinates();
break;
case 38: // up
h = h-1;
highlight('h'+h+'_w'+w);
checkCoordinates();
break;
case 39: // right
w = w+1;
highlight('h'+h+'_w'+w);
checkCoordinates();
break;
case 40: // down
h = h+1;
highlight('h'+h+'_w'+w);
checkCoordinates();
break;
default: return; // exit this handler for other keys
}
console.log(h,w);
}else{
alert('you are dead');
}
e.preventDefault(); // prevent the default action (scroll / move caret)
});
function checkCoordinates(){ //Check Coordinates
socket.emit('checkCoordinates', { x: 123, y: 456 });
}