Skip to content

Commit 9a47ba3

Browse files
Added JavaScript Programs
1 parent d333e1b commit 9a47ba3

15 files changed

Lines changed: 1630 additions & 0 deletions

File tree

‎JavaScript Dragon Game/bg.png‎

778 KB
Loading

‎JavaScript Dragon Game/dino.png‎

1.01 MB
Loading

‎JavaScript Dragon Game/dragon.png‎

516 KB
Loading
305 KB
Binary file not shown.

‎JavaScript Dragon Game/index.html‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>iDragon - JavaScript Game</title>
7+
<link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@300&display=swap" rel="stylesheet">
8+
<link rel="stylesheet" href="style.css">
9+
<script src="script.js"></script>
10+
</head>
11+
<body>
12+
<div class="gameContainer">
13+
<div class="gameOver">Welcome to iDragon Adventures</div>
14+
<!-- <div class="gameOver">Welcome to iDragon - Created by Harry</div> -->
15+
<div class="dino"></div>
16+
<div id="scoreCont">Your Score: 0</div>
17+
<div class="obstacle obstacleAni"></div>
18+
</div>
19+
</body>
20+
</html>

‎JavaScript Dragon Game/music.mp3‎

2.55 MB
Binary file not shown.

‎JavaScript Dragon Game/script.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
score = 0;
2+
cross = true;
3+
4+
audio = new Audio('music.mp3');
5+
audiogo = new Audio('gameover.mp3');
6+
setTimeout(() => {
7+
audio.play()
8+
}, 1000);
9+
document.onkeydown = function (e) {
10+
console.log("Key code is: ", e.keyCode)
11+
if (e.keyCode == 38) {
12+
dino = document.querySelector('.dino');
13+
dino.classList.add('animateDino');
14+
setTimeout(() => {
15+
dino.classList.remove('animateDino')
16+
}, 700);
17+
}
18+
if (e.keyCode == 39) {
19+
dino = document.querySelector('.dino');
20+
dinoX = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
21+
dino.style.left = dinoX + 112 + "px";
22+
}
23+
if (e.keyCode == 37) {
24+
dino = document.querySelector('.dino');
25+
dinoX = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
26+
dino.style.left = (dinoX - 112) + "px";
27+
}
28+
}
29+
30+
setInterval(() => {
31+
dino = document.querySelector('.dino');
32+
gameOver = document.querySelector('.gameOver');
33+
obstacle = document.querySelector('.obstacle');
34+
35+
dx = parseInt(window.getComputedStyle(dino, null).getPropertyValue('left'));
36+
dy = parseInt(window.getComputedStyle(dino, null).getPropertyValue('top'));
37+
38+
ox = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue('left'));
39+
oy = parseInt(window.getComputedStyle(obstacle, null).getPropertyValue('top'));
40+
41+
offsetX = Math.abs(dx - ox);
42+
offsetY = Math.abs(dy - oy);
43+
// console.log(offsetX, offsetY)
44+
if (offsetX < 73 && offsetY < 52) {
45+
gameOver.innerHTML = "Game Over - Reload to Play Again"
46+
obstacle.classList.remove('obstacleAni')
47+
audiogo.play();
48+
setTimeout(() => {
49+
audiogo.pause();
50+
audio.pause();
51+
}, 1000);
52+
}
53+
else if (offsetX < 145 && cross) {
54+
score += 1;
55+
updateScore(score);
56+
cross = false;
57+
setTimeout(() => {
58+
cross = true;
59+
}, 1000);
60+
setTimeout(() => {
61+
aniDur = parseFloat(window.getComputedStyle(obstacle, null).getPropertyValue('animation-duration'));
62+
newDur = aniDur - 0.1;
63+
obstacle.style.animationDuration = newDur + 's';
64+
console.log('New animation duration: ', newDur)
65+
}, 500);
66+
67+
}
68+
69+
}, 10);
70+
71+
function updateScore(score) {
72+
scoreCont.innerHTML = "Your Score: " + score
73+
}

‎JavaScript Dragon Game/style.css‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
*{
2+
margin: 0;
3+
padding:0;
4+
}
5+
body{
6+
background-color: red;
7+
overflow: hidden;
8+
}
9+
.gameContainer{
10+
background-image: url(bg.png);
11+
background-repeat: no-repeat;
12+
background-size: 100vw 100vh;
13+
width: 100%;
14+
height: 100vh;
15+
}
16+
17+
.dino{
18+
background-image: url(dino.png);
19+
background-repeat: no-repeat;
20+
background-size: cover;
21+
width: 233px;
22+
height: 114px;
23+
position: absolute;
24+
bottom:0;
25+
left: 52px;
26+
}
27+
28+
.obstacle{
29+
width: 166px;
30+
height: 113px;
31+
background-image: url(dragon.png);
32+
background-size: cover;
33+
position: absolute;
34+
bottom: 0;
35+
left: 44vw;
36+
}
37+
38+
.animateDino{
39+
animation: dino 0.6s linear;
40+
}
41+
42+
.obstacleAni{
43+
animation: obstacleAni 5s linear infinite;
44+
}
45+
46+
.gameOver{
47+
position: relative;
48+
top:63px;
49+
font-size: 53px;
50+
text-align: center;
51+
font-family: 'Ubuntu', sans-serif;
52+
}
53+
54+
#scoreCont{
55+
font-size: 25px;
56+
color: #54212f;
57+
font-weight: bold;
58+
position: absolute;
59+
right: 45px;
60+
top: 31px;
61+
border: 2px solid black;
62+
padding: 10px;
63+
font-family: 'Ubuntu', sans-serif;
64+
border-radius: 10px;
65+
}
66+
67+
@keyframes dino{
68+
0%{
69+
bottom: 0;
70+
}
71+
50%{
72+
bottom: 422px;
73+
}
74+
100%{
75+
bottom: 0;
76+
}
77+
}
78+
79+
@keyframes obstacleAni{
80+
0%{
81+
left: 100vw;
82+
}
83+
100%{
84+
left: -10vw;
85+
}
86+
}

‎NodeJs-Web-Scraper/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Node Js Web Scraper
2+
3+
## This program scrapes the entire web and displays the number of times the given word appears in search results of Google.
4+
5+
### It also checks for the validity of a word and hence if a word is not a valid english word, it throws an error to the user showing invalid word. It then assigns a point to the found word based on the number of searches.
6+
7+
## How to run:
8+
9+
1. Download node Js
10+
2. Download this zipped folder
11+
3. Just open the directory & run cmd line
12+
4. Run the following commands:-
13+
npm install
14+
node scrapers.js <word>
15+
Example: node scrapers.js hello

‎NodeJs-Web-Scraper/scraper.js‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
const puppeteer = require('puppeteer'); // web scraping
2+
let checkWord = require('check-word'); // Checks the words
3+
4+
// Points table
5+
const pointsTable = {
6+
100: [1, 25],
7+
99: [25.0000001, 50],
8+
98: [50.0000001, 75],
9+
97: [75.0000001, 100],
10+
96: [100.0000001, 125],
11+
95: [125.0000001, 150],
12+
94: [150.0000001, 175],
13+
93: [175.0000001, 200],
14+
92: [200.0000001, 225],
15+
91: [225.0000001, 250],
16+
90: [250.0000001, 275],
17+
89: [275.0000001, 300],
18+
88: [300.0000001, 325],
19+
87: [325.0000001, 350],
20+
86: [350.0000001, 375],
21+
85: [375.0000001, 400],
22+
84: [400.0000001, 425],
23+
83: [425.0000001, 450],
24+
82: [450.0000001, 475],
25+
81: [475.0000001, 500],
26+
80: [500.0000001, 525],
27+
79: [525.0000001, 550],
28+
78: [550.0000001, 575],
29+
77: [575.0000001, 600],
30+
76: [600.0000001, 625],
31+
75: [625.0000001, 650],
32+
74: [650.0000001, 675],
33+
73: [675.0000001, 700],
34+
72: [700.0000001, 725],
35+
71: [725.0000001, 750],
36+
70: [750.0000001, 775],
37+
69: [775.0000001, 800],
38+
68: [800.0000001, 825],
39+
67: [825.0000001, 850],
40+
66: [850.0000001, 875],
41+
65: [875.0000001, 900],
42+
64: [900.0000001, 925],
43+
63: [925.0000001, 950],
44+
62: [950.0000001, 975],
45+
61: [975.0000001, 1000],
46+
60: [1000.0000001, 1025],
47+
59: [1025.0000001, 1050],
48+
58: [1050.0000001, 1075],
49+
57: [1075.0000001, 1100],
50+
56: [1100.0000001, 1125],
51+
55: [1125.0000001, 1150],
52+
54: [1150.0000001, 1175],
53+
53: [1175.0000001, 1200],
54+
52: [1200.0000001, 1225],
55+
51: [1225.0000001, 1250],
56+
50: [1250.0000001, 1275],
57+
49: [1275.0000001, 1300],
58+
48: [1300.0000001, 1325],
59+
47: [1325.0000001, 1350],
60+
46: [1350.0000001, 1375],
61+
45: [1375.0000001, 1400],
62+
44: [1400.0000001, 1425],
63+
43: [1425.0000001, 1450],
64+
42: [1450.0000001, 1475],
65+
41: [1475.0000001, 1500],
66+
40: [1500.0000001, 1525],
67+
39: [1525.0000001, 1550],
68+
38: [1550.0000001, 1575],
69+
37: [1575.0000001, 1600],
70+
36: [1600.0000001, 1625],
71+
35: [1625.0000001, 1650],
72+
34: [1650.0000001, 1675],
73+
33: [1675.0000001, 1700],
74+
32: [1700.0000001, 1725],
75+
31: [1725.0000001, 1750],
76+
30: [1750.0000001, 1775],
77+
29: [1775.0000001, 1800],
78+
28: [1800.0000001, 1825],
79+
27: [1825.0000001, 1850],
80+
26: [1850.0000001, 1875],
81+
25: [1875.0000001, 1900],
82+
24: [1900.0000001, 1925],
83+
23: [1925.0000001, 1950],
84+
22: [1950.0000001, 1975],
85+
21: [1975.0000001, 2000],
86+
20: [2000.0000001, 2025],
87+
19: [2025.0000001, 2050],
88+
18: [2050.0000001, 2075],
89+
17: [2075.0000001, 2100],
90+
16: [2100.0000001, 2125],
91+
15: [2125.0000001, 2150],
92+
14: [2150.0000001, 2175],
93+
13: [2175.0000001, 2200],
94+
12: [2200.0000001, 2225],
95+
11: [2225.0000001, 2250],
96+
10: [2250.0000001, 2275],
97+
9: [2275.0000001, 2300],
98+
8: [2300.0000001, 2325],
99+
7: [2325.0000001, 2350],
100+
6: [2350.0000001, 2375],
101+
5: [2375.0000001, 2400],
102+
4: [2400.0000001, 2425],
103+
3: [2425.0000001, 2450],
104+
2: [2450.0000001, 2475],
105+
1: [2475.0000001, 25000000],
106+
};
107+
108+
// Checking if the word even exists
109+
function checkIfWordExists(word) {
110+
words = checkWord('en'); // We are looking for english words
111+
if (words.check(word)) {
112+
startScrape(`https://www.google.com/search?q=${word}`);
113+
} else {
114+
console.log('Invalid word...');
115+
}
116+
}
117+
118+
// Web scrapes everything
119+
async function startScrape(url) {
120+
const browser = await puppeteer.launch();
121+
const page = await browser.newPage();
122+
await page.goto(url);
123+
124+
const [el] = await page.$x('//*[@id="result-stats"]'); // X path
125+
const text = await el.getProperty('textContent');
126+
const rawText = await text.jsonValue();
127+
const actualNumber = +rawText.split(' ')[1].split(',').join('') / 10000000;
128+
console.log(
129+
'The word appeared a total of:',
130+
actualNumber * 10000000,
131+
'times'
132+
);
133+
allocatePoints(actualNumber);
134+
}
135+
136+
function allocatePoints(number) {
137+
let found = false;
138+
for (let [key, value] of Object.entries(pointsTable)) {
139+
if (value[0] <= number && value[1] >= number) {
140+
found = true;
141+
console.log('Total Points awarded:', key);
142+
}
143+
}
144+
if (!found) {
145+
console.log('Total Points awarded: 0');
146+
}
147+
}
148+
149+
// Pass arguments via CMD
150+
let searchTerm = process.argv[2];
151+
checkIfWordExists(searchTerm);

0 commit comments

Comments
 (0)