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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -102
app.py CHANGED
@@ -1,102 +1,101 @@
1
- html_code = """
2
- <!DOCTYPE html>
3
- <html>
4
- <head>
5
- <style>
6
- body {
7
- background: black;
8
- color: white;
9
- text-align: center;
10
- }
11
-
12
- canvas {
13
- background: #111;
14
- border: 2px solid #0f0;
15
- margin-top: 10px;
16
- }
17
-
18
- .controls button {
19
- width: 60px;
20
- height: 60px;
21
- margin: 5px;
22
- font-size: 20px;
23
- }
24
- </style>
25
- </head>
26
-
27
- <body>
28
-
29
- <h2>🐍 Snake Game</h2>
30
- <button onclick="startGame()">Start Game</button>
31
-
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, dx, dy, food, score, game;
48
-
49
- function startGame(){
50
- snake = [{x:150,y:150}];
51
- dx = 20;
52
- dy = 0;
53
- score = 0;
54
-
55
- food = {
56
- x: Math.floor(Math.random()*15)*20,
57
- y: Math.floor(Math.random()*15)*20
58
- };
59
-
60
- clearInterval(game);
61
- game = setInterval(draw, 120);
62
- }
63
-
64
- function setDir(x,y){
65
- dx = x;
66
- dy = y;
67
- }
68
-
69
- function draw(){
70
- ctx.fillStyle = "#111";
71
- ctx.fillRect(0,0,300,300);
72
-
73
- ctx.fillStyle = "#0f0";
74
- snake.forEach(s => ctx.fillRect(s.x,s.y,20,20));
75
-
76
- ctx.fillStyle = "red";
77
- ctx.fillRect(food.x,food.y,20,20);
78
-
79
- let head = {x: snake[0].x + dx, y: snake[0].y + dy};
80
- snake.unshift(head);
81
-
82
- if(head.x === food.x && head.y === food.y){
83
- score++;
84
- document.getElementById("score").innerText = score;
85
- food = {
86
- x: Math.floor(Math.random()*15)*20,
87
- y: Math.floor(Math.random()*15)*20
88
- };
89
- } else {
90
- snake.pop();
91
- }
92
-
93
- if(head.x<0 || head.y<0 || head.x>=300 || head.y>=300){
94
- alert("Game Over! Score: " + score);
95
- clearInterval(game);
96
- }
97
- }
98
- </script>
99
-
100
- </body>
101
- </html>
102
- """
 
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()