-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (160 loc) · 4.38 KB
/
Copy pathscript.js
File metadata and controls
174 lines (160 loc) · 4.38 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
const maze = document.getElementById('maze');
const status = document.getElementById('status');
const timerDisplay = document.getElementById('timer');
const scoreDisplay = document.getElementById('score');
const levelInfo = document.getElementById('level-info');
let player, goal, walls = [];
let level = 1;
let score = 0;
let timeLeft = 30;
let timerInterval;
// Levels configuration
const levels = [
{
walls: [
{ top: 100, left: 50, width: 200, height: 20 },
{ top: 200, left: 100, width: 20, height: 150 },
{ top: 300, left: 200, width: 150, height: 20 },
],
},
{
walls: [
{ top: 50, left: 50, width: 20, height: 300 },
{ top: 150, left: 100, width: 300, height: 20 },
{ top: 250, left: 50, width: 200, height: 20 },
],
},
{
walls: [
{ top: 100, left: 50, width: 300, height: 20 },
{ top: 200, left: 100, width: 20, height: 200 },
{ top: 150, left: 300, width: 20, height: 150 },
],
},
];
// Create maze layout
function createMaze(level) {
maze.innerHTML = '';
walls = [];
// Player
player = document.createElement('div');
player.id = 'player';
player.style.top = '20px';
player.style.left = '20px';
maze.appendChild(player);
// Goal
goal = document.createElement('div');
goal.id = 'goal';
goal.style.top = '350px';
goal.style.left = '350px';
maze.appendChild(goal);
// Walls
levels[level - 1].walls.forEach((wallConfig) => {
const wall = document.createElement('div');
wall.className = 'wall';
wall.style.top = `${wallConfig.top}px`;
wall.style.left = `${wallConfig.left}px`;
wall.style.width = `${wallConfig.width}px`;
wall.style.height = `${wallConfig.height}px`;
maze.appendChild(wall);
walls.push(wall);
});
}
// Start the timer
function startTimer() {
timeLeft = 30;
timerDisplay.textContent = timeLeft;
timerInterval = setInterval(() => {
timeLeft--;
timerDisplay.textContent = timeLeft;
if (timeLeft <= 0) {
clearInterval(timerInterval);
status.textContent = 'Time is up! Game Over!';
document.removeEventListener('keydown', movePlayer);
}
}, 1000);
}
// Check if colliding with walls
function isCollidingWithWall(position) {
return walls.some((wall) => {
const wallBounds = wall.getBoundingClientRect();
return (
position.left < wallBounds.right &&
position.left + 20 > wallBounds.left &&
position.top < wallBounds.bottom &&
position.top + 20 > wallBounds.top
);
});
}
// Check if reaching the goal
function checkWin() {
const goalBounds = goal.getBoundingClientRect();
const playerBounds = player.getBoundingClientRect();
if (
playerBounds.left < goalBounds.right &&
playerBounds.right > goalBounds.left &&
playerBounds.top < goalBounds.bottom &&
playerBounds.bottom > goalBounds.top
) {
clearInterval(timerInterval);
score += timeLeft * 10; // Add score
scoreDisplay.textContent = score;
level++;
if (level > levels.length) {
status.textContent = `Congratulations! You completed all levels! Final Score: ${score}`;
document.removeEventListener('keydown', movePlayer);
} else {
levelInfo.textContent = `Level: ${level}`;
createMaze(level);
startTimer();
}
}
}
// Move the player
function movePlayer(e) {
const playerPosition = {
top: parseInt(player.style.top),
left: parseInt(player.style.left),
};
const step = 10;
const newPosition = { ...playerPosition };
switch (e.key) {
case 'ArrowUp':
newPosition.top -= step;
break;
case 'ArrowDown':
newPosition.top += step;
break;
case 'ArrowLeft':
newPosition.left -= step;
break;
case 'ArrowRight':
newPosition.left += step;
break;
default:
return;
}
if (
newPosition.top >= 0 &&
newPosition.left >= 0 &&
newPosition.top + 20 <= maze.clientHeight &&
newPosition.left + 20 <= maze.clientWidth &&
!isCollidingWithWall(newPosition)
) {
player.style.top = `${newPosition.top}px`;
player.style.left = `${newPosition.left}px`;
checkWin();
}
}
// Start the game
function startGame() {
level = 1;
score = 0;
levelInfo.textContent = `Level: ${level}`;
scoreDisplay.textContent = score;
status.textContent = 'Use arrow keys to move the player to the goal!';
createMaze(level);
startTimer();
document.addEventListener('keydown', movePlayer);
}
startGame();