-
Notifications
You must be signed in to change notification settings - Fork 39
/
snake-js.html
225 lines (209 loc) · 3.88 KB
/
snake-js.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SNAKE GAME JS</title>
<style>
*{
margin:0;
padding: 0;
}
.case{
width: 100%;
height:100vh;
background: black;
text-align: center;
}
#gc{
}
.control{
margin-top:15px;
}
.btn{
width: 70px;
height: 70px;
text-align:center;
}
.atas{
display: block;
margin:auto;
}
.bawah{
display: block;
margin:auto;
}
p{
color:white;
}
</style>
</head>
<body>
<div class="case">
<p>Score: <span class="score">0</span> | Highscore: <span class="highscore">0</span></p>
<canvas id="gc" style="border:1px solid blue;"></canvas>
<div class="control">
<button class="btn atas" onclick="atas()">^</button>
<button class="btn kiri" onclick="kiri()"><</button>
<button class="btn">P</button>
<button class="btn kanan" onclick="kanan()">></button>
<button class="btn bawah" onclick="bawah()">v</button>
</div>
</div>
<script>
const canvas = document.getElementById("gc")
const ctx = canvas.getContext('2d')
const score = document.getElementsByClassName("score")[0]
const highscore = document.getElementsByClassName("highscore")[0]
document.addEventListener('keydown',control)
interval = setInterval(game,1000/10)
const tombol = document.getElementsByClassName("control")[0]
if(window.innerWidth>800){
tombol.style.display = "none"
canvas.width = 600
petak=30
}else{
canvas.width = 300
petak = 20
}
canvas.height = canvas.width
canvasW = canvas.width
ujung=canvasW/petak
ularX=ularY=10
arahX=arahY=0
apelX=apelY=Math.floor(Math.random()*ujung)
jejak=[]
ekor = 2
play=false
count = 0
high = 0
function kiri(){
if (arahX != 1) {
arahX = -1
arahY = 0
}
}
function atas(){
if(arahY != 1){
arahY = -1
arahX = 0
}
}
function kanan(){
if (arahX != -1) {
arahX = 1
arahY = 0
}
}
function bawah(){
if (arahY != -1){
arahY = 1
arahX = 0
}
}
function control(event){
switch(event.keyCode){
// kiri
case 37:
kiri()
break;
// atas
case 38:
atas()
break;
// kanan
case 39:
kanan()
break;
// bawah
case 40:
bawah()
break;
}
}
function game(){
ularX += arahX
ularY += arahY
// melebihi batas
if(ularX<0){
ularX = ujung-1
}
if (ularX>ujung-1) {
ularX = 0
}
if (ularY<0) {
ularY = ujung-1
}
if (ularY>ujung-1) {
ularY = 0
}
// gambar background
bg = []
for(var i=0;i<ujung;i++){
var row = []
for(var j=0;j<ujung;j++){
let background = (i%2 == 0 && j%2==1) || (i%2 == 1 && j%2==0)? '#1c4e6b' : '#133954'
row.push({
background,
x: j * petak,
y: i * petak
});
}
bg.push(row)
}
bg.forEach((row, rowIndex) => {
row.forEach((col,colIndex) => {
ctx.fillStyle = col.background;
ctx.fillRect(col.x,col.y,petak,petak);
})
})
// gambar ular
for(var i=0;i<jejak.length;i++){
ctx.beginPath()
ctx.fillStyle="yellow"
ctx.strokeStyle="white"
ctx.rect(jejak[i].x*petak,jejak[i].y*petak,petak,petak)
ctx.fill()
ctx.stroke()
ctx.closePath()
// mati
if(play==true && jejak[i].x==ularX && jejak[i].y==ularY){
play=false
ularX=ularY=10
arahX=arahY=0
apelX=apelY=Math.floor(Math.random()*ujung)
jejak=[]
ekor = 2
alert(`Your score: ${count}`)
if(count>high){
high = count
}
count = 0
}
}
if(ekor==3){
play = true
}
jejak.push({x:ularX,y:ularY})
while(jejak.length>ekor){
jejak.shift()
}
// makanan
if (ularX==apelX && ularY==apelY) {
ekor++
count++
apelX = Math.floor(Math.random()*ujung)
apelY = Math.floor(Math.random()*ujung)
}
ctx.beginPath()
ctx.fillStyle = "red"
ctx.strokeStyle = "black"
ctx.rect(apelX*petak,apelY*petak,petak,petak)
ctx.fill()
ctx.stroke()
ctx.closePath()
score.innerHTML = count
highscore.innerHTML = high
}
</script>
</body>
</html>