-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (42 loc) · 1.3 KB
/
main.py
File metadata and controls
52 lines (42 loc) · 1.3 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
import sys, pygame
from pygame.event import Event
from assets.entity import Snake, Food
def main():
SIZE = WIDTH, HEIGHT = 640, 480
BLACK = 10, 10, 10
SCREEN = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
snake = Snake()
food = Food(320, 320)
pygame.init()
MOVE_EVENT = Event(666)
pygame.time.set_timer(MOVE_EVENT, 150)
gameover = False
snake_increment = False
snake_collision = False
while not gameover:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameover = True
if event == MOVE_EVENT:
is_move_possible = snake.move(SCREEN)
if not is_move_possible:
gameover = True
snake_collision = snake.has_collision(*snake.body[1:])
snake_increment = snake.has_collision(food.body)
if snake_collision:
gameover = True
if snake_increment:
snake.increment()
snake_increment = False
food.reposition(SCREEN, *snake.body)
food.draw(SCREEN)
snake.draw(SCREEN)
snake.key_handler()
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()