-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathular.php
263 lines (230 loc) · 7.57 KB
/
ular.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
session_start();
// Inisialisasi atau ambil high score
if (!isset($_SESSION['highscore'])) {
$_SESSION['highscore'] = 0;
}
// Update high score via AJAX
if (isset($_POST['newScore'])) {
$newScore = intval($_POST['newScore']);
if ($newScore > $_SESSION['highscore']) {
$_SESSION['highscore'] = $newScore;
echo $_SESSION['highscore'];
exit;
}
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Snake Game</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(45deg, #1a1a1a, #4a4a4a);
font-family: 'Press Start 2P', cursive;
color: #fff;
}
.game-container {
text-align: center;
}
#gameCanvas {
border: 3px solid #00ff00;
background-color: #000;
box-shadow: 0 0 20px #00ff00;
}
.score-board {
display: flex;
justify-content: space-between;
margin: 20px 0;
padding: 10px;
background: rgba(0, 255, 0, 0.1);
border: 2px solid #00ff00;
}
.controls {
margin-top: 20px;
color: #00ff00;
}
.game-over {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.9);
padding: 20px;
border: 2px solid #00ff00;
text-align: center;
}
button {
background: #00ff00;
color: #000;
border: none;
padding: 10px 20px;
margin: 10px;
cursor: pointer;
font-family: 'Press Start 2P', cursive;
}
button:hover {
background: #00cc00;
}
</style>
</head>
<body>
<div class="game-container">
<div class="score-board">
<div>Score: <span id="score">0</span></div>
<div>High Score: <span id="highScore"><?php echo $_SESSION['highscore']; ?></span></div>
</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="controls">
<p>Use Arrow Keys to Move</p>
<button onclick="resetGame()">New Game</button>
</div>
<div id="gameOver" class="game-over">
<h2>Game Over!</h2>
<p>Final Score: <span id="finalScore">0</span></p>
<button onclick="resetGame()">Play Again</button>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [];
let food = {};
let dx = gridSize;
let dy = 0;
let score = 0;
let gameSpeed = 150;
let gameLoop;
function initGame() {
snake = [
{x: 5 * gridSize, y: 5 * gridSize}
];
createFood();
score = 0;
document.getElementById('score').textContent = score;
document.getElementById('gameOver').style.display = 'none';
}
function createFood() {
food = {
x: Math.floor(Math.random() * tileCount) * gridSize,
y: Math.floor(Math.random() * tileCount) * gridSize
};
// Pastikan makanan tidak muncul di tubuh ular
snake.forEach(segment => {
if (segment.x === food.x && segment.y === food.y) {
createFood();
}
});
}
function drawGame() {
// Bersihkan canvas
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Gambar makanan
ctx.fillStyle = '#ff0000';
ctx.fillRect(food.x, food.y, gridSize - 2, gridSize - 2);
// Gambar ular
snake.forEach((segment, index) => {
ctx.fillStyle = index === 0 ? '#00ff00' : '#008800';
ctx.fillRect(segment.x, segment.y, gridSize - 2, gridSize - 2);
});
}
function moveSnake() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
// Cek tabrakan dengan dinding
if (head.x < 0 || head.x >= canvas.width ||
head.y < 0 || head.y >= canvas.height) {
gameOver();
return;
}
// Cek tabrakan dengan tubuh sendiri
for (let i = 0; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
return;
}
}
snake.unshift(head);
// Cek makan
if (head.x === food.x && head.y === food.y) {
score += 10;
document.getElementById('score').textContent = score;
createFood();
// Tingkatkan kecepatan
if (score % 50 === 0 && gameSpeed > 50) {
gameSpeed -= 10;
clearInterval(gameLoop);
gameLoop = setInterval(moveSnake, gameSpeed);
}
} else {
snake.pop();
}
drawGame();
}
function gameOver() {
clearInterval(gameLoop);
document.getElementById('gameOver').style.display = 'block';
document.getElementById('finalScore').textContent = score;
// Update high score
fetch('', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'newScore=' + score
})
.then(response => response.text())
.then(newHighScore => {
document.getElementById('highScore').textContent = newHighScore;
});
}
function resetGame() {
clearInterval(gameLoop);
initGame();
gameSpeed = 150;
dx = gridSize;
dy = 0;
gameLoop = setInterval(moveSnake, gameSpeed);
}
document.addEventListener('keydown', (event) => {
switch(event.key) {
case 'ArrowUp':
if (dy === 0) {
dx = 0;
dy = -gridSize;
}
break;
case 'ArrowDown':
if (dy === 0) {
dx = 0;
dy = gridSize;
}
break;
case 'ArrowLeft':
if (dx === 0) {
dx = -gridSize;
dy = 0;
}
break;
case 'ArrowRight':
if (dx === 0) {
dx = gridSize;
dy = 0;
}
break;
}
});
// Mulai game
resetGame();
</script>
</body>
</html>