-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
320 lines (285 loc) · 11.6 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Platform Oyunu</title>
<style>
canvas {
border: 1px solid black;
display: block;
margin: 0 auto;
}
#startScreen {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(45deg, #ff0000, #00ff00, #0000ff, #ffff00);
background-size: 400% 400%;
animation: gradient 15s ease infinite;
display: flex;
justify-content: center;
align-items: center;
}
#startButton {
padding: 15px 30px;
font-size: 24px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#guide {
position: absolute;
top: 20px;
left: 20px;
color: white;
font-size: 16px;
}
#restartButton {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
padding: 10px 20px;
font-size: 18px;
background-color: #f44336;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
display: none;
}
@keyframes gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
</style>
</head>
<body>
<div id="startScreen">
<div id="guide">
<h2>Nasıl Oynanır?</h2>
<p>Ok tuşlarıyla hareket et</p>
<p>Yukarı ok tuşuyla zıpla</p>
<p>Hedefin en yüksek puanı elde etmek</p>
</div>
<button id="startButton">Oyuna Başla</button>
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<button id="restartButton" onclick="restartGame()">Yeniden Başla</button>
<script>
var canvas = document.getElementById('gameCanvas');
var ctx = canvas.getContext('2d');
// Ses dosyalarını yükle
var jumpSound = new Audio('jump.mp3');
var fallSound = new Audio('fall.mp3');
var backgroundMusic = new Audio('background_music.mp3');
backgroundMusic.loop = true; // Arka planda müziği döngüye al
var game = {
state: 'start', // Başlangıç durumu
speedIncreaseScore: 50,
speedIncreaseAmount: 0.5,
minX: 0,
maxX: canvas.width,
minY: 0,
maxY: canvas.height,
scrollingSpeed: 5
};
var player = {
x: 50,
y: canvas.height - 50,
width: 50,
height: 50,
speed: 0,
maxSpeed: 5,
acceleration: 0.2,
jumpStrength: 10,
jumping: false,
velocityX: 0,
velocityY: 0,
gravity: 0.3,
lastPlatformY: canvas.height - 100
};
var ground = {
x: 0,
y: canvas.height - 20,
width: canvas.width,
height: 20
};
var platforms = [
{ x: 150, y: canvas.height - 100, width: 100, height: 20 },
{ x: 350, y: canvas.height - 200, width: 100, height: 20 },
{ x: 550, y: canvas.height - 300, width: 100, height: 20 },
{ x: 200, y: canvas.height - 400, width: 100, height: 20 },
{ x: 450, y: canvas.height - 500, width: 100, height: 20 },
{ x: 600, y: canvas.height - 600, width: 100, height: 20 }
];
var score = 0;
let keys = [];
document.addEventListener('keydown', function(event) {
if (event.key === 'ArrowLeft') {
player.speed = -player.maxSpeed;
} else if (event.key === 'ArrowRight') {
player.speed = player.maxSpeed;
} else if (event.key === 'ArrowUp' && !player.jumping) {
player.jumping = true;
player.velocityY = -player.jumpStrength;
// Zıplama sesini çal
jumpSound.play();
} else if (event.key === 'r' && game.state === 'start') { // 'r' tuşuna basıldığında ve oyun durumu başlangıçta ise yeniden başlat
restartGame();
game.state = 'running';
}
});
document.addEventListener('keyup', function(event) {
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
player.speed = 0;
}
});
function gameLoop() {
if (game.state === 'running') {
update();
draw();
} else if (game.state === 'start') { // Başlangıç ekranını göster
drawStartScreen();
}
requestAnimationFrame(gameLoop);
}
function update() {
if (player.speed !== 0) {
player.speed += player.acceleration * (player.speed > 0 ? -1 : 1);
player.speed = Math.max(-player.maxSpeed, Math.min(player.speed, player.maxSpeed));
}
player.x += player.speed;
player.y += player.velocityY;
player.x = Math.max(game.minX, Math.min(player.x, game.maxX - player.width));
player.y = Math.max(game.minY, Math.min(player.y, game.maxY - player.height));
player.velocityY += player.gravity;
player.jumping = true;
for (var i = 0; i < platforms.length; i++) {
var p = platforms[i];
if (player.y < p.y && player.y + player.height > p.y && player.x + player.width > p.x && player.x < p.x + p.width) {
player.jumping = false;
player.velocityY = 0;
player.y = p.y - player.height;
if (player.y < p.y && player.y + player.height > p.y - 1 && player.lastPlatformY !== p.y) {
score += 5;
player.lastPlatformY = p.y;
}
break;
}
}
if (player.y + player.height > ground.y) {
player.jumping = false;
player.velocityY = 0;
player.y = ground.y - player.height;
}
if (score % game.speedIncreaseScore === 0 && score > 0) {
player.maxSpeed += game.speedIncreaseAmount;
}
if (player.y < canvas.height) {
var lastPlatform = platforms[platforms.length - 1];
if (lastPlatform.y < canvas.height - 100) {
platforms.push({
x: Math.random() * (canvas.width - 100),
y: lastPlatform.y - 100,
width: 100,
height: 20
});
}
}
// Ekranın kaydırılması
if (player.y < canvas.height / 2) {
platforms.forEach(function(p) {
p.y += game.scrollingSpeed;
});
ground.y += game.scrollingSpeed;
player.y += game.scrollingSpeed;
}
if (player.y + player.height > canvas.height - 1 ) {
game.state = 'start'; // Oyunu başlangıç durumuna geri döndür
// Düşme sesini çal
fallSound.play();
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'green';
ctx.fillRect(ground.x, ground.y, ground.width, ground.height);
ctx.fillStyle = 'brown';
for (var i = 0; i < platforms.length; i++) {
var p = platforms[i];
ctx.fillRect(p.x, p.y, p.width, p.height);
}
ctx.fillStyle = 'blue';
ctx.fillRect(player.x, player.y, player.width, player.height);
ctx.fillStyle = 'black';
ctx.font = '24px Arial';
ctx.fillText('Score: ' + score, 20, 40);
if (player.y + player.height > canvas.height - 1) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'white';
ctx.font = '30px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over', canvas.width / 2, canvas.height / 2 - 20);
ctx.font = '20px Arial';
ctx.fillText('Yeniden Başla (r)', canvas.width / 2, canvas.height / 2 + 20);
document.getElementById('restartButton').style.display = 'block'; // Yeniden başlat düğmesini görünür yap
}
}
// Başlangıç ekranını çiz
function drawStartScreen() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.5)'; // Arkaplanı hafifçe karart
ctx.fillRect(0, 0, canvas.width, canvas.height);
var guide = document.getElementById('guide');
guide.style.display = 'block'; // Kılavuzu göster
// Oyuna başlama düğmesi ve ekranı ortala
var startButton = document.getElementById('startButton');
startButton.style.display = 'block';
startButton.style.left = (canvas.width - startButton.offsetWidth) / 2 + 'px';
startButton.style.top = (canvas.height - startButton.offsetHeight) / 2 + 'px';
}
function restartGame() {
player.x = 50;
player.y = canvas.height - 50;
player.speed = 0;
player.maxSpeed = 5;
player.velocityX = 0;
player.velocityY = 0;
player.lastPlatformY = canvas.height - 100;
platforms = [
{ x: 150, y: canvas.height - 100, width: 100, height: 20 },
{ x: 350, y: canvas.height - 200, width: 100, height: 20 },
{ x: 550, y: canvas.height - 300, width: 100, height: 20 },
{ x: 200, y: canvas.height - 400, width: 100, height: 20 },
{ x: 450, y: canvas.height - 500, width: 100, height: 20 },
{ x: 600, y: canvas.height - 600, width: 100, height: 20 }
];
score = 0;
player.jumping = false;
document.getElementById('restartButton').style.display = 'none'; // Yeniden başlat düğmesini gizle
}
// Başlangıç ekranı görünümü ve başlangıç düğmesi olayı
var startScreen = document.getElementById('startScreen');
var startButton = document.getElementById('startButton');
startButton.addEventListener('click', function() {
startScreen.style.display = 'none'; // Başlangıç ekranını gizle
game.state = 'running'; // Oyun durumunu çalışıyor olarak ayarla
backgroundMusic.play(); // Arka planda müziği çal
gameLoop(); // Oyun döngüsünü başlat
});
</script>
</body>
</html>