-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
49 lines (41 loc) · 1.3 KB
/
snake.py
File metadata and controls
49 lines (41 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
import pygame
import sys
from objects import Snake
from objects import Food
import settings as sett
import screen_modifications as sm
def GameLoop():
errors = pygame.init()[1]
if errors > 0:
print(f"[!] Game had {errors} when starting, exiting...")
sys.exit(-1)
else:
print("[+] Successfully started game")
fps_controller = pygame.time.Clock()
game_window = pygame.display.set_mode(
(sett.SCREEN_WIDTH, sett.SCREEN_HEIGHT), 0, 32
)
pygame.display.set_caption("Snake game")
surface = pygame.Surface(game_window.get_size())
surface = surface.convert()
sm.title_screen(game_window)
sm.draw_grid(surface)
snake = Snake()
food = Food(snake.get_head_position())
while True:
fps_controller.tick(sett.FPS_MAX)
snake.handle_keys()
sm.draw_grid(surface)
gaming = snake.move()
if not gaming:
sm.game_over_screen(game_window, snake)
else:
if snake.get_head_position() == food.position:
snake.addCube(food)
pygame.display.set_caption(
f"Snake game | Score: {snake.score}")
snake.draw(surface)
food.draw(surface)
game_window.blit(surface, (0, 0))
pygame.display.update()
GameLoop()