-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (39 loc) · 1.2 KB
/
Copy pathmain.py
File metadata and controls
60 lines (39 loc) · 1.2 KB
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
import pygame
from src.settings import *
from src.snake import Snake
from src.food import Food
pygame.init()
font = pygame.font.SysFont("Arial", 28)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
snake = Snake()
food = Food()
score = 0
running = True
while running:
for event in pygame.event.get():
snake.move()
if snake .body[0] == food.position:
snake.eat()
food.randomize(snake.body)
score += 1
score_text = font.render(f"Score: {score}", True, TEXT)
screen.blit(score_text, (15, 15))
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction((0, -1))
elif event.key == pygame.K_DOWN:
snake.change_direction((0, 1))
elif event.key == pygame.K_LEFT:
snake.change_direction((-1, 0))
elif event.key == pygame.K_RIGHT:
snake.change_direction((1, 0))
screen.fill(BACKGROUND)
snake.draw(screen)
food.draw(screen)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()