Skip to content

Commit 978c56d

Browse files
committed
Patch 2
1 parent 4b86980 commit 978c56d

File tree

18 files changed

+360
-44
lines changed

18 files changed

+360
-44
lines changed

02-Archery-Game/script.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ var svg = document.querySelector("svg");
22
var cursor = svg.createSVGPoint();
33
var arrows = document.querySelector(".arrows");
44
var randomAngle = 0;
5+
var totalScore = 0;
6+
7+
function syncScore() {
8+
if (window.GameHub) {
9+
if (typeof window.GameHub.setStageScore === 'function') {
10+
window.GameHub.setStageScore(totalScore);
11+
}
12+
if (typeof window.GameHub.recordScore === 'function') {
13+
window.GameHub.recordScore('archery', totalScore, 'best');
14+
}
15+
}
16+
}
517

618
// center of target
719
var target = {
@@ -159,6 +171,9 @@ function hitTest(tween) {
159171
selector = ".bullseye"
160172
}
161173
showMessage(selector);
174+
var gain = selector === ".bullseye" ? 5 : 2;
175+
totalScore += gain;
176+
syncScore();
162177
}
163178

164179
}
@@ -168,6 +183,9 @@ function onMiss() {
168183
showMessage(".miss");
169184
}
170185

186+
syncScore();
187+
}
188+
171189
function showMessage(selector) {
172190
// handle all text animations by providing selector
173191
TweenMax.killTweensOf(selector);
@@ -220,4 +238,4 @@ function getIntersection(segment1, segment2) {
220238
segment1: ua >= 0 && ua <= 1,
221239
segment2: ub >= 0 && ub <= 1
222240
};
223-
}
241+
}

11-Rock-Paper-Scissors/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const computerPointsEl = document.querySelector(".computer-points");
88
let userPoints= 0;
99
let computerPoints = 0;
1010

11+
function syncScore() {
12+
if (window.GameHub) {
13+
const { setStageScore, recordScore } = window.GameHub;
14+
if (typeof setStageScore === "function") setStageScore(userPoints);
15+
if (typeof recordScore === "function") recordScore('rps', userPoints, 'best');
16+
}
17+
}
18+
1119
imgEls.forEach((img) => {
1220
img.addEventListener("click", () => {
1321
const computerTurn = computerChoice();
@@ -36,10 +44,14 @@ function gamePlay(userSelection, computerSelection) {
3644
) {
3745
userPoints++;
3846
userPointsEl.textContent = userPoints;
47+
syncScore();
3948
return "Hurrah! You win..! " + userSelection + " beats " + computerSelection;
4049
} else {
4150
computerPoints++;
4251
computerPointsEl.textContent = computerPoints;
52+
syncScore();
4353
return "Oops! You lose...! " + computerSelection + " beats " + userSelection;
4454
}
45-
}
55+
}
56+
57+
syncScore();

12-Type-Number-Guessing-Game/index.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ let randomNumber = Math.floor(Math.random() * 100);
88

99
totalChances = 10;
1010

11+
function syncScore() {
12+
if (window.GameHub) {
13+
const { setStageScore, recordScore } = window.GameHub;
14+
if (typeof setStageScore === "function") setStageScore(Math.max(totalChances, 0));
15+
if (typeof recordScore === "function") recordScore('number-guess', Math.max(totalChances, 0), 'best');
16+
}
17+
}
18+
19+
syncScore();
20+
1121
checkBtnEl.addEventListener("click", () => {
12-
22+
const previousChances = totalChances;
1323
totalChances--;
1424
let inputValue = inputEl.value;
1525

@@ -20,6 +30,7 @@ checkBtnEl.addEventListener("click", () => {
2030
guessEl.style.color = "red";
2131
checkBtnEl.textContent = "Play Again...😉";
2232
remainingChancesTextEl.textContent = "No chances left"
33+
syncScore();
2334
}
2435
else if (totalChances < 0) {
2536
window.location.reload();
@@ -29,18 +40,28 @@ checkBtnEl.addEventListener("click", () => {
2940
guessEl.textContent = "Hurrah...! Congratulations😍, You won the game."
3041
guessEl.style.color = "green";
3142
checkBtnEl.textContent = "Play Again...😉";
43+
if (window.GameHub) {
44+
const { recordScore, addPoints, setStageScore } = window.GameHub;
45+
const scoreValue = Math.max(previousChances, 0);
46+
if (typeof recordScore === "function") recordScore('number-guess', scoreValue, 'best');
47+
if (typeof addPoints === "function") addPoints(Math.max(scoreValue, 0) * 10);
48+
if (typeof setStageScore === "function") setStageScore(scoreValue);
49+
}
3250
totalChances = 0;
3351
} else if (inputValue > randomNumber && inputValue < 100) {
3452
guessEl.textContent = "Your Guess is High👍.";
3553
remainingChancesEl.textContent = totalChances;
3654
guessEl.style.color = "#1446a0";
55+
syncScore();
3756
} else if (inputValue < randomNumber && inputValue > 0) {
3857
guessEl.textContent = "Your Guess is low👎.";
3958
remainingChancesEl.textContent = totalChances;
4059
guessEl.style.color = "#1446a0";
60+
syncScore();
4161
} else {
4262
guessEl.textContent = "Your number is invalid.";
4363
remainingChancesEl.textContent = totalChances;
4464
guessEl.style.color = "red";
65+
syncScore();
4566
}
4667
});

13-Tic-Tac-Toe/index.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ let currentPlayer=x;
2323
// Text of X or O to place in the status.
2424
let player="X";
2525
let running=false;
26+
let playerWins = 0;
27+
28+
function syncScore() {
29+
if (window.GameHub) {
30+
const { setStageScore, recordScore } = window.GameHub;
31+
if (typeof setStageScore === 'function') setStageScore(playerWins);
32+
if (typeof recordScore === 'function') recordScore('tic-tac-toe', playerWins, 'best');
33+
}
34+
}
2635
init();
2736

2837
// Initially it adds click event to every box. as if we click on any of the box then it calls the boxClick function.
@@ -75,6 +84,7 @@ function restartGame(){
7584
box.innerHTML="";
7685
box.classList.remove('win');
7786
});
87+
syncScore();
7888
}
7989

8090
// Checks winner
@@ -104,14 +114,19 @@ function checkWinner(){
104114
statusEl.style.color = "green"
105115
restartBtnEl.textContent = "Play Again 😉"
106116
running=false;
117+
playerWins += 1;
118+
syncScore();
107119
// if the game is draw then this executes.
108120
}else if(!options.includes("")){
109121
statusEl.textContent=`Oops..! Game Draw..!`;
110122
statusEl.style.color = "red"
111123
restartBtnEl.textContent = "Play Again 😉"
112124
running=false;
125+
syncScore();
113126
// else the player will change to continue the game.
114127
}else{
115128
changePlayer();
116129
}
117-
}
130+
}
131+
132+
syncScore();

15-Connect-Four-Game/script.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ var playerNumber = 1; // Initially player - 1 gets to start his/her turn
1111
var filledGrid = []; // Player board
1212

1313
var filledCells = 0; // No. of cells that has been filled
14+
var playerOneWins = 0;
15+
var playerTwoWins = 0;
16+
17+
function syncScore() {
18+
if (window.GameHub) {
19+
const best = Math.max(playerOneWins, playerTwoWins);
20+
const { setStageScore, recordScore } = window.GameHub;
21+
if (typeof setStageScore === 'function') setStageScore(best);
22+
if (typeof recordScore === 'function') recordScore('connect-four', best, 'best');
23+
}
24+
}
1425

1526
for(var i = 0; i < 6; i++) {
1627

@@ -62,6 +73,8 @@ function makeMove(button , buttonNo) {
6273
if(playerWon(row , col , 1) === true) {
6374
setTimeout(function() {
6475
alert("Game Over: Green Wins");
76+
playerOneWins += 1;
77+
syncScore();
6578
resetBoard();
6679
} , 200);
6780
}
@@ -81,6 +94,8 @@ function makeMove(button , buttonNo) {
8194
if(playerWon(row , col , 2) === true) {
8295
setTimeout(function() {
8396
alert("Game Over : Red Wins");
97+
playerTwoWins += 1;
98+
syncScore();
8499
resetBoard();
85100
} , 200);
86101
}
@@ -96,6 +111,7 @@ function makeMove(button , buttonNo) {
96111
if(filledCells === 42) {
97112
setTimeout(function() {
98113
alert("Game Draw");
114+
syncScore();
99115
resetBoard();
100116
} , 200);
101117
return;
@@ -237,6 +253,10 @@ function resetBoard() {
237253
for(var j = 0; j < 7; j++) {
238254
filledGrid[i][j] = -1;
239255
}
240-
}
256+
}
257+
258+
syncScore();
259+
260+
}
241261

242-
}
262+
syncScore();

18-Hangman-Game/script.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ let playable = true;
3939

4040
const correctLetters = [];
4141
const wrongLetters = [];
42+
let wins = 0;
43+
let losses = 0;
44+
45+
function syncScore() {
46+
if (window.GameHub) {
47+
const { setStageScore, recordScore } = window.GameHub;
48+
if (typeof setStageScore === 'function') setStageScore(wins);
49+
if (typeof recordScore === 'function') recordScore('hangman', wins, 'best');
50+
}
51+
}
4252

4353
function displayWord() {
4454
wordElement.innerHTML = `
@@ -59,6 +69,8 @@ function displayWord() {
5969
finalMessageRevealWord.innerText = "";
6070
popup.style.display = "flex";
6171
playable = false;
72+
wins += 1;
73+
syncScore();
6274
}
6375
}
6476

@@ -78,6 +90,8 @@ function updateWrongLettersElement() {
7890
finalMessageRevealWord.innerText = `...the word was: ${selectedWord}`;
7991
popup.style.display = "flex";
8092
playable = false;
93+
losses += 1;
94+
syncScore();
8195
}
8296
}
8397

@@ -119,7 +133,9 @@ playAgainButton.addEventListener("click", () => {
119133
displayWord();
120134
updateWrongLettersElement();
121135
popup.style.display = "none";
136+
syncScore();
122137
});
123138

124139
// Init
125140
displayWord();
141+
syncScore();

19-Flappy-Bird-Game/script.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var game_mode = "prestart";
88
var time_game_last_running;
99
var bottom_bar_offset = 0;
1010
var pipes = [];
11+
var currentScore = 0;
12+
var bestScore = 0;
13+
14+
syncScore();
1115

1216
function MySprite(img_url) {
1317
this.x = 0;
@@ -125,14 +129,16 @@ function display_intro_instructions() {
125129
);
126130
}
127131
function display_game_over() {
128-
var score = 0;
129-
for (var i = 0; i < pipes.length; i++)
130-
if (pipes[i].x < bird.x) score = score + 0.5;
132+
currentScore = scoreFromPipes();
133+
if (currentScore > bestScore) {
134+
bestScore = currentScore;
135+
syncScore();
136+
}
131137
ctx.font = "30px Arial";
132138
ctx.fillStyle = "red";
133139
ctx.textAlign = "center";
134140
ctx.fillText("Game Over", myCanvas.width / 2, 100);
135-
ctx.fillText("Score: " + score, myCanvas.width / 2, 150);
141+
ctx.fillText("Score: " + currentScore, myCanvas.width / 2, 150);
136142
ctx.font = "20px Arial";
137143
ctx.fillText("Click, touch, or press to play again", myCanvas.width / 2, 300);
138144
}
@@ -149,6 +155,7 @@ function reset_game() {
149155
bird.angle = 0;
150156
pipes = []; // erase all the pipes from the array
151157
add_all_my_pipes(); // and load them back in their starting positions
158+
currentScore = 0;
152159
}
153160
function add_all_my_pipes() {
154161
add_pipe(500, 100, 140);
@@ -188,6 +195,11 @@ function Do_a_Frame() {
188195
make_bird_tilt_appropriately();
189196
make_bird_slow_and_fall();
190197
check_for_end_game();
198+
currentScore = scoreFromPipes();
199+
if (currentScore > bestScore) {
200+
bestScore = currentScore;
201+
syncScore();
202+
}
191203
break;
192204
}
193205
case "over": {
@@ -205,3 +217,21 @@ bird.x = myCanvas.width / 3;
205217
bird.y = myCanvas.height / 2;
206218

207219
setInterval(Do_a_Frame, 1000 / FPS);
220+
function scoreFromPipes() {
221+
var score = 0;
222+
for (var i = 0; i < pipes.length; i++) {
223+
if (pipes[i].x < bird.x) score = score + 0.5;
224+
}
225+
return Math.floor(score);
226+
}
227+
228+
function syncScore() {
229+
if (window.GameHub) {
230+
if (typeof window.GameHub.setStageScore === 'function') {
231+
window.GameHub.setStageScore(bestScore);
232+
}
233+
if (typeof window.GameHub.recordScore === 'function') {
234+
window.GameHub.recordScore('flappy', bestScore, 'best');
235+
}
236+
}
237+
}

20-Crossy-Road-Game/script.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ let previousTimestamp;
4343
let startMoving;
4444
let moves;
4545
let stepStartTimestamp;
46+
let bestLane = 0;
47+
48+
function syncStageScore() {
49+
if (window.GameHub) {
50+
const { setStageScore, recordScore } = window.GameHub;
51+
if (typeof setStageScore === 'function') setStageScore(bestLane);
52+
if (typeof recordScore === 'function') recordScore('crossy-road', bestLane, 'best');
53+
}
54+
}
4655

4756
const carFrontTexture = new Texture(40, 80, [{ x: 0, y: 10, w: 30, h: 60 }]);
4857
const carBackTexture = new Texture(40, 80, [{ x: 10, y: 10, w: 30, h: 60 }]);
@@ -140,6 +149,7 @@ const initaliseValues = () => {
140149
};
141150

142151
initaliseValues();
152+
syncStageScore();
143153

144154
const renderer = new THREE.WebGLRenderer({
145155
alpha: true,
@@ -672,6 +682,10 @@ function animate(timestamp) {
672682
case "forward": {
673683
currentLane++;
674684
counterDOM.innerHTML = currentLane;
685+
if (currentLane > bestLane) {
686+
bestLane = currentLane;
687+
syncStageScore();
688+
}
675689
break;
676690
}
677691
case "backward": {

0 commit comments

Comments
 (0)