Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,101 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 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()
|
|
|