-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (157 loc) · 5.48 KB
/
Copy pathscript.js
File metadata and controls
192 lines (157 loc) · 5.48 KB
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
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const scoreElement = document.getElementById("score");
const highScoreElement = document.getElementById("highScore");
const restartBtn = document.getElementById("restartBtn");
// متغيرات اللعبة الأساسية
let score = 0;
// استرجاع أعلى نتيجة محفوظة سابقاً في المتصفح، أو البدء بـ 0 لو كانت أول مرة
let highScore = localStorage.getItem("carGameHighScore") || 0;
highScoreElement.textContent = highScore;
let gameOver = false;
let roadOffset = 0;
let enemies = [];
let enemySpeed = 4;
let spawnTimer = 0;
// سيارة اللاعب
const player = {
x: canvas.width / 2 - 20,
y: canvas.height - 100,
width: 40,
height: 70,
speed: 6,
dx: 0,
color: "#ef4444" // أحمر
};
// متابعة أسهم الكيبورد
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowLeft" || e.key === "a") player.dx = -player.speed;
if (e.key === "ArrowRight" || e.key === "d") player.dx = player.speed;
});
document.addEventListener("keyup", (e) => {
if (e.key === "ArrowLeft" || e.key === "a" || e.key === "ArrowRight" || e.key === "d") {
player.dx = 0;
}
});
restartBtn.addEventListener("click", resetGame);
// رسم سيارة
function drawCar(x, y, width, height, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
ctx.fillStyle = "#1e293b";
ctx.fillRect(x + 5, y + 15, width - 10, 12);
ctx.fillStyle = "#000000";
ctx.fillRect(x - 4, y + 8, 4, 15);
ctx.fillRect(x + width, y + 8, 4, 15);
ctx.fillRect(x - 4, y + height - 23, 4, 15);
ctx.fillRect(x + width, y + height - 23, 4, 15);
}
// رسم خطوط الطريق المتنقلة
function drawRoad() {
ctx.strokeStyle = "#ffffff";
ctx.lineWidth = 6;
ctx.setLineDash([30, 20]);
ctx.lineDashOffset = -roadOffset;
ctx.beginPath();
ctx.moveTo(canvas.width / 2, 0);
ctx.lineTo(canvas.width / 2, canvas.height);
ctx.stroke();
ctx.setLineDash([]);
ctx.lineWidth = 8;
ctx.strokeStyle = "#f59e0b";
ctx.strokeRect(0, 0, canvas.width, canvas.height);
}
// توليد سيارات الأعداء
function spawnEnemy() {
spawnTimer++;
if (spawnTimer % 60 === 0) {
const lanes = [20, 105, 190, 275];
const randomLane = lanes[Math.floor(Math.random() * lanes.length)];
const colors = ["#3b82f6", "#10b981", "#8b5cf6", "#f59e0b"];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
enemies.push({
x: randomLane,
y: -80,
width: 40,
height: 70,
color: randomColor
});
}
}
// كشف التصادم
function checkCollision(rect1, rect2) {
return (
rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y
);
}
// الدالة الرئيسية للتحديث والرسم (Game Loop)
function update() {
if (gameOver) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roadOffset += enemySpeed;
player.x += player.dx;
if (player.x < 10) player.x = 10;
if (player.x + player.width > canvas.width - 10) player.x = canvas.width - player.width - 10;
drawRoad();
drawCar(player.x, player.y, player.width, player.height, player.color);
spawnEnemy();
for (let i = 0; i < enemies.length; i++) {
enemies[i].y += enemySpeed;
drawCar(enemies[i].x, enemies[i].y, enemies[i].width, enemies[i].height, enemies[i].color);
if (checkCollision(player, enemies[i])) {
endGame();
}
if (enemies[i].y > canvas.height) {
enemies.splice(i, 1);
score += 10;
scoreElement.textContent = score;
// تحديث أعلى نتيجة فوراً أثناء اللعب إذا تجاوزتها
if (score > highScore) {
highScore = score;
highScoreElement.textContent = highScore;
localStorage.setItem("carGameHighScore", highScore); // حفظ في المتصفح
}
if (score % 50 === 0) {
enemySpeed += 0.5;
}
i--;
}
}
requestAnimationFrame(update);
}
function endGame() {
gameOver = true;
// التأكد من حفظ أعلى نتيجة عند نهاية اللعبة
if (score > highScore) {
highScore = score;
highScoreElement.textContent = highScore;
localStorage.setItem("carGameHighScore", highScore);
}
ctx.fillStyle = "rgba(0, 0, 0, 0.75)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#ef4444";
ctx.font = "30px Arial";
ctx.textAlign = "center";
ctx.fillText("انتهت اللعبة! 💥", canvas.width / 2, canvas.height / 2 - 40);
ctx.fillStyle = "#ffffff";
ctx.font = "20px Arial";
ctx.fillText(`النقاط: ${score}`, canvas.width / 2, canvas.height / 2);
ctx.fillStyle = "#f59e0b";
ctx.fillText(`👑 أعلى نتيجة: ${highScore}`, canvas.width / 2, canvas.height / 2 + 35);
restartBtn.style.display = "block";
}
function resetGame() {
score = 0;
enemySpeed = 4;
enemies = [];
gameOver = false;
player.x = canvas.width / 2 - 20;
scoreElement.textContent = score;
restartBtn.style.display = "none";
update();
}
// بدء اللعبة
update();