-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (46 loc) · 1.39 KB
/
main.py
File metadata and controls
59 lines (46 loc) · 1.39 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
import pygame
import time
pygame.init()
from classes.snake import Snake
from classes.food import Food
from modules.constants import *
from modules.colors import *
pygame.display.set_caption('Snake')
clock = pygame.time.Clock()
snake = Snake()
current_food = Food()
current_food.new_position(snake.body)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
snake.h_vel = 1
snake.v_vel = 0
elif keys[pygame.K_LEFT]:
snake.h_vel = -1
snake.v_vel = 0
elif keys[pygame.K_UP]:
snake.h_vel = 0
snake.v_vel = -1
elif keys[pygame.K_DOWN]:
snake.h_vel = 0
snake.v_vel = 1
SCREEN.fill(COLOR_DARK)
if len(snake.body) < 2:
snake.move()
else:
del snake.body[-1]
snake.body.insert(1, [snake.head[0], snake.head[1]])
snake.move()
running = snake.check_if_alive()
current_food.check_for_collision(snake)
snake.draw()
pygame.draw.rect(SCREEN, COLOR_SECONDARY, pygame.Rect(current_food.x * TILE_SIZE, current_food.y * TILE_SIZE, TILE_SIZE, TILE_SIZE))
pygame.display.update()
clock.tick(1 / WAIT_TIME)
pygame.mixer.music.load('sound/death.wav')
pygame.mixer.music.play(0)
time.sleep(1)