forked from jmndoza/snake-pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (49 loc) · 1.45 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
"""
Snake Eater
Made with PyGame
"""
import pygame, sys
from elements import player, supplies, label
from core import tools, prepare
from themes.normal import *
# Difficulty settings
# Easy -> 10
# Medium -> 25
# Hard -> 40
# Harder -> 60
# Impossible-> 120
difficulty = 25
game_over_label = label.GameOverText(prepare.game_window, prepare.WINSIZE)
# Game variables
snake = player.Snake()
food = supplies.Pellet()
pencil = tools.Pencil(prepare.game_window)
# Main logic
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Whenever a key is pressed down
elif event.type == pygame.KEYDOWN:
snake.set_change(event)
# Making sure the snake cannot move in the opposite direction instantaneously
snake.set_direction()
# Moving the snake and grow
snake.update(food)
# Spawning food on the screen
if not food.active:
food = supplies.Pellet()
# GFX
prepare.game_window.fill(black)
# Snake body
pencil.draw(snake.body, green)
# Snake food
pencil.draw(food, white)
# Game Over conditions
if snake.body_collision() or snake.border_collision(*prepare.WINSIZE):
game_over_label.draw(prepare.game_window)
# Refresh game screen
pygame.display.update()
# Refresh rate
prepare.clock.tick(difficulty)