SVG应用:贪吃蛇游戏。使用SVG,Java脚本,CSS和HTML开发了一个简单的游戏。
HTML
x
171
171
1
2
<html>
3
<head>
4
<meta name="viewport" content="width=device-width, initial-scale=1">
5
<title>SVG应用:贪吃蛇游戏 | web176教程网</title>
6
<style>
7
html, body {
8
height: 100%;
9
margin: 0;
10
padding: 0;
11
border: 0;
12
width:100%;
13
}
14
.snake {fill: #000;}
15
16
17
.apple {fill: #f00;}
18
.game-over {border-color: #fcc !important;}
19
</style>
20
</head>
21
<body >
22
23
</body>
24
</html>
25
26
<script>
27
28
var svgns = "http://www.w3.org/2000/svg",
29
svg = document.createElementNS(svgns, "svg"),
30
rectSize = 31,
31
matrixSize = 15,
32
matrixLimit = matrixSize - 1;
33
speedMs = 250;
34
svg.setAttributeNS(null, 'width', rectSize * matrixSize);
35
svg.setAttributeNS(null, 'height', rectSize * matrixSize);
36
svg.setAttributeNS(null, 'style', 'border: ' + rectSize + 'px solid #ccc;');
37
document.body.appendChild(svg);
38
var currentX = -1,
39
currentY = 0,
40
nextMoveX = 1,
41
nextMoveY = 0,
42
snakeL = 5,
43
swipe = 0,
44
rectArray = [];
45
gameIsOver = false;
46
47
function drawPoint(x, y) {
48
var rect = [document.createElementNS(svgns, 'rect'), x, y];
49
var rectObj = rect[0];
50
rectObj.setAttributeNS(null, 'x', x * rectSize);
51
rectObj.setAttributeNS(null, 'y', y * rectSize);
52
rectObj.setAttributeNS(null, 'height', rectSize);
53
rectObj.setAttributeNS(null, 'width', rectSize);
54
rectObj.setAttributeNS(null, 'class', 'snake');
55
rectArray.push(rect);
56
svg.appendChild(rectObj);
57
if (rectArray.length > snakeL) {
58
svg.removeChild(rectArray[0][0]);
59
rectArray.shift();
60
}
61
}
62
function setApple() {
63
var appleX = Math.floor((Math.random() * matrixSize)),
64
appleY = Math.floor((Math.random() * matrixSize));
65
apple = [document.createElementNS(svgns, 'rect'), appleX, appleY];
66
var thisApple = apple[0];
67
thisApple.setAttributeNS(null, 'x', appleX * rectSize);
68
thisApple.setAttributeNS(null, 'y', appleY * rectSize);
69
thisApple.setAttributeNS(null, 'height', rectSize);
70
thisApple.setAttributeNS(null, 'width', rectSize);
71
thisApple.setAttributeNS(null, 'class', 'apple');
72
svg.appendChild(thisApple);
73
}
74
function gameOver() {
75
svg.setAttributeNS(null, 'class', 'game-over');
76
clearInterval(timing);
77
alert('GAME OVER!\nYour result is ' + snakeL + '!');
78
gameIsOver = true;
79
return;
80
}
81
var timing = setInterval(controllingSnake, speedMs);
82
function controllingSnake() {
83
var nextX = currentX + nextMoveX,
84
nextY = currentY + nextMoveY;
85
rectArray.forEach(function(element){
86
if (nextX === element[1] && nextY === element[2]) {
87
gameOver();
88
};
89
});
90
if (nextY < 0 || nextY > matrixLimit || nextX < 0 || nextX > matrixLimit) {
91
gameOver();
92
}
93
if (!gameIsOver) {
94
if (currentX === apple[1] && currentY === apple[2]) {
95
snakeL++;
96
svg.removeChild(apple[0]);
97
setApple();
98
}
99
drawPoint(nextX, nextY);
100
currentX = nextX;
101
currentY = nextY;
102
}
103
}
104
function changeDirection(dirCode) {
105
switch (dirCode) {
106
case 37:
107
if (nextMoveX !== 1) {
108
nextMoveX = -1;
109
nextMoveY = 0;
110
}
111
break;
112
case 38:
113
if (nextMoveY !== 1) {
114
nextMoveX = 0;
115
nextMoveY = -1;
116
}
117
break;
118
case 39:
119
if (nextMoveX !== -1) {
120
nextMoveX = 1;
121
nextMoveY = 0;
122
}
123
break;
124
case 40:
125
if (nextMoveY !== -1) {
126
nextMoveX = 0;
127
nextMoveY = 1;
128
}
129
}
130
}
131
document.onkeydown = checkKey;
132
function checkKey(evt) {
133
var evt = evt || window.event;
134
changeDirection(evt.keyCode);
135
}
136
function is_touch_device() {
137
return (('ontouchstart' in window)
138
|| (navigator.MaxTouchPoints > 0)
139
|| (navigator.msMaxTouchPoints > 0));
140
}
141
function startup() {
142
if (is_touch_device()) {
143
tStartX = null;
144
tStartY = null;
145
document.body.addEventListener("touchstart", handleStart, false);
146
document.body.addEventListener("touchend", handleEnd, false);
147
} else {
148
console.log('Is not touch device');
149
}
150
}
151
function handleStart(evt) {
152
evt.preventDefault();
153
tStartX = evt.touches[0].screenX;
154
tStartY = evt.touches[0].screenY;
155
}
156
function handleEnd(evt) {
157
evt.preventDefault();
158
var tEndX = evt.changedTouches[0].screenX,
159
tEndY = evt.changedTouches[0].screenY,
160
totalX = tStartX - tEndX,
161
totalY = tStartY - tEndY;
162
if (Math.abs(totalX) > Math.abs(totalY)) {
163
swipe = (totalX >= 0) ? 37 : 39;
164
} else {
165
swipe = (totalY >= 0) ? 38 : 40;
166
}
167
changeDirection(swipe);
168
}
169
setApple();
170
171
</script>
阅读剩余 85%
作者:terry,如若转载,请注明出处:https://www.web176.com/svgtag/1458.html