-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
115 lines (114 loc) · 2.96 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪吃蛇</title>
<script src="./tools.js"></script>
<script src="./food.js"></script>
<script src="./snake.js"></script>
<script src="./game.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #662244;
}
#map {
width: 94vw;
height: 80vh;
max-width: 400px;
max-height: 600px;
background-color: rgb(2, 38, 44);
position: relative;
margin: 0 auto;
border: 10px solid #663300;
box-shadow: 4px 4px 4px #000 inset;
}
.btn {
max-width: 400px;
width: 100vw;
height: 14vh;
background-color: #662244;
display: flex;
margin: 0 auto;
}
.title {
width: 50%;
text-align: center;
font-size: 0.9rem;
}
.title h1 {
font-size: 1.5rem;
margin-bottom: 5%;
}
.container {
width: 50%;
height: 100px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.middle {
display: flex;
width: 100px;
justify-content: space-between;
}
.container button {
width: 30px;
height: 30px;
font-size: 1.2rem;
}
</style>
</head>
<body>
<div id="map">
</div>
<div class="btn">
<div class="title">
<h1>操作面板</h1>
<p>键盘:上下左右键控制</p>
</div>
<div class="container">
<div class="top"><button id="up">⬆</button></div>
<div class="middle"><button id="left">⬅</button><button id="right">➡</button></div>
<div class="bottom"><button id="down">⬇</button></div>
</div>
</div>
<script>
let map = document.getElementById('map')
let newGame = new Game(map);
newGame.start();
document.addEventListener("keydown",keydown);
document.addEventListener("keyup",keyup);
let up = document.getElementById('up');
let left = document.getElementById('left');
let right = document.getElementById('right');
let down = document.getElementById('down');
up.addEventListener("click", ()=>newGame.control(38));
right.addEventListener("click", ()=>newGame.control(39));
down.addEventListener("click", ()=>newGame.control(40));
left.addEventListener("click", ()=>newGame.control(37));
let flag = true; // 节流阀
function keydown(event){
if(flag){
flag =false;
if(event.keyCode == '38' || event.keyCode == '40'){ // 阻止默认事件
if(event.preventDefault){
event.preventDefault();
}else{
event.returnValue = false;
}
}
newGame.control(event.keyCode);
}
}
function keyup(){
flag = true;
}
</script>
</body>
</html>