-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.htm
132 lines (122 loc) · 3.39 KB
/
game.htm
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<style>
canvas { display: block; margin: 0 auto; }
</style>
<script type="text/javascript" src="modernizr.js"></script>
<script>
var canvas;
var ctx;
var cW, cH; // canvas width, height
var MainScreen = {
draw: function () {
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, cW, cH);
ctx.fill();
}
};
var Keys = {
up: 38,
down: 40,
left: 37,
right: 39
};
function KeyDown(aEvent) {
switch(aEvent.keyCode) {
case Keys.up:
Snake.moveUp();
break;
case Keys.down:
Snake.moveDown();
break;
case Keys.left:
Snake.moveLeft();
break;
case Keys.right:
Snake.moveRight();
break;
}
};
var Snake = {
x: 0,
y: 0,
vectSpeed: 20, // Vector speed
snakeX: 10,
snakeY: 10,
moveUp: function () {
if (this.y > 0) {
this.y -= this.vectSpeed;
}
},
moveDown: function () {
if (this.y < cH - this.snakeY) {
this.y += this.vectSpeed;
}
},
moveLeft: function () {
if (this.x > 0) {
this.x -= this.vectSpeed;
}
},
moveRight: function () {
if (this.x < cW - this.snakeX) {
this.x += this.vectSpeed;
}
},
draw: function () {
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(this.x, this.y, this.snakeX, this.snakeY);
ctx.fill();
}
};
var Food = {
images: ["images/image1.png", 'images/image2.png', 'images/image3.png', 'images/image4.png',
'images/image5.png', 'images/image6.png', 'images/image7.png', 'images/image8.png'],
randImg: 0,
lastDrawTime: 0,
x: 0,
y: 0,
draw: function () {
var currentDrawTime = new Date().getTime();
if (currentDrawTime - this.lastDrawTime > 7000) {
this.randImg = Math.floor(Math.random() * this.images.length);
this.x = Math.floor(Math.random() * cW);
this.y = Math.floor(Math.random() * cH);
this.lastDrawTime = new Date().getTime();
}
var img = new Image();
img.src = this.images[this.randImg];
ctx.drawImage(img, this.x, this.y);
}
};
function mainDraw() {
requestAnimationFrame(mainDraw);
gameElements.forEach(function(element) {
element.draw();
});
}
var gameElements = [
MainScreen,
Snake,
Food
];
function init() {
if (!Modernizr.canvas) {
document.write("Unfortunately your current browser doesn't support HTML Canvas, either grab the latest Chrome or Firefox");
} else {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
cW = canvas.width;
cH = canvas.height;
window.onkeydown = KeyDown;
requestAnimationFrame(mainDraw);
}
};
</script>
</head>
<body onload="init();">
<canvas id="canvas" width="410" height="350" style="border:10px solid #587CDC;"></canvas>
</body>
</html>