-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
77 lines (70 loc) · 2.01 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
<canvas width="400" height="400" style="border: 1px solid black"></canvas>
<script>
const gameMap = [
"####################",
"# #",
"# #",
"# ############## #",
"# ### ### ### #### #",
"# #",
"# ######## ####### #",
"# #",
"# ################ #",
"# #",
"# ### ### ### #### #",
"# #",
"# ################ #",
"# #",
"# ### ### ### #### #",
"# #",
"# ################ #",
"# #",
"# #",
"####################",
];
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "green";
for (let i = 0; i < gameMap.length; i++) {
for (let j = 0; j < gameMap[i].length; j++) {
if (gameMap[i][j] === "#") {
ctx.fillRect(j * 20, i * 20, 20, 20);
}
}
}
ctx.fillStyle = "blue";
ctx.fillRect(20, 20, 20, 20)
let playerX = 20
let playerY = 20
let direction = {x: 1, y: 0}
addEventListener('keydown', event => {
if (event.key === "ArrowLeft") {
direction = {x: -1, y: 0}
}
if (event.key === "ArrowRight") {
direction = {x: 1, y: 0}
}
if (event.key === "ArrowUp") {
direction = {x: 0, y: -1}
}
if (event.key === "ArrowDown") {
direction = {x: 0, y: 1}
}
})
const drawPlayer = () => {
setTimeout(() => {
if (playerX === 360 && direction.x === 1) return drawPlayer()
if (playerX === 20 && direction.x === -1) return drawPlayer()
if (playerY === 360 && direction.y === 1) return drawPlayer()
if (playerY === 20 && direction.y === -1) return drawPlayer()
ctx.fillStyle = "white"
ctx.fillRect(playerX, playerY, 20, 20)
ctx.fillStyle = "blue"
playerX += direction.x * 2
playerY += direction.y * 2
ctx.fillRect(playerX, playerY, 20, 20)
return drawPlayer()
}, 10)
}
drawPlayer()
</script>