This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
199 lines (185 loc) · 4.9 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Snakes</title>
<style type="text/css">
#game{
position:absolute;
left:10%;
width:80%;
height: 80%;
top:10%;
background-color: black;
border: solid 5px white;
margin: -5px;
z-index: 1000;
}
.snake_block{
position: absolute;
height: 1%;
width:1%;
}
.arrow{
position: absolute;
}
#up{
width:80%;
height: 10%;
left:10%;
top:0;
background-size: contain;
background: url(http://imgur.com/6acMU2Q.png);
background-repeat: no-repeat;
background-position: center;
}
#down{
width:80%;
height: 10%;
left:10%;
bottom:0;
text-align: center;
background-size: contain;
background: url(http://imgur.com/2IRxJ8j.png);
background-repeat: no-repeat;
background-position: center;
}
#left{
width:10%;
height: 80%;
top:10%;
left:0;
text-align: center;
background-size: contain;
background: url(http://imgur.com/ET4B7aV.png);
background-repeat: no-repeat;
background-position: center;
}
#right{
width:10%;
height: 80%;
top:10%;
right:0;
text-align: center;
background-size: contain;
background: url(http://imgur.com/F2B7oPF.png);
background-repeat: no-repeat;
background-position: center;
}
</style>
</head>
<body>
<div class="arrow" direction="ArrowUp" id="up"></div>
<div class="arrow" direction="ArrowLeft" id="left"></div>
<div id="game"></div>
<div class="arrow" direction="ArrowRight" id="right"></div>
<div class="arrow" direction="ArrowDown" id="down"></div>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var myColor = "white";
var snake_position = [];
var direction = "static";
possibleDirections = ["ArrowDown","ArrowRight","ArrowUp","ArrowLeft"];
function updateDirection(dir){
if(direction === "static"){
direction = dir;
updateSnakeAndEmit();
}else{
direction = dir;
}
}
document.addEventListener('keydown', function(event) {
if(possibleDirections.indexOf(event.key) != -1){
updateDirection(event.key);
}
});
for(var i=0; i<4;i++){
document.getElementsByClassName("arrow")[i].addEventListener('click',function(){
updateDirection(this.getAttribute("direction"));
});
}
function drawSnake(position,color){
var game = document.getElementById("game");
var block = document.createElement("div");
block.style.left = position.x +"%";
block.style.top = position.y +"%";
block.classList.add("snake_block");
block.classList.add(color);
block.style.backgroundColor = color;
game.appendChild(block);
}
function updateSnakeAndEmit(){
var mySnake = {
"position" : snake_position,
"direction" : direction,
"color" : myColor
}
socket.emit('snake', mySnake);
}
function deleteSnake(color){
var game = document.getElementById("game");
while(document.getElementsByClassName(color).length > 0){
game.removeChild(document.getElementsByClassName(color)[0]);
}
}
function flash(color){
document.body.style.background = "white";
setTimeout(function(){
document.body.style.background = color;
setTimeout(function(){
document.body.style.background = "white";
setTimeout(function(){
document.body.style.background = myColor;
},75);
},150);
},75);
}
function initSnake(){
deleteSnake(myColor);
var start_x = Math.floor(Math.random()*100);
var start_y = Math.floor(Math.random()*100);
var first_block = {"x":start_x,"y":start_y};
snake_position=[first_block];
direction = "static";
drawSnake(first_block, myColor);
}
var host = window.location.hostname;
if(host==="localhost"){
host="http://localhost:5000/";
}
var socket = io.connect(host);
socket.on("wait",function(message){
alert("You can't play because, "+message+"\n But you can watch other and reload the page when someone leaves.");
});
socket.on("init", function(conditions){
myColor=conditions.color;
document.body.style.background = myColor;
for(player in conditions.other){
for(var i=0;i<conditions.other[player].length;i++){
drawSnake(conditions.other[player][i],player);
}
}
initSnake();
updateSnakeAndEmit();
});
// received a message from server
socket.on('myNewBlock', function(new_position) {
snake_position.push(new_position);
drawSnake(new_position,myColor);
updateSnakeAndEmit();
});
socket.on('othersnake', function(new_other_block) {
drawSnake(new_other_block.position,new_other_block.color);
});
socket.on("gameover", function(color){
initSnake();
flash(color);
});
socket.on('otherdead',function(color){
deleteSnake(color);
flash(color);
})
</script>
</body>
</html>