-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (157 loc) · 4.33 KB
/
index.js
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
const papanGame = document.querySelector("#papanGame");
const ctx = papanGame.getContext("2d");
const scoreText = document.querySelector("#scoreText");
const resetBtn = document.querySelector("#resetBtn");
const gameWidth = papanGame.width;
const gameHeight = papanGame.height;
const backgroundPapan = "white";
const warnaUlar = "lightgreen";
const borderUlar = "black";
const warnaMakanan = "red";
const ukuranUnit = 25;
let running= false;
let xVelocity = ukuranUnit;
let yVelocity = 0;
let makananX;
let makananY;
let score = 0;
let Ular = [
{x:ukuranUnit * 4, y:0},
{x:ukuranUnit * 3, y:0},
{x:ukuranUnit * 2, y:0},
{x:ukuranUnit, y:0},
{x:0, y:0}
];
window.addEventListener("keydown", changeDirection);
resetBtn.addEventListener("click", resetGame);
gameStart();
function gameStart(){
running = true;
scoreText.textContent = score;
buatMakanan();
gambarMakanan();
nextTick();
};
function nextTick(){
if(running){
setTimeout(()=>{
clearPapan();
gambarMakanan();
gerakanUlar();
gambarUlar();
checkGameOver();
nextTick();
}, 75);
}
else{
displayGameOver();
}
};
function clearPapan(){
ctx.fillStyle = backgroundPapan;
ctx.fillRect(0, 0, gameWidth, gameHeight);
};
function checkGameOver(){
switch(true){
case(Ular[0].x < 0):
running = false;
break;
case(Ular[0].x >= gameWidth):
running = false;
break;
case(Ular[0].y < 0):
running = false;
break;
case(Ular[0].y >= gameHeight):
running = false;
break;
}
for(let i = 1; i < Ular.length; i+=1){
if(Ular[i].x == Ular[0].x && Ular[i].y == Ular[0].y){
running = false;
}
}
};
function buatMakanan(){
function randomMakanan(min, max){
const randNum = Math.round((Math.random() * (max - min) + min) / ukuranUnit) * ukuranUnit;
return randNum;
}
makananX = randomMakanan(0, gameWidth - ukuranUnit);
makananY = randomMakanan(0, gameWidth - ukuranUnit);
};
function gerakanUlar(){
const head = {x: Ular[0].x + xVelocity,
y: Ular[0].y + yVelocity};
Ular.unshift(head);
if(Ular[0].x == makananX && Ular[0].y == makananY){
score+=1;
scoreText.textContent = score;
buatMakanan();
}
else{
Ular.pop();
}
};
function gambarMakanan(){
ctx.fillStyle = warnaMakanan;
ctx.fillRect(makananX, makananY, ukuranUnit, ukuranUnit);
};
function gambarUlar(){
ctx.fillStyle = warnaUlar;
ctx.strokeStyle = borderUlar;
Ular.forEach(bagianUlar => {
ctx.fillRect(bagianUlar.x, bagianUlar.y, ukuranUnit, ukuranUnit);
ctx.strokeRect(bagianUlar.x, bagianUlar.y, ukuranUnit, ukuranUnit);
})
};
function changeDirection(event){
const keyPressed = event.keyCode;
console.log(keyPressed);
const KIRI = 37;
const KANAN = 39;
const ATAS = 38;
const BAWAH = 40;
const menujuAtas = (yVelocity == -ukuranUnit);
const menujuBawah = (yVelocity == ukuranUnit);
const menujuKanan = (xVelocity == ukuranUnit);
const menujuKiri = (xVelocity == -ukuranUnit);
switch(true){
case(keyPressed == KIRI && !menujuKanan):
xVelocity = -ukuranUnit;
yVelocity = 0;
break;
case(keyPressed == ATAS && !menujuBawah):
xVelocity = 0;
yVelocity = -ukuranUnit;
break;
case(keyPressed == KANAN && !menujuKiri):
xVelocity = ukuranUnit;
yVelocity = 0;
break;
case(keyPressed == BAWAH && !menujuAtas):
xVelocity = 0;
yVelocity = ukuranUnit;
break;
}
};
function displayGameOver(){
ctx.font = "50px MV Boli";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx.fillText = "GAME OVER:(", gameWidth / 2, gameHeight / 2;
running = false;
};
function resetGame(){
score = 0;
xVelocity = ukuranUnit;
yVelocity = 0;
Ular = [
{x:ukuranUnit * 4, y:0},
{x:ukuranUnit * 3, y:0},
{x:ukuranUnit * 2, y:0},
{x:ukuranUnit, y:0},
{x:0, y:0}
];
gameStart();
};