Update app.py
Browse files
app.py
CHANGED
|
@@ -1,101 +1,108 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
html_code = """
|
| 4 |
+
<!DOCTYPE html>
|
| 5 |
+
<html>
|
| 6 |
+
<head>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
background: black;
|
| 10 |
+
color: white;
|
| 11 |
+
text-align: center;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
canvas {
|
| 15 |
+
background: #111;
|
| 16 |
+
border: 2px solid #0f0;
|
| 17 |
+
margin-top: 10px;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
.controls button {
|
| 21 |
+
width: 60px;
|
| 22 |
+
height: 60px;
|
| 23 |
+
margin: 5px;
|
| 24 |
+
font-size: 20px;
|
| 25 |
+
}
|
| 26 |
+
</style>
|
| 27 |
+
</head>
|
| 28 |
+
|
| 29 |
+
<body>
|
| 30 |
+
|
| 31 |
+
<h2>🐍 Snake Game</h2>
|
| 32 |
+
<canvas id="game" width="300" height="300"></canvas>
|
| 33 |
+
<p>Score: <span id="score">0</span></p>
|
| 34 |
+
|
| 35 |
+
<div class="controls">
|
| 36 |
+
<br>
|
| 37 |
+
<button onclick="setDir(0,-20)">⬆️</button><br>
|
| 38 |
+
<button onclick="setDir(-20,0)">⬅️</button>
|
| 39 |
+
<button onclick="setDir(20,0)">➡️</button><br>
|
| 40 |
+
<button onclick="setDir(0,20)">⬇️</button>
|
| 41 |
+
</div>
|
| 42 |
+
|
| 43 |
+
<script>
|
| 44 |
+
let canvas = document.getElementById("game");
|
| 45 |
+
let ctx = canvas.getContext("2d");
|
| 46 |
+
|
| 47 |
+
let snake = [{x:150,y:150}];
|
| 48 |
+
let dx = 20, dy = 0;
|
| 49 |
+
let food = {x:100,y:100};
|
| 50 |
+
let score = 0;
|
| 51 |
+
|
| 52 |
+
document.addEventListener("keydown", changeDirection);
|
| 53 |
+
|
| 54 |
+
function changeDirection(event){
|
| 55 |
+
if(event.key==="ArrowUp" && dy===0){dx=0;dy=-20;}
|
| 56 |
+
if(event.key==="ArrowDown" && dy===0){dx=0;dy=20;}
|
| 57 |
+
if(event.key==="ArrowLeft" && dx===0){dx=-20;dy=0;}
|
| 58 |
+
if(event.key==="ArrowRight" && dx===0){dx=20;dy=0;}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
function setDir(x,y){
|
| 62 |
+
dx = x;
|
| 63 |
+
dy = y;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
function draw(){
|
| 67 |
+
ctx.fillStyle="#111";
|
| 68 |
+
ctx.fillRect(0,0,300,300);
|
| 69 |
+
|
| 70 |
+
ctx.fillStyle="#0f0";
|
| 71 |
+
snake.forEach(s=>{
|
| 72 |
+
ctx.fillRect(s.x,s.y,20,20);
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
+
ctx.fillStyle="red";
|
| 76 |
+
ctx.fillRect(food.x,food.y,20,20);
|
| 77 |
+
|
| 78 |
+
let head = {x: snake[0].x + dx, y: snake[0].y + dy};
|
| 79 |
+
snake.unshift(head);
|
| 80 |
+
|
| 81 |
+
if(head.x===food.x && head.y===food.y){
|
| 82 |
+
score++;
|
| 83 |
+
document.getElementById("score").innerText = score;
|
| 84 |
+
food = {
|
| 85 |
+
x: Math.floor(Math.random()*15)*20,
|
| 86 |
+
y: Math.floor(Math.random()*15)*20
|
| 87 |
+
};
|
| 88 |
+
} else {
|
| 89 |
+
snake.pop();
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
if(head.x<0 || head.y<0 || head.x>=300 || head.y>=300){
|
| 93 |
+
alert("Game Over! Score: " + score);
|
| 94 |
+
location.reload();
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
setInterval(draw, 120);
|
| 99 |
+
</script>
|
| 100 |
+
|
| 101 |
+
</body>
|
| 102 |
+
</html>
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
with gr.Blocks() as demo:
|
| 106 |
+
gr.HTML(html_code)
|
| 107 |
+
|
| 108 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|