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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import streamlit.components.v1 as components
|
3 |
+
|
4 |
+
# Configure the Streamlit page
|
5 |
+
st.set_page_config(
|
6 |
+
page_title="Slot Machine Game",
|
7 |
+
page_icon="π°",
|
8 |
+
layout="wide",
|
9 |
+
initial_sidebar_state="collapsed"
|
10 |
+
)
|
11 |
+
|
12 |
+
# Custom CSS to modify Streamlit's default styling
|
13 |
+
st.markdown("""
|
14 |
+
<style>
|
15 |
+
.main > div {
|
16 |
+
padding-top: 1rem;
|
17 |
+
}
|
18 |
+
#root > div:nth-child(1) > div > div > div > div > section > div {
|
19 |
+
padding-top: 0rem;
|
20 |
+
}
|
21 |
+
</style>
|
22 |
+
""", unsafe_allow_html=True)
|
23 |
+
|
24 |
+
# Title
|
25 |
+
st.title("π° Slot Machine")
|
26 |
+
|
27 |
+
# HTML/JavaScript slot machine game
|
28 |
+
slot_machine_html = """
|
29 |
+
<!DOCTYPE html>
|
30 |
+
<html>
|
31 |
+
<head>
|
32 |
+
<title>40-Card Slot Machine</title>
|
33 |
+
<style>
|
34 |
+
body { margin: 0; background: #1a1a1a; color: #fff; font-family: Arial, sans-serif; }
|
35 |
+
.game-container {
|
36 |
+
width: 1024px;
|
37 |
+
height: 768px;
|
38 |
+
margin: 0 auto;
|
39 |
+
position: relative;
|
40 |
+
background: #2a2a2a;
|
41 |
+
padding: 20px;
|
42 |
+
border-radius: 10px;
|
43 |
+
border: 2px solid gold;
|
44 |
+
box-sizing: border-box;
|
45 |
+
}
|
46 |
+
.grid-container {
|
47 |
+
display: grid;
|
48 |
+
grid-template-columns: repeat(5, 1fr);
|
49 |
+
gap: 5px;
|
50 |
+
margin: 20px 0;
|
51 |
+
height: calc(100% - 120px);
|
52 |
+
}
|
53 |
+
.reel {
|
54 |
+
aspect-ratio: 1;
|
55 |
+
background: #333;
|
56 |
+
border: 2px solid gold;
|
57 |
+
display: flex;
|
58 |
+
justify-content: center;
|
59 |
+
align-items: center;
|
60 |
+
font-size: 32px;
|
61 |
+
position: relative;
|
62 |
+
transition: transform 0.3s;
|
63 |
+
}
|
64 |
+
.controls {
|
65 |
+
text-align: center;
|
66 |
+
margin-top: 20px;
|
67 |
+
position: absolute;
|
68 |
+
bottom: 20px;
|
69 |
+
width: calc(100% - 40px);
|
70 |
+
}
|
71 |
+
.button {
|
72 |
+
background: linear-gradient(45deg, #FFD700, #FFA500);
|
73 |
+
color: #000;
|
74 |
+
padding: 15px 30px;
|
75 |
+
border: none;
|
76 |
+
border-radius: 25px;
|
77 |
+
cursor: pointer;
|
78 |
+
font-size: 18px;
|
79 |
+
font-weight: bold;
|
80 |
+
transition: all 0.3s ease;
|
81 |
+
}
|
82 |
+
.button:hover { transform: scale(1.1); }
|
83 |
+
.button:disabled {
|
84 |
+
background: #666;
|
85 |
+
cursor: not-allowed;
|
86 |
+
}
|
87 |
+
#balance, #timer {
|
88 |
+
position: absolute;
|
89 |
+
top: 10px;
|
90 |
+
font-size: 24px;
|
91 |
+
background: #FFD700;
|
92 |
+
color: #000;
|
93 |
+
padding: 10px;
|
94 |
+
border-radius: 5px;
|
95 |
+
z-index: 10;
|
96 |
+
}
|
97 |
+
#balance { left: 10px; }
|
98 |
+
#timer { right: 10px; }
|
99 |
+
@keyframes spin {
|
100 |
+
0% { transform: rotateY(0deg); }
|
101 |
+
100% { transform: rotateY(360deg); }
|
102 |
+
}
|
103 |
+
.spinning {
|
104 |
+
animation: spin 0.5s linear infinite;
|
105 |
+
}
|
106 |
+
.win-line {
|
107 |
+
position: absolute;
|
108 |
+
background: rgba(255, 215, 0, 0.5);
|
109 |
+
z-index: 1;
|
110 |
+
pointer-events: none;
|
111 |
+
}
|
112 |
+
</style>
|
113 |
+
</head>
|
114 |
+
<body>
|
115 |
+
<div class="game-container">
|
116 |
+
<div id="balance">$100.00</div>
|
117 |
+
<div id="timer">10</div>
|
118 |
+
<div class="grid-container">
|
119 |
+
<!-- 40 reels will be created by JavaScript -->
|
120 |
+
</div>
|
121 |
+
<div class="controls">
|
122 |
+
<button class="button" onclick="spin()">SPIN ($1)</button>
|
123 |
+
</div>
|
124 |
+
</div>
|
125 |
+
|
126 |
+
<script>
|
127 |
+
// Previous JavaScript code remains exactly the same
|
128 |
+
let balance = 100;
|
129 |
+
let timer = 10;
|
130 |
+
let timerInterval;
|
131 |
+
let spinning = false;
|
132 |
+
let spinTimeouts = [];
|
133 |
+
const symbols = ['π°', 'π', '7οΈβ£', 'π²', 'π', 'π', 'π', 'π'];
|
134 |
+
let currentState = Array(40).fill('');
|
135 |
+
const ROWS = 8;
|
136 |
+
const COLS = 5;
|
137 |
+
|
138 |
+
// Initialize grid
|
139 |
+
function initializeGrid() {
|
140 |
+
const grid = document.querySelector('.grid-container');
|
141 |
+
grid.innerHTML = '';
|
142 |
+
for (let i = 0; i < ROWS * COLS; i++) {
|
143 |
+
const reel = document.createElement('div');
|
144 |
+
reel.className = 'reel';
|
145 |
+
reel.id = `reel-${i}`;
|
146 |
+
reel.textContent = symbols[Math.floor(Math.random() * symbols.length)];
|
147 |
+
grid.appendChild(reel);
|
148 |
+
}
|
149 |
+
}
|
150 |
+
|
151 |
+
function updateBalance(amount) {
|
152 |
+
balance += amount;
|
153 |
+
document.getElementById('balance').textContent = `$${balance.toFixed(2)}`;
|
154 |
+
}
|
155 |
+
|
156 |
+
function spin() {
|
157 |
+
if (spinning || balance < 1) return;
|
158 |
+
|
159 |
+
updateBalance(-1);
|
160 |
+
|
161 |
+
spinning = true;
|
162 |
+
resetTimer();
|
163 |
+
|
164 |
+
document.querySelectorAll('.win-line').forEach(line => line.remove());
|
165 |
+
|
166 |
+
for (let i = 0; i < ROWS * COLS; i++) {
|
167 |
+
const reel = document.getElementById(`reel-${i}`);
|
168 |
+
reel.classList.add('spinning');
|
169 |
+
|
170 |
+
const delay = 2000 + Math.random() * 2000;
|
171 |
+
spinTimeouts.push(setTimeout(() => stopReel(i), delay));
|
172 |
+
}
|
173 |
+
}
|
174 |
+
|
175 |
+
function stopReel(index) {
|
176 |
+
const reel = document.getElementById(`reel-${index}`);
|
177 |
+
const symbol = symbols[Math.floor(Math.random() * symbols.length)];
|
178 |
+
reel.textContent = symbol;
|
179 |
+
reel.classList.remove('spinning');
|
180 |
+
currentState[index] = symbol;
|
181 |
+
|
182 |
+
const stillSpinning = document.querySelectorAll('.spinning').length;
|
183 |
+
if (stillSpinning === 0) {
|
184 |
+
spinning = false;
|
185 |
+
checkWins();
|
186 |
+
}
|
187 |
+
}
|
188 |
+
|
189 |
+
function checkWins() {
|
190 |
+
let winningLines = [];
|
191 |
+
|
192 |
+
for (let row = 0; row < ROWS; row++) {
|
193 |
+
let startIdx = row * COLS;
|
194 |
+
let currentSymbol = currentState[startIdx];
|
195 |
+
let count = 1;
|
196 |
+
|
197 |
+
for (let col = 1; col < COLS; col++) {
|
198 |
+
if (currentState[startIdx + col] === currentSymbol) {
|
199 |
+
count++;
|
200 |
+
} else {
|
201 |
+
if (count >= 3) {
|
202 |
+
winningLines.push({
|
203 |
+
start: startIdx + col - count,
|
204 |
+
count: count,
|
205 |
+
direction: 'horizontal',
|
206 |
+
row: row
|
207 |
+
});
|
208 |
+
}
|
209 |
+
currentSymbol = currentState[startIdx + col];
|
210 |
+
count = 1;
|
211 |
+
}
|
212 |
+
}
|
213 |
+
if (count >= 3) {
|
214 |
+
winningLines.push({
|
215 |
+
start: startIdx + COLS - count,
|
216 |
+
count: count,
|
217 |
+
direction: 'horizontal',
|
218 |
+
row: row
|
219 |
+
});
|
220 |
+
}
|
221 |
+
}
|
222 |
+
|
223 |
+
let totalWin = 0;
|
224 |
+
winningLines.forEach(line => {
|
225 |
+
const multiplier = line.count - 2;
|
226 |
+
const baseWin = Math.min(multiplier * 10, 100);
|
227 |
+
totalWin += baseWin;
|
228 |
+
|
229 |
+
showWinLine(line);
|
230 |
+
});
|
231 |
+
|
232 |
+
if (totalWin > 0) {
|
233 |
+
updateBalance(totalWin);
|
234 |
+
setTimeout(() => {
|
235 |
+
document.querySelectorAll('.win-line').forEach(line => line.remove());
|
236 |
+
}, 3000);
|
237 |
+
}
|
238 |
+
}
|
239 |
+
|
240 |
+
function showWinLine(line) {
|
241 |
+
const winLine = document.createElement('div');
|
242 |
+
winLine.className = 'win-line';
|
243 |
+
|
244 |
+
const firstReel = document.getElementById(`reel-${line.start}`);
|
245 |
+
const reelWidth = firstReel.offsetWidth;
|
246 |
+
const reelHeight = firstReel.offsetHeight;
|
247 |
+
|
248 |
+
winLine.style.width = `${reelWidth * line.count}px`;
|
249 |
+
winLine.style.height = '4px';
|
250 |
+
winLine.style.top = `${(line.row * reelHeight) + (reelHeight / 2)}px`;
|
251 |
+
winLine.style.left = `${(line.start % COLS) * reelWidth}px`;
|
252 |
+
|
253 |
+
document.querySelector('.grid-container').appendChild(winLine);
|
254 |
+
}
|
255 |
+
|
256 |
+
function resetTimer() {
|
257 |
+
clearInterval(timerInterval);
|
258 |
+
timer = 10;
|
259 |
+
document.getElementById('timer').textContent = timer;
|
260 |
+
timerInterval = setInterval(() => {
|
261 |
+
timer--;
|
262 |
+
document.getElementById('timer').textContent = timer;
|
263 |
+
if (timer <= 0) {
|
264 |
+
resetGame();
|
265 |
+
}
|
266 |
+
}, 1000);
|
267 |
+
}
|
268 |
+
|
269 |
+
function resetGame() {
|
270 |
+
spinning = false;
|
271 |
+
clearInterval(timerInterval);
|
272 |
+
spinTimeouts.forEach(timeout => clearTimeout(timeout));
|
273 |
+
spinTimeouts = [];
|
274 |
+
document.querySelectorAll('.spinning').forEach(reel => reel.classList.remove('spinning'));
|
275 |
+
document.querySelectorAll('.win-line').forEach(line => line.remove());
|
276 |
+
resetTimer();
|
277 |
+
}
|
278 |
+
|
279 |
+
// Initialize the game
|
280 |
+
initializeGrid();
|
281 |
+
resetTimer();
|
282 |
+
</script>
|
283 |
+
</body>
|
284 |
+
</html>
|
285 |
+
"""
|
286 |
+
|
287 |
+
# Create the Streamlit component
|
288 |
+
components.html(
|
289 |
+
slot_machine_html,
|
290 |
+
height=800, # Slightly taller than game height to account for padding
|
291 |
+
width=1050, # Slightly wider than game width to account for padding
|
292 |
+
)
|