-
Notifications
You must be signed in to change notification settings - Fork 1
/
snake.html
85 lines (74 loc) · 1.92 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
<!DOCTYPEHTML>
<html>
<head>
<title>Snake</title>
<meta charset="utf-8">
</head>
<style type="text/css"></style>
<body>
<divid="wrapper">
<h1>Snake</h1>
<div id="score">Score:0 Level:1</div>
<canvas width="300"height="400"id="canvas">
</canvas>
<div id="control">Controls: W = Up; A = Left; S = Down; D = Right</div>
</div>
<script type="text/javascript">
var context;
var width = 300;
var height = 400;
var snakeLength = 3;
var level = 1;
var sqSize = 10;
var bodyX = new Array(150, 150-sqSize, 150-2*sqSize);
var bodyY = new Array(200, 200, 200);
var vX = new Array(1, 1, 1);
var vY = new Array(0, 0, 0);
var rX;
var rY;
var score = 0;
var scoreDiv;
var eaten = true;
var gameOver = false;
var controlsDiv;
function init()
{
// Get game context
context = document.getElementById("canvas").getContext("2d");
//draws the canvas
drawCanvasBoundary();
//draws snake
drawSnake();
}
// insert the listener for onload event to call our init function
window.addEventListener("load", init, true);
function drawCanvasBoundary()
{
//set canvas color to be white
context.fillStyle="#FFF";
//draws a rectangle of canvas size filled with white color.
//This serves as our background
context.fillRect(0,0,width,height);
context.fill();
/*Draw black boundary if working in white background*/
context.strokeStyle="#000";
context.strokeRect(0,0,width,height)
}
function drawPoint(x,y)
{
// First draw a square for size "sqSize" filled with black
context.fillStyle = "#000";
context.fillRect(x,y,sqSize, sqSize);
context.fill();
// Then draw the square boundary in white
context.strokeStyle="#FFFFFF";
context.strokeRect(x,y,sqSize, sqSize);
}
function drawSnake()
{
for(var i=0; i < snakeLength; i++)
drawPoint(bodyX[i],bodyY[i]);
}
</script>
</body>
</html>