-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
82 lines (68 loc) · 2.04 KB
/
main.py
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
import pygame
from grid import *
from snake import *
from apple import *
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# initialize pygame
pygame.init()
screen_size = (700, 500)
# create a window
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("Snake")
# clock is used to set a max fps
clock = pygame.time.Clock()
grid = Grid(50, screen, RED)
snake = Snake(50, screen, WHITE)
apple = Apple(25, 50, screen, GREEN)
pygame.mixer.music.load("./assets/bgm2.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)
running = True
paused = False
while running:
print("loop")
# event handling
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
snake.set_direction(Direction.LEFT)
elif event.key == pygame.K_RIGHT:
snake.set_direction(Direction.RIGHT)
elif event.key == pygame.K_UP:
snake.set_direction(Direction.UP)
elif event.key == pygame.K_DOWN:
snake.set_direction(Direction.DOWN)
# cheat codes
elif event.key == pygame.K_a:
apple.reset()
elif event.key == pygame.K_p:
paused = not paused
if paused:
pygame.mixer.music.pause()
continue
else:
pygame.mixer.music.play(-1)
if event.type == pygame.QUIT:
running = False
if not paused:
# game logic
snake.move()
if snake.out_of_bounds():
snake.die("Out of bounds!")
elif snake.eating_self():
snake.die("Collision with self!")
elif snake.eating_apple(apple):
apple.reset()
snake.eat()
# drawing logic
screen.fill(BLACK)
grid.draw()
snake.draw()
apple.draw()
pygame.display.flip()
# how many updates per second
clock.tick(1)
pygame.quit()