-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·238 lines (198 loc) · 6.38 KB
/
script.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
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
var text = [
"I make websites",
"I develop Discord bots",
"I build APIs",
"I write scripts",
"I perform systems administration",
"I harden systems",
"I know how to exit Vim",
"I program",
"I turn computers off and on... again",
"I use Arch btw",
"I like the command line",
"I publish FOSS",
"I host services",
"I manage servers",
"I write descriptive commit messages",
"I troubleshoot issues",
"I automate tasks",
"I document my code",
"I customize dotfiles",
"I implement security measures",
"I optimize performance",
"I maintain backups",
"I do a lot"
];
var counter = 0
var elem = document.getElementById("changeText")
change()
var inst = setInterval(change, 2000)
function change() {
elem.innerHTML = text[counter]
counter++
if (counter >= text.length) {
counter = 0
}
}
// current year
document.getElementById("current__year").innerHTML = new Date().getFullYear();
// When the user clicks on a navbar link, execute scrollToSection
document.querySelectorAll(".scroll").forEach(function (link) {
link.addEventListener("click", scrollToSection);
});
// Smoothly scroll to the target section, adjusting for the navbar height
function scrollToSection(event) {
event.preventDefault();
const navbarHeight = navbar.offsetHeight; // Get the height of the navbar
const targetId = this.getAttribute("href"); // Get the href value of the clicked link
// Calculate the offset position of the target section, subtracting the navbar height
const targetOffset = document.querySelector(targetId).offsetTop - (navbarHeight + 50);
// Scroll to the target section with a smooth behavior
window.scrollTo({
top: targetOffset,
behavior: "smooth"
});
}
//TICTACTOE CODE
const statusDisplay = document.querySelector('.tictactoe__status');
let gameActive = true;
let currentPlayer = "X";
let gameState = ["", "", "", "", "", "", "", "", ""];
const winningMessage = () => `Player ${currentPlayer} has won!`;
const drawMessage = () => `Game ended in a draw!`;
const currentPlayerTurn = () => `It's ${currentPlayer}'s turn`;
statusDisplay.innerHTML = currentPlayerTurn();
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function handleCellPlayed(clickedCell, clickedCellIndex) {
gameState[clickedCellIndex] = currentPlayer;
clickedCell.innerHTML = currentPlayer;
}
function handlePlayerChange() {
currentPlayer = currentPlayer === "X" ? "O" : "X";
statusDisplay.innerHTML = currentPlayerTurn();
}
function handleResultValidation() {
let roundWon = false;
for (let i = 0; i <= 7; i++) {
const winCondition = winningConditions[i];
let a = gameState[winCondition[0]];
let b = gameState[winCondition[1]];
let c = gameState[winCondition[2]];
if (a === '' || b === '' || c === '') {
continue;
}
if (a === b && b === c) {
roundWon = true;
break
}
}
if (roundWon) {
statusDisplay.innerHTML = winningMessage();
gameActive = false;
return;
}
let roundDraw = !gameState.includes("");
if (roundDraw) {
statusDisplay.innerHTML = drawMessage();
gameActive = false;
return;
}
handlePlayerChange();
}
function handleCellClick(clickedCellEvent) {
const clickedCell = clickedCellEvent.target;
const clickedCellIndex = parseInt(clickedCell.getAttribute('data-cell-index'));
if (gameState[clickedCellIndex] !== "" || !gameActive) {
return;
}
handleCellPlayed(clickedCell, clickedCellIndex);
handleResultValidation();
}
function tictactoeRestartGame() {
gameActive = true;
currentPlayer = "X";
gameState = ["", "", "", "", "", "", "", "", ""];
statusDisplay.innerHTML = currentPlayerTurn();
document.querySelectorAll('.cell').forEach(cell => cell.innerHTML = "");
}
document.querySelectorAll('.cell').forEach(cell => cell.addEventListener('click', handleCellClick));
// block-bounce
const player = document.getElementById("block-bounce-player");
const enemy = document.getElementById("block-bounce-enemy");
const gameContainer = document.getElementById("block-bounce");
const scoreDisplay = document.getElementById("block-bounce-score");
const slider = document.getElementById("block-bounce-speed");
let jumping = false;
let lastJumpTime = 0;
let score = 0;
let enemySpeed = 2;
slider.oninput = function () {
enemySpeed = this.value;
enemySpeed = Math.round(enemySpeed);
}
gameContainer.addEventListener("click", () => {
const currentTime = Date.now();
if (!jumping && currentTime - lastJumpTime >= 1000) {
jumping = true;
lastJumpTime = Date.now();
player.style.transition = "transform 0.5s ease-out";
player.style.transform = "translateY(-" + jumpHeight + "px)";
setTimeout(() => {
player.style.transition = "transform 0.5s ease-in";
player.style.transform = "translateY(0px)";
jumping = false;
}, 500);
}
});
function isDead() {
const playerRect = player.getBoundingClientRect();
const enemyRect = enemy.getBoundingClientRect();
return (
playerRect.left < enemyRect.right &&
playerRect.right > enemyRect.left &&
playerRect.top < enemyRect.bottom &&
playerRect.bottom > enemyRect.top
);
}
function gameLoop() {
const enemyRight = parseInt(getComputedStyle(enemy).right);
const containerWidth = gameContainer.offsetWidth;
jumpHeight = 100;
if (isDead()) {
enemy.style.right = "0px";
score = 0;
scoreDisplay.innerHTML = "Score: 0";
} else if (enemyRight + 50 < containerWidth) {
enemy.style.right = (enemyRight + enemySpeed) + "px";
} else {
enemy.style.right = "0px";
score++;
// fizzbuzz
if (score % 3 == 0 && score % 5 == 0) {
scoreDisplay.innerHTML = "Score: FizzBuzz";
}
// fizz
else if (score % 3 == 0) {
scoreDisplay.innerHTML = "Score: Fizz";
}
// buzz
else if (score % 5 == 0) {
scoreDisplay.innerHTML = "Score: Buzz";
}
// normal
else {
scoreDisplay.innerHTML = "Score: " + score;
}
}
requestAnimationFrame(gameLoop);
}
gameLoop();