-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
87 lines (82 loc) · 2.93 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div id='world'>
</div>
<script>
//若第二行为 (、[、+、-或 / 开头时,JavaScript不会自动添加分号
var map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1],
[1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1],
[1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1],
[1, 2, 2, 2, 1, 1, 4, 1, 1, 2, 2, 2, 1],
[1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1],
[1, 2, 1, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1],
[1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
var pacmac = {
r: 4,
l: 6,
dir:'right'
}
var world = document.querySelector('#world')
draw = () => {
var html = ''
for (k in map) {
for (j in map[k]) {
if (map[k][j] === 1) html += `<div class='wall'></div>`
else if (map[k][j] === 2) html += `<div class='coin'></div>`
else if (map[k][j] === 3) html += `<div class='ground'></div>`
else if (map[k][j] === 4) html += '<div class="pacmac '+pacmac.dir+'"></div>'
}
html += '<br>'
}
world.innerHTML = html
}
draw();
document.addEventListener('keydown', (e) => {
// console.log(e.key);
console.log(e.code);
if (e.code === 'ArrowRight') {
pacmac.dir='right'
if (map[pacmac.r][pacmac.l + 1] !== 1) {
map[pacmac.r][pacmac.l] = 3
pacmac.l += 1
map[pacmac.r][pacmac.l] = 4
}
} else if (e.code === 'ArrowLeft') {
pacmac.dir='left'
if (map[pacmac.r][pacmac.l - 1] !== 1) {
map[pacmac.r][pacmac.l] = 3
pacmac.l -= 1
map[pacmac.r][pacmac.l] = 4
}
} else if (e.code === 'ArrowUp') {
pacmac.dir='up'
if (map[pacmac.r - 1][pacmac.l] !== 1) {
map[pacmac.r][pacmac.l] = 3
pacmac.r -= 1
map[pacmac.r][pacmac.l] = 4
}
} else if (e.code === 'ArrowDown') {
pacmac.dir='down'
if (map[pacmac.r + 1][pacmac.l] !== 1) {
map[pacmac.r][pacmac.l] = 3
pacmac.r += 1
map[pacmac.r][pacmac.l] = 4
}
}
draw();
})
</script>
</body>
</html>