Create a slot machine game, use JavaScript and CSS.
Create a slot machine game, use JavaScript and CSS.
To create a fun and engaging slot machine game, we will start by designing a simple interface using HTML, CSS for visuals, and JavaScript for interactivity. Hereβs the plan:
Letβs begin!
<div class="slot-machine">
<div class="reels">
<div class="reel">π</div>
<div class="reel">π</div>
<div class="reel">π</div>
</div>
<button id="spin-button">Spin</button>
<div class="result"></div>
</div>
<script>
const symbols = ["π", "π", "π", "π", "π", "β"]; // Possible slot symbols
const reels = document.querySelectorAll(".reel");
const spinButton = document.getElementById("spin-button");
const resultDisplay = document.querySelector(".result");
function spinReels() {
resultDisplay.textContent = ""; // Clear previous result
// Generate random symbols for each reel
reels.forEach(reel => {
const randomSymbol = symbols[Math.floor(Math.random() * symbols.length)];
// Add spin animation
reel.style.animation = "spin 1s ease";
setTimeout(() => {
reel.textContent = randomSymbol; // Update symbol after animation
reel.style.animation = ""; // Remove animation
// Check if we have a match after spinning
checkWin();
}, 1000);
});
}
function checkWin() {
const reelSymbols = Array.from(reels).map(reel => reel.textContent);
const allSame = reelSymbols.every(symbol => symbol === reelSymbols[0]);
if (allSame) {
resultDisplay.textContent = "π You win! π";
} else {
resultDisplay.textContent = "Try again!";
}
}
spinButton.addEventListener("click", spinReels);
</script>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(to bottom, #eee, #ccc);
}
.slot-machine {
text-align: center;
background: white;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 300px;
}
.reels {
display: flex;
justify-content: space-around;
margin: 20px 0;
font-size: 2rem;
}
.reel {
width: 60px;
height: 60px;
background: #f8f8f8;
border: 4px solid #ddd;
border-radius: 8px;
display: flex;
justify-content: center;
align-items: center;
color: #333;
}
button {
background: #007bff;
color: white;
font-size: 1rem;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #0056b3;
}
.result {
margin-top: 20px;
font-size: 1.2rem;
font-weight: bold;
}
@keyframes spin {
0% {
transform: translateY(0) rotate(0deg);
}
25% {
transform: translateY(-20px) rotate(10deg);
}
50% {
transform: translateY(-40px) rotate(-10deg);
}
75% {
transform: translateY(-20px) rotate(5deg);
}
100% {
transform: translateY(0) rotate(0deg);
}
}
Create a simple HTML page for a slot machine game, use JavaScript and CSS. Create as a single HTML page for download.
Hereβs the complete slot machine game wrapped into a single HTML file, which you can download and use.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Slot Machine Game</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(to bottom, #eee, #ccc);
}
.slot-machine {
text-align: center;
background: white;
border-radius: 12px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
padding: 20px;
width: 300px;
}
.reels {
display: flex;
justify-content: space-around;
margin: 20px 0;
font-size: 2rem;
}
.reel {
width: 60px;
height: 60px;
background: #f8f8f8;
border: 4px solid #ddd;
border-radius: 8px;
display: flex;
justify-content: center;
align-items: center;
color: #333;
}
button {
background: #007bff;
color: white;
font-size: 1rem;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #0056b3;
}
.result {
margin-top: 20px;
font-size: 1.2rem;
font-weight: bold;
}
@keyframes spin {
0% {
transform: translateY(0) rotate(0deg);
}
25% {
transform: translateY(-20px) rotate(10deg);
}
50% {
transform: translateY(-40px) rotate(-10deg);
}
75% {
transform: translateY(-20px) rotate(5deg);
}
100% {
transform: translateY(0) rotate(0deg);
}
}
</style>
</head>
<body>
<div class="slot-machine">
<div class="reels">
<div class="reel">π</div>
<div class="reel">π</div>
<div class="reel">π</div>
</div>
<button id="spin-button">Spin</button>
<div class="result"></div>
</div>
<script>
const symbols = ["π", "π", "π", "π", "π", "β"]; // Possible slot symbols
const reels = document.querySelectorAll(".reel");
const spinButton = document.getElementById("spin-button");
const resultDisplay = document.querySelector(".result");
function spinReels() {
resultDisplay.textContent = ""; // Clear previous result
// Generate random symbols for each reel
reels.forEach(reel => {
const randomSymbol = symbols[Math.floor(Math.random() * symbols.length)];
// Add spin animation
reel.style.animation = "spin 1s ease";
setTimeout(() => {
reel.textContent = randomSymbol; // Update symbol after animation
reel.style.animation = ""; // Remove animation
// Check if we have a match after spinning
checkWin();
}, 1000);
});
}
function checkWin() {
const reelSymbols = Array.from(reels).map(reel => reel.textContent);
const allSame = reelSymbols.every(symbol => symbol === reelSymbols[0]);
if (allSame) {
resultDisplay.textContent = "π You win! π";
} else {
resultDisplay.textContent = "Try again!";
}
}
spinButton.addEventListener("click", spinReels);
</script>
</body>
</html>
You can save this as a .html
file (e.g., slot_machine.html
) on your computer and open it in any modern web browser to play the game.
Create a simple HTML page for a Blackjack game, use JavaScript and CSS.
- Create as single HTML page for download.
- Show the cards of the dealer after standing.
- Add option for additional bot players, show the bot players cards.
- Add option to bet and keep track of amount.
- Show total of player cards.
- Show dealer cards before displaying popup of who won.
Here is the complete Blackjack game as a single HTML file with the requested functionality. It includes JavaScript and CSS, providing a simple betting system, tracking of amounts, display of dealer and bot cards, and a popup showing the winner.
Below is the HTML file content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blackjack Game</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background: linear-gradient(to bottom, #3e8e41, #1d4d26);
color: white;
margin: 0;
padding: 20px;
height: 100vh;
overflow-y: auto;
}
.game {
max-width: 800px;
}
.header {
text-align: center;
}
.section {
margin: 20px 0;
}
.cards {
display: flex;
gap: 10px;
}
.card {
width: 50px;
height: 70px;
background: white;
color: black;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
border: 1px solid #666;
border-radius: 4px;
}
button {
cursor: pointer;
background: #28a745;
color: white;
border: none;
padding: 10px;
border-radius: 4px;
font-size: 1rem;
margin: 5px;
transition: background 0.2s;
}
button:hover {
background: #218838;
}
#popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
color: black;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
}
#popup button {
background: #dc3545;
}
</style>
</head>
<body>
<div class="game">
<div class="header">
<h1>Blackjack Game</h1>
<p>Your Balance: <span id="balance">$100</span></p>
</div>
<div>
<label for="bet">Place your Bet:</label>
<input type="number" id="bet" min="1" max="100" value="10">
</div>
<div id="player-section" class="section">
<h2>Your Cards (<span id="player-total">0</span>):</h2>
<div id="player-cards" class="cards"></div>
</div>
<div id="bot-section" class="section">
<h2>Bot's Cards:</h2>
<div id="bot-cards" class="cards"></div>
</div>
<div id="dealer-section" class="section">
<h2>Dealer's Cards:</h2>
<div id="dealer-cards" class="cards"></div>
</div>
<div>
<button id="hit">Hit</button>
<button id="stand">Stand</button>
</div>
</div>
<div id="popup">
<p id="result-text"></p>
<button id="restart">Restart</button>
</div>
<script>
const deck = [];
const suits = ['β₯', 'β¦', 'β£', 'β '];
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
let balance = 100;
let currentBet;
let playerCards = [];
let dealerCards = [];
let botCards = [];
let gameOver = false;
const balanceEl = document.getElementById('balance');
const betEl = document.getElementById('bet');
const playerTotalEl = document.getElementById('player-total');
const playerCardsEl = document.getElementById('player-cards');
const dealerCardsEl = document.getElementById('dealer-cards');
const botCardsEl = document.getElementById('bot-cards');
const hitButton = document.getElementById('hit');
const standButton = document.getElementById('stand');
const popup = document.getElementById('popup');
const resultTextEl = document.getElementById('result-text');
const restartButton = document.getElementById('restart');
// Initialize deck
function initializeDeck() {
deck.length = 0;
for (const suit of suits) {
for (const value of values) {
deck.push({ value, suit });
}
}
}
// Shuffle deck
function shuffleDeck() {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}
// Draw card
function drawCard() {
return deck.pop();
}
// Calculate total
function calculateTotal(cards) {
let total = 0;
let aces = 0;
for (const card of cards) {
if (card.value === 'A') {
aces++;
total += 11;
} else if (['J', 'Q', 'K'].includes(card.value)) {
total += 10;
} else {
total += parseInt(card.value);
}
}
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
}
// Render cards
function renderCards(cards, container) {
container.innerHTML = '';
for (const card of cards) {
const cardEl = document.createElement('div');
cardEl.className = 'card';
cardEl.textContent = `${card.value}${card.suit}`;
container.appendChild(cardEl);
}
}
// Update game state
function updateGameState() {
renderCards(playerCards, playerCardsEl);
playerTotalEl.textContent = calculateTotal(playerCards);
renderCards(botCards, botCardsEl);
renderCards(dealerCards, dealerCardsEl);
if (gameOver) return;
if (calculateTotal(playerCards) > 21) {
endGame('You busted! Dealer wins!');
}
}
// Start game
function startGame() {
initializeDeck();
shuffleDeck();
playerCards = [drawCard(), drawCard()];
botCards = [drawCard(), drawCard()];
dealerCards = [drawCard()];
currentBet = parseInt(betEl.value, 10) || 10;
balance -= currentBet;
balanceEl.textContent = `$${balance}`;
gameOver = false;
updateGameState();
}
// End game
function endGame(result) {
gameOver = true;
dealerCards.push(drawCard());
const dealerTotal = calculateTotal(dealerCards);
renderCards(dealerCards, dealerCardsEl);
if (!result) {
const playerTotal = calculateTotal(playerCards);
const botTotal = calculateTotal(botCards);
if ((dealerTotal > 21 || playerTotal > dealerTotal) && playerTotal <= 21) {
result = 'You win!';
balance += currentBet * 2; // Winnings
} else {
result = 'Dealer wins!';
}
}
balanceEl.textContent = `$${balance}`;
resultTextEl.textContent = result;
popup.style.display = 'block';
}
// Event listeners
hitButton.addEventListener('click', () => {
if (gameOver) return;
playerCards.push(drawCard());
updateGameState();
});
standButton.addEventListener('click', () => {
if (gameOver) return;
endGame();
});
restartButton.addEventListener('click', () => {
popup.style.display = 'none';
startGame();
});
// Start first game
startGame();
</script>
</body>
</html>
blackjack.html
.This game covers all requested features, including betting, card totals, dealer logic, bot players, and displaying results effectively.
Make an artifact of it and embed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blackjack Game</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background: linear-gradient(to bottom, #3e8e41, #1d4d26);
color: white;
margin: 0;
padding: 20px;
height: 100vh;
overflow-y: auto;
}
.game {
max-width: 800px;
}
.header {
text-align: center;
}
.section {
margin: 20px 0;
}
.cards {
display: flex;
gap: 10px;
}
.card {
width: 50px;
height: 70px;
background: white;
color: black;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
border: 1px solid #666;
border-radius: 4px;
}
button {
cursor: pointer;
background: #28a745;
color: white;
border: none;
padding: 10px;
border-radius: 4px;
font-size: 1rem;
margin: 5px;
transition: background 0.2s;
}
button:hover {
background: #218838;
}
#popup {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
color: black;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
}
#popup button {
background: #dc3545;
}
</style>
</head>
<body>
<div class="game">
<div class="header">
<h1>Blackjack Game</h1>
<p>Your Balance: <span id="balance">$100</span></p>
</div>
<div>
<label for="bet">Place your Bet:</label>
<input type="number" id="bet" min="1" max="100" value="10">
</div>
<div id="player-section" class="section">
<h2>Your Cards (<span id="player-total">0</span>):</h2>
<div id="player-cards" class="cards"></div>
</div>
<div id="bot-section" class="section">
<h2>Bot's Cards:</h2>
<div id="bot-cards" class="cards"></div>
</div>
<div id="dealer-section" class="section">
<h2>Dealer's Cards:</h2>
<div id="dealer-cards" class="cards"></div>
</div>
<div>
<button id="hit">Hit</button>
<button id="stand">Stand</button>
</div>
</div>
<div id="popup">
<p id="result-text"></p>
<button id="restart">Restart</button>
</div>
<script>
const deck = [];
const suits = ['β₯', 'β¦', 'β£', 'β '];
const values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
let balance = 100;
let currentBet;
let playerCards = [];
let dealerCards = [];
let botCards = [];
let gameOver = false;
const balanceEl = document.getElementById('balance');
const betEl = document.getElementById('bet');
const playerTotalEl = document.getElementById('player-total');
const playerCardsEl = document.getElementById('player-cards');
const dealerCardsEl = document.getElementById('dealer-cards');
const botCardsEl = document.getElementById('bot-cards');
const hitButton = document.getElementById('hit');
const standButton = document.getElementById('stand');
const popup = document.getElementById('popup');
const resultTextEl = document.getElementById('result-text');
const restartButton = document.getElementById('restart');
// Initialize deck
function initializeDeck() {
deck.length = 0;
for (const suit of suits) {
for (const value of values) {
deck.push({ value, suit });
}
}
}
// Shuffle deck
function shuffleDeck() {
for (let i = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}
// Draw card
function drawCard() {
return deck.pop();
}
// Calculate total
function calculateTotal(cards) {
let total = 0;
let aces = 0;
for (const card of cards) {
if (card.value === 'A') {
aces++;
total += 11;
} else if (['J', 'Q', 'K'].includes(card.value)) {
total += 10;
} else {
total += parseInt(card.value);
}
}
while (total > 21 && aces > 0) {
total -= 10;
aces--;
}
return total;
}
// Render cards
function renderCards(cards, container) {
container.innerHTML = '';
for (const card of cards) {
const cardEl = document.createElement('div');
cardEl.className = 'card';
cardEl.textContent = `${card.value}${card.suit}`;
container.appendChild(cardEl);
}
}
// Update game state
function updateGameState() {
renderCards(playerCards, playerCardsEl);
playerTotalEl.textContent = calculateTotal(playerCards);
renderCards(botCards, botCardsEl);
renderCards(dealerCards, dealerCardsEl);
if (gameOver) return;
if (calculateTotal(playerCards) > 21) {
endGame('You busted! Dealer wins!');
}
}
// Start game
function startGame() {
initializeDeck();
shuffleDeck();
playerCards = [drawCard(), drawCard()];
botCards = [drawCard(), drawCard()];
dealerCards = [drawCard()];
currentBet = parseInt(betEl.value, 10) || 10;
balance -= currentBet;
balanceEl.textContent = `$${balance}`;
gameOver = false;
updateGameState();
}
// End game
function endGame(result) {
gameOver = true;
dealerCards.push(drawCard());
const dealerTotal = calculateTotal(dealerCards);
renderCards(dealerCards, dealerCardsEl);
if (!result) {
const playerTotal = calculateTotal(playerCards);
const botTotal = calculateTotal(botCards);
if ((dealerTotal > 21 || playerTotal > dealerTotal) && playerTotal <= 21) {
result = 'You win!';
balance += currentBet * 2; // Winnings
} else {
result = 'Dealer wins!';
}
}
balanceEl.textContent = `$${balance}`;
resultTextEl.textContent = result;
popup.style.display = 'block';
}
// Event listeners
hitButton.addEventListener('click', () => {
if (gameOver) return;
playerCards.push(drawCard());
updateGameState();
});
standButton.addEventListener('click', () => {
if (gameOver) return;
endGame();
});
restartButton.addEventListener('click', () => {
popup.style.display = 'none';
startGame();
});
// Start first game
startGame();
</script>
</body>
</html>