-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
132 lines (109 loc) · 2.66 KB
/
snake.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
<canvas id="gc" width="400" height="400"></canvas>
<script>
window.onload=function(){
t = Math.floor(Math.random()*titles.length);
canv=document.getElementById("gc");
ctx=canv.getContext("2d");
document.addEventListener("keydown",keyPush);
startmenu = setInterval(menu,1000);
}
waiting = true;
ingame = false;
tframes=[
new Image(), new Image()
];
tframes[0].src="res/img/title1.png";
tframes[1].src="res/img/title2.png";
tf = 0;
titles=[
"ULTIMATE SNAKE: CITY OF EVIL",
"SNAKE: THE UNTOLD STORY",
"SNAKE II: RETURN TO SKYRIM",
"SNAKE III: OUROBOROS FILES",
"No step on snek EXTREM"
];
high_score = score = 0;
xv=yv=zv= 0;
px=py=pz= 10;
gs=tc= 20;
ax=ay=az= 15;
trail=[];
tail=5;
function gen_a(){
ax=Math.floor(Math.random()*tc);
ay=Math.floor(Math.random()*tc);
}
function menu(){
if(!ingame){
tf = tf == 0 ? 1 : 0;
ctx.drawImage(tframes[tf], 0, 0, 400, 400, 0, 0, 400, 400);
}
}
function start_game(){
ingame = true;
clearInterval(startmenu);
setInterval(game,1000/15);
}
function game(){
px += xv;
py += yv;
if(px<0) px=tc-1;
if(py<0) py=tc-1;
if(px>tc-1) px=0;
if(py>tc-1) py=0;
ctx.fillStyle="black";
ctx.fillRect(0,0,canv.width,canv.height);
picked = false;
for(var i=0; i<trail.length; i++){
r = Math.floor(Math.pow(Math.sin(i/80)-0.7,3)*255);
g = Math.floor(Math.pow(Math.cos(i/25),2)*255);
b = Math.floor(Math.pow(Math.sin(i/20)+0.5,2)*255);
ctx.fillStyle='rgb('+r+', '+g+', ' +b+')';
ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs,gs);
if(trail[i].x==px && trail[i].y==py) lose();
if(trail[i].x==ax && trail[i].y==ay) gen_a();
}
trail.push({x:px,y:py});
while(trail.length > tail) trail.shift();
if(ax==px && ay==py) {
tail++;
score++;
for(var i=0; i<trail.length; i++){
if(trail[i].x==ax && trail[i].y==ay){
gen_a();
}
}
}
ctx.fillStyle="red";
ctx.fillRect(ax*gs,ay*gs,gs,gs);
if(score > high_score){
high_score = score;
}
ctx.font="16px Courier";
ctx.fillStyle="white";
ctx.fillText("High Score: "+ high_score,10,20);
ctx.fillText("Score: "+ score,10,30);
}
function keyPush(evt){
switch(evt.keyCode){
//180s invalid!!!!
case 13:
waiting=false;
if(!ingame) start_game();
break;
case 37:
if (xv != 1 && !picked){xv=-1;yv= 0;picked=true;} break;
case 38:
if (yv != 1 && !picked){xv= 0;yv=-1;picked=true;} break;
case 39:
if (xv != -1 && !picked){xv= 1;yv= 0;picked=true;} break;
case 40:
if (yv != -1 && !picked){xv= 0;yv= 1;picked=true;} break;
}
}
function lose(){ //high scores saved
tail = 5;
score = 0;
menu();
}
</script>