Entrance Announcement
MICTE 2080
2080 Magh 07
Virtual game: Difference between revisions
Jump to navigation
Jump to search
(WikiSysop created the page Virtual game using a non-default content model "JavaScript") |
mNo edit summary |
||
Line 1: | Line 1: | ||
const wordContainer = document.getElementById('word-container'); | |||
const inputWord1 = document.getElementById('input-word1'); | |||
const inputWord2 = document.getElementById('input-word2'); | |||
const result = document.getElementById('result'); | |||
const scoreElement1 = document.getElementById('score1'); | |||
const scoreElement2 = document.getElementById('score2'); | |||
const submitWordButton1 = document.getElementById('submit-word1'); | |||
const submitWordButton2 = document.getElementById('submit-word2'); | |||
let score1 = 0; | |||
let score2 = 0; | |||
const words = ['कुतकुती','तारकेश्वोर','कमल', 'तालिम', 'मुसा', 'PIP', 'पाल', 'शाखा', 'म्युजिक', 'संख्या', 'रामायण', 'त्रिविवि']; | |||
function generateRandomWord() { | |||
const randomIndex = Math.floor(Math.random() * words.length); | |||
wordContainer.textContent = words[randomIndex]; | |||
wordContainer.style.backgroundColor = getRandomColor(); | |||
} | |||
function getRandomColor() { | |||
const letters = '0123456789ABCDEF'; | |||
let color = '#'; | |||
for (let i = 0; i < 6; i++) { | |||
color += letters[Math.floor(Math.random() * 16)]; | |||
} | |||
return color; | |||
} | |||
function submitWord(player) { | |||
const currentWord = wordContainer.textContent; | |||
const typedWord = player === 1 ? inputWord1.value.trim() : inputWord2.value.trim(); | |||
const scoreElement = player === 1 ? scoreElement1 : scoreElement2; | |||
let score = player === 1 ? score1 : score2; | |||
if (typedWord === currentWord) { | |||
score += 10; | |||
result.textContent = `Player ${player} Correct!`; | |||
result.style.color = 'green'; | |||
} else { | |||
result.textContent = `Player ${player} Incorrect!`; | |||
result.style.color = 'red'; | |||
} | |||
if (player === 1) { | |||
score1 = score; | |||
} else { | |||
score2 = score; | |||
} | |||
scoreElement.textContent = score; | |||
if (player === 1) { | |||
inputWord1.value = ''; | |||
} else { | |||
inputWord2.value = ''; | |||
} | |||
generateRandomWord(); | |||
} | |||
submitWordButton1.addEventListener('click', () => submitWord(1)); | |||
submitWordButton2.addEventListener('click', () => submitWord(2)); | |||
inputWord1.addEventListener('keyup', (event) => { | |||
if (event.key === 'Enter') { | |||
submitWord(1); | |||
} | |||
}); | |||
inputWord2.addEventListener('keyup', (event) => { | |||
if (event.key === 'Enter') { | |||
submitWord(2); | |||
} | |||
}); | |||
generateRandomWord(); |
Latest revision as of 04:16, 6 November 2024
const wordContainer = document.getElementById('word-container'); const inputWord1 = document.getElementById('input-word1'); const inputWord2 = document.getElementById('input-word2'); const result = document.getElementById('result'); const scoreElement1 = document.getElementById('score1'); const scoreElement2 = document.getElementById('score2'); const submitWordButton1 = document.getElementById('submit-word1'); const submitWordButton2 = document.getElementById('submit-word2'); let score1 = 0; let score2 = 0; const words = ['कुतकुती','तारकेश्वोर','कमल', 'तालिम', 'मुसा', 'PIP', 'पाल', 'शाखा', 'म्युजिक', 'संख्या', 'रामायण', 'त्रिविवि']; function generateRandomWord() { const randomIndex = Math.floor(Math.random() * words.length); wordContainer.textContent = words[randomIndex]; wordContainer.style.backgroundColor = getRandomColor(); } function getRandomColor() { const letters = '0123456789ABCDEF'; let color = '#'; for (let i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } function submitWord(player) { const currentWord = wordContainer.textContent; const typedWord = player === 1 ? inputWord1.value.trim() : inputWord2.value.trim(); const scoreElement = player === 1 ? scoreElement1 : scoreElement2; let score = player === 1 ? score1 : score2; if (typedWord === currentWord) { score += 10; result.textContent = `Player ${player} Correct!`; result.style.color = 'green'; } else { result.textContent = `Player ${player} Incorrect!`; result.style.color = 'red'; } if (player === 1) { score1 = score; } else { score2 = score; } scoreElement.textContent = score; if (player === 1) { inputWord1.value = ''; } else { inputWord2.value = ''; } generateRandomWord(); } submitWordButton1.addEventListener('click', () => submitWord(1)); submitWordButton2.addEventListener('click', () => submitWord(2)); inputWord1.addEventListener('keyup', (event) => { if (event.key === 'Enter') { submitWord(1); } }); inputWord2.addEventListener('keyup', (event) => { if (event.key === 'Enter') { submitWord(2); } }); generateRandomWord();