hey can u make some games using only html css and javascript
- User
hey can u make some games using only html css and javascript
- Assistant
hey can u make some games using only html css and javascript
Certainly! Pure HTML, CSS, and JavaScript can be used to create a variety of games. Here are a couple of simple game ideas and how you might begin implementing them:
This simple game chooses a random number and asks the player to guess it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Guess the Number Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Guess the Number!</h1>
<p>We have selected a random number between 1 and 100. See if you can guess it.</p>
<input type="number" id="guessInput" min="1" max="100" />
<button onclick="guessNumber()">Guess</button>
<p id="result"></p>
<script src="script.js"></script>
</body>
</html>
styles.css):body {
font-family: 'Arial', sans-serif;
text-align: center;
}
input[type="number"] {
margin: 10px;
}
button {
cursor: pointer;
}
script.js):let randomNumber = Math.floor(Math.random() * 100) + 1;
function guessNumber() {
const userGuess = document.getElementById('guessInput').value;
const resultElement = document.getElementById('result');
if (userGuess == randomNumber) {
resultElement.textContent = 'Congratulations! You got it right!';
} else if (userGuess < randomNumber) {
resultElement.textContent = 'Too low! Try again.';
} else {
resultElement.textContent = 'Too high! Try again.';
}
}
This game presents a series of questions and checks the answers.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Quiz Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Simple Quiz Game</h1>
<div id="quiz-container">
<div class="question">
<p>What is 10 + 4?</p>
<input type="number" id="q1">
</div>
<div class="question">
<p>What is the capital of France?</p>
<input type="text" id="q2">
</div>
<button onclick="checkAnswers()">Submit Answers</button>
<p id="quiz-result"></p>
</div>
<script src="script.js"></script>
</body>
</html>
styles.css):/* Same as above, or add new styles */
script.js):function checkAnswers() {
let score = 0;
const answer1 = document.getElementById('q1').value;
if (parseInt(answer1) === 14) {
score++;
}
const answer2 = document.getElementById('q2').value.toLowerCase();
if (answer2 === 'paris') {
score++;
}
const result = document.getElementById('quiz-result');
result.textContent = 'You scored ' + score + ' out of 2.';
}
These games are quite basic and would need more features to be engaging, such as more questions for the quiz or better feedback for the guessing game. However, they serve as a starting point for building games using HTML, CSS, and JavaScript.
Remember to put your JavaScript <script src="script.js"></script> just before the closing </body> tag to ensure that the HTML is loaded before the JavaScript tries to modify it.