Download app.py from Rashi-jan/abcd: direct link, hf CLI and curl.
- Browser
- Download file 11.2 kB
-
https://huggingface.co/spaces/Rashi-jan/abcd/resolve/main/app.py
- Command line
-
hf download hf://spaces/Rashi-jan/abcd/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Rashi-jan/abcd/resolve/main/app.py
11.2 kB
| import gradio as gr | |
| HTML = """ | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <style> | |
| * { | |
| box-sizing: border-box; | |
| } | |
| body { | |
| margin: 0; | |
| font-family: Arial, sans-serif; | |
| background: #0f172a; | |
| color: white; | |
| } | |
| .game-wrapper { | |
| width: 100%; | |
| max-width: 620px; | |
| margin: auto; | |
| text-align: center; | |
| padding: 15px; | |
| } | |
| h1 { | |
| margin: 5px 0 10px; | |
| color: #22c55e; | |
| font-size: 32px; | |
| } | |
| .info { | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: center; | |
| background: #1e293b; | |
| border-radius: 12px; | |
| padding: 12px 18px; | |
| margin-bottom: 12px; | |
| font-size: 18px; | |
| font-weight: bold; | |
| } | |
| #gameCanvas { | |
| display: block; | |
| width: 100%; | |
| max-width: 560px; | |
| aspect-ratio: 1 / 1; | |
| margin: auto; | |
| background: #020617; | |
| border: 3px solid #22c55e; | |
| border-radius: 12px; | |
| box-shadow: 0 0 25px rgba(34, 197, 94, 0.25); | |
| } | |
| .controls { | |
| margin-top: 15px; | |
| display: grid; | |
| grid-template-columns: repeat(3, 70px); | |
| justify-content: center; | |
| gap: 8px; | |
| } | |
| button { | |
| border: none; | |
| border-radius: 10px; | |
| background: #334155; | |
| color: white; | |
| font-size: 22px; | |
| padding: 14px; | |
| cursor: pointer; | |
| transition: 0.15s; | |
| touch-action: manipulation; | |
| } | |
| button:hover { | |
| background: #22c55e; | |
| color: #052e16; | |
| } | |
| .empty { | |
| visibility: hidden; | |
| } | |
| #startBtn { | |
| margin-top: 15px; | |
| background: #22c55e; | |
| color: #052e16; | |
| font-weight: bold; | |
| padding: 12px 28px; | |
| font-size: 16px; | |
| } | |
| #message { | |
| margin-top: 10px; | |
| min-height: 24px; | |
| color: #cbd5e1; | |
| } | |
| .instructions { | |
| margin-top: 12px; | |
| color: #94a3b8; | |
| font-size: 14px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="game-wrapper"> | |
| <h1>🐍 Snake Game</h1> | |
| <div class="info"> | |
| <span>Score: <span id="score">0</span></span> | |
| <span>High Score: <span id="highScore">0</span></span> | |
| </div> | |
| <canvas id="gameCanvas" width="560" height="560"></canvas> | |
| <div id="message"> | |
| Press Start or use Arrow Keys | |
| </div> | |
| <button id="startBtn" onclick="startGame()">Start Game</button> | |
| <div class="controls"> | |
| <button class="empty">.</button> | |
| <button onclick="changeDirection('UP')">⬆️</button> | |
| <button class="empty">.</button> | |
| <button onclick="changeDirection('LEFT')">⬅️</button> | |
| <button onclick="changeDirection('DOWN')">⬇️</button> | |
| <button onclick="changeDirection('RIGHT')">➡️</button> | |
| </div> | |
| <div class="instructions"> | |
| Use Arrow Keys or the buttons above to control the snake. | |
| </div> | |
| </div> | |
| <script> | |
| const canvas = document.getElementById("gameCanvas"); | |
| const ctx = canvas.getContext("2d"); | |
| const scoreElement = document.getElementById("score"); | |
| const highScoreElement = document.getElementById("highScore"); | |
| const messageElement = document.getElementById("message"); | |
| const startButton = document.getElementById("startBtn"); | |
| const gridSize = 28; | |
| const tileSize = canvas.width / gridSize; | |
| let snake = []; | |
| let food = {}; | |
| let direction = "RIGHT"; | |
| let nextDirection = "RIGHT"; | |
| let score = 0; | |
| let highScore = Number(localStorage.getItem("snakeHighScore")) || 0; | |
| let gameRunning = false; | |
| let gameOver = false; | |
| let gameLoop = null; | |
| highScoreElement.textContent = highScore; | |
| function resetGame() { | |
| snake = [ | |
| {x: 10, y: 14}, | |
| {x: 9, y: 14}, | |
| {x: 8, y: 14} | |
| ]; | |
| direction = "RIGHT"; | |
| nextDirection = "RIGHT"; | |
| score = 0; | |
| scoreElement.textContent = score; | |
| createFood(); | |
| drawGame(); | |
| } | |
| function startGame() { | |
| if (gameLoop) { | |
| clearInterval(gameLoop); | |
| } | |
| resetGame(); | |
| gameRunning = true; | |
| gameOver = false; | |
| startButton.textContent = "Restart Game"; | |
| messageElement.textContent = "Game started!"; | |
| gameLoop = setInterval(updateGame, 100); | |
| } | |
| function changeDirection(newDirection) { | |
| if (!gameRunning) return; | |
| const opposite = { | |
| UP: "DOWN", | |
| DOWN: "UP", | |
| LEFT: "RIGHT", | |
| RIGHT: "LEFT" | |
| }; | |
| if (newDirection !== opposite[direction]) { | |
| nextDirection = newDirection; | |
| } | |
| } | |
| document.addEventListener("keydown", function(event) { | |
| const keys = { | |
| ArrowUp: "UP", | |
| ArrowDown: "DOWN", | |
| ArrowLeft: "LEFT", | |
| ArrowRight: "RIGHT" | |
| }; | |
| if (keys[event.key]) { | |
| event.preventDefault(); | |
| if (!gameRunning) { | |
| startGame(); | |
| } | |
| changeDirection(keys[event.key]); | |
| } | |
| }); | |
| function updateGame() { | |
| direction = nextDirection; | |
| const head = { | |
| x: snake[0].x, | |
| y: snake[0].y | |
| }; | |
| if (direction === "UP") head.y--; | |
| if (direction === "DOWN") head.y++; | |
| if (direction === "LEFT") head.x--; | |
| if (direction === "RIGHT") head.x++; | |
| // Wall collision | |
| if ( | |
| head.x < 0 || | |
| head.x >= gridSize || | |
| head.y < 0 || | |
| head.y >= gridSize | |
| ) { | |
| endGame(); | |
| return; | |
| } | |
| // Snake collision | |
| for (let i = 0; i < snake.length; i++) { | |
| if ( | |
| head.x === snake[i].x && | |
| head.y === snake[i].y | |
| ) { | |
| endGame(); | |
| return; | |
| } | |
| } | |
| snake.unshift(head); | |
| // Food collision | |
| if ( | |
| head.x === food.x && | |
| head.y === food.y | |
| ) { | |
| score++; | |
| scoreElement.textContent = score; | |
| if (score > highScore) { | |
| highScore = score; | |
| highScoreElement.textContent = highScore; | |
| localStorage.setItem( | |
| "snakeHighScore", | |
| highScore | |
| ); | |
| } | |
| createFood(); | |
| } else { | |
| snake.pop(); | |
| } | |
| drawGame(); | |
| } | |
| function createFood() { | |
| let validPosition = false; | |
| while (!validPosition) { | |
| food = { | |
| x: Math.floor(Math.random() * gridSize), | |
| y: Math.floor(Math.random() * gridSize) | |
| }; | |
| validPosition = !snake.some( | |
| segment => | |
| segment.x === food.x && | |
| segment.y === food.y | |
| ); | |
| } | |
| } | |
| function drawGame() { | |
| // Background | |
| ctx.fillStyle = "#020617"; | |
| ctx.fillRect(0, 0, canvas.width, canvas.height); | |
| // Grid | |
| ctx.strokeStyle = "rgba(148, 163, 184, 0.08)"; | |
| ctx.lineWidth = 1; | |
| for (let i = 0; i <= gridSize; i++) { | |
| const position = i * tileSize; | |
| ctx.beginPath(); | |
| ctx.moveTo(position, 0); | |
| ctx.lineTo(position, canvas.height); | |
| ctx.stroke(); | |
| ctx.beginPath(); | |
| ctx.moveTo(0, position); | |
| ctx.lineTo(canvas.width, position); | |
| ctx.stroke(); | |
| } | |
| // Food | |
| ctx.fillStyle = "#ef4444"; | |
| ctx.beginPath(); | |
| ctx.arc( | |
| food.x * tileSize + tileSize / 2, | |
| food.y * tileSize + tileSize / 2, | |
| tileSize * 0.35, | |
| 0, | |
| Math.PI * 2 | |
| ); | |
| ctx.fill(); | |
| // Snake | |
| snake.forEach((segment, index) => { | |
| ctx.fillStyle = | |
| index === 0 ? "#4ade80" : "#22c55e"; | |
| const padding = 2; | |
| ctx.beginPath(); | |
| ctx.roundRect( | |
| segment.x * tileSize + padding, | |
| segment.y * tileSize + padding, | |
| tileSize - padding * 2, | |
| tileSize - padding * 2, | |
| 5 | |
| ); | |
| ctx.fill(); | |
| }); | |
| // Eyes | |
| if (snake.length > 0) { | |
| const head = snake[0]; | |
| ctx.fillStyle = "#052e16"; | |
| let eye1X = head.x * tileSize + tileSize * 0.35; | |
| let eye2X = head.x * tileSize + tileSize * 0.65; | |
| let eyeY = head.y * tileSize + tileSize * 0.35; | |
| if (direction === "LEFT") { | |
| eye1X = eye2X = head.x * tileSize + tileSize * 0.3; | |
| eyeY = head.y * tileSize + tileSize * 0.35; | |
| } | |
| if (direction === "RIGHT") { | |
| eye1X = eye2X = head.x * tileSize + tileSize * 0.7; | |
| eyeY = head.y * tileSize + tileSize * 0.35; | |
| } | |
| if (direction === "UP") { | |
| eye1X = head.x * tileSize + tileSize * 0.35; | |
| eye2X = head.x * tileSize + tileSize * 0.65; | |
| eyeY = head.y * tileSize + tileSize * 0.3; | |
| } | |
| if (direction === "DOWN") { | |
| eye1X = head.x * tileSize + tileSize * 0.35; | |
| eye2X = head.x * tileSize + tileSize * 0.65; | |
| eyeY = head.y * tileSize + tileSize * 0.7; | |
| } | |
| ctx.beginPath(); | |
| ctx.arc(eye1X, eyeY, 3, 0, Math.PI * 2); | |
| ctx.fill(); | |
| ctx.beginPath(); | |
| ctx.arc(eye2X, eyeY, 3, 0, Math.PI * 2); | |
| ctx.fill(); | |
| } | |
| } | |
| function endGame() { | |
| gameRunning = false; | |
| gameOver = true; | |
| clearInterval(gameLoop); | |
| gameLoop = null; | |
| messageElement.textContent = | |
| "💥 Game Over! Your score: " + score; | |
| drawGame(); | |
| // Game-over overlay | |
| ctx.fillStyle = "rgba(2, 6, 23, 0.65)"; | |
| ctx.fillRect(0, 0, canvas.width, canvas.height); | |
| ctx.fillStyle = "#ffffff"; | |
| ctx.font = "bold 42px Arial"; | |
| ctx.textAlign = "center"; | |
| ctx.fillText( | |
| "GAME OVER", | |
| canvas.width / 2, | |
| canvas.height / 2 - 10 | |
| ); | |
| ctx.font = "20px Arial"; | |
| ctx.fillText( | |
| "Score: " + score, | |
| canvas.width / 2, | |
| canvas.height / 2 + 35 | |
| ); | |
| } | |
| // Initial screen | |
| resetGame(); | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| def create_app(): | |
| with gr.Blocks( | |
| title="Snake Game", | |
| theme=gr.themes.Soft() | |
| ) as demo: | |
| gr.HTML(HTML) | |
| return demo | |
| demo = create_app() | |
| if __name__ == "__main__": | |
| demo.launch() |