prathamt commited on
Commit
89ef29a
·
verified ·
1 Parent(s): 407e255

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -101
app.py CHANGED
@@ -1,101 +1,108 @@
1
- import pygame
2
- import random
3
-
4
- # Init
5
- pygame.init()
6
-
7
- # Screen
8
- WIDTH, HEIGHT = 600, 600
9
- screen = pygame.display.set_mode((WIDTH, HEIGHT))
10
- pygame.display.set_caption("Snake Game")
11
-
12
- # Colors
13
- BLACK = (0,0,0)
14
- GREEN = (0,255,0)
15
- RED = (255,0,0)
16
- WHITE = (255,255,255)
17
-
18
- # Snake settings
19
- snake_block = 20
20
- clock = pygame.time.Clock()
21
-
22
- font = pygame.font.SysFont("Arial", 30)
23
-
24
- def draw_snake(snake):
25
- for block in snake:
26
- pygame.draw.rect(screen, GREEN, [block[0], block[1], snake_block, snake_block])
27
-
28
- def show_score(score):
29
- value = font.render(f"Score: {score}", True, WHITE)
30
- screen.blit(value, [10, 10])
31
-
32
- def game_loop():
33
- x = WIDTH // 2
34
- y = HEIGHT // 2
35
-
36
- dx = 0
37
- dy = 0
38
-
39
- snake = []
40
- length = 1
41
-
42
- food_x = random.randrange(0, WIDTH, snake_block)
43
- food_y = random.randrange(0, HEIGHT, snake_block)
44
-
45
- running = True
46
-
47
- while running:
48
- for event in pygame.event.get():
49
- if event.type == pygame.QUIT:
50
- running = False
51
-
52
- if event.type == pygame.KEYDOWN:
53
- if event.key == pygame.K_LEFT:
54
- dx = -snake_block
55
- dy = 0
56
- elif event.key == pygame.K_RIGHT:
57
- dx = snake_block
58
- dy = 0
59
- elif event.key == pygame.K_UP:
60
- dy = -snake_block
61
- dx = 0
62
- elif event.key == pygame.K_DOWN:
63
- dy = snake_block
64
- dx = 0
65
-
66
- x += dx
67
- y += dy
68
-
69
- # Game over conditions
70
- if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
71
- running = False
72
-
73
- screen.fill(BLACK)
74
-
75
- pygame.draw.rect(screen, RED, [food_x, food_y, snake_block, snake_block])
76
-
77
- snake.append([x, y])
78
- if len(snake) > length:
79
- del snake[0]
80
-
81
- # Self collision
82
- for block in snake[:-1]:
83
- if block == [x, y]:
84
- running = False
85
-
86
- draw_snake(snake)
87
- show_score(length - 1)
88
-
89
- pygame.display.update()
90
-
91
- # Eat food
92
- if x == food_x and y == food_y:
93
- food_x = random.randrange(0, WIDTH, snake_block)
94
- food_y = random.randrange(0, HEIGHT, snake_block)
95
- length += 1
96
-
97
- clock.tick(10)
98
-
99
- pygame.quit()
100
-
101
- game_loop()
 
 
 
 
 
 
 
 
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)