This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsnake.js
176 lines (151 loc) Β· 5.17 KB
/
jsnake.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
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
/*
This file is part of JSnake.
JSnake is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
JSnake is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JSnake.
If not, see <http://www.gnu.org/licenses/>.
*/
// Vardeklaration
var game, context;
var xSpeed, ySpeed;
var xPlayer, yPlayer;
var xMap, yMap;
var xFood, yFood;
var path = [], tail;
var points, foodColors, bodyColor;
var currentIndex, temporaryValue, randomIndex;
window.onload = function start() {
game = document.getElementById( "game" );
context = game.getContext( "2d" );
document.addEventListener( "keydown", keyPushed );
setInterval( gameloop, 100);
xSpeed = ySpeed = 0;
xPlayer = yPlayer = 10;
xMap = yMap = 20;
xFood = Math.floor( Math.random() * xMap );
yFood = Math.floor( Math.random() * yMap );
tail = 5;
points = 0;
foodColors = ['red', 'orange', 'yellow', 'aqua', 'darksalmon', 'grey'];
bodyColor = "lime";
shuffle(foodColors);
readCookie();
}
function gameloop() {
xPlayer += xSpeed;
yPlayer += ySpeed;
if ( xPlayer < 0 )
xPlayer = xMap -1;
if ( xPlayer > xMap -1)
xPlayer = 0;
if ( yPlayer < 0 )
yPlayer = yMap -1;
if ( yPlayer > yMap -1)
yPlayer = 0;
// Background
context.fillStyle = "black";
context.fillRect( 0, 0, game.width, game.height );
// Draw player
var j = 0;
for ( var i = 0 ; i < path.length ; i++ ) {
// snake head
if ( i == path.length - 1 )
context.fillStyle = "white";
// snake body
else if ( i == 0 )
context.fillStyle = bodyColor;
context.fillRect( path[i].x * xMap, path[i].y * yMap, xMap - 2, yMap - 2 );
// eat itself
if ( path[i].x == xPlayer && path[i].y == yPlayer && tail > 5 ) {
createCookie();
window.location.href = "index.html";
}
}
path.push( { x : xPlayer, y : yPlayer } );
while ( path.length > tail ) {
path.shift();
}
// eat food
if ( xFood == xPlayer && yFood == yPlayer ) {
tail++;
points++;
shuffle(foodColors);
document.getElementById('points').innerHTML = points;
xFood = Math.floor( Math.random() * xMap );
yFood = Math.floor( Math.random() * yMap );
}
// Draw food
context.fillStyle = foodColors[0];
context.fillRect( xFood * xMap, yFood * yMap, xMap - 2, yMap - 2 );
}
function keyPushed(key) {
switch ( key.keyCode ) {
case 27: // esc
xSpeed = 0;
ySpeed = 0;
break;
case 37: // arrowkey left
xSpeed = -1;
ySpeed = 0;
break;
case 38: // arrowkey top
xSpeed = 0;
ySpeed = -1;
break;
case 39: // arrowkey rights
xSpeed = 1;
ySpeed = 0;
break;
case 40: // arrowkey bottom
xSpeed = 0;
ySpeed = 1;
break;
}
}
function shuffle(array) {
currentIndex = array.length;
while ( 0 !== currentIndex ) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function setBodyColor( box ) {
bodyColor = box.options[ box.selectedIndex ].value;
}
function createCookie() {
var expires = "";
var days = 7;
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
if ( points > document.getElementById('highScore').innerHTML )
document.cookie = "coockieHighScore=" +
points + expires + "; path=/";
else
return;
}
function readCookie() {
var nameEQ = "coockieHighScore=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while ( c.charAt(0)==' ' ) {
c = c.substring( 1, c.length );
}
if ( c.indexOf(nameEQ) == 0 ) {
text = c.substring( nameEQ.length,c.length );
}
}
document.getElementById('highScore').innerHTML = text;
return null;
}