-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
92 lines (70 loc) · 2.99 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
83
84
85
86
87
88
89
90
91
92
import pygame
import sys
import random
import config
def update_position(snake, direction, step):
if direction == "UP":
snake = [snake[0], snake[1] - step]
if direction == "DOWN":
snake = [snake[0], snake[1] + step]
if direction == "LEFT":
snake = [snake[0] - step, snake[1]]
if direction == "RIGHT":
snake = [snake[0] + step, snake[1]]
return snake
def update_direction(direction, keys):
if keys[pygame.K_LEFT]:
return "LEFT" if direction != "RIGHT" else direction
if keys[pygame.K_RIGHT]:
return "RIGHT" if direction != "LEFT" else direction
if keys[pygame.K_UP]:
return "UP" if direction != "DOWN" else direction
if keys[pygame.K_DOWN]:
return "DOWN" if direction != "UP" else direction
return direction
def is_out(snake, game_res):
if snake[0] < 0 or snake[1] < 0 or snake[0] > game_res[0] or snake[1] > game_res[1]:
return True
return False
def end_game(window):
print("Game over.")
window.fill(config.BACKGROUND_COLOR)
pygame.quit()
sys.exit()
def generate_apple(game_res, snake_size):
x = random.choice(range(0, game_res[0] - snake_size + 1, snake_size))
y = random.choice(range(0, game_res[1] - snake_size + 1, snake_size))
return [x, y]
def is_collision(snake, apple):
if snake[0] == apple[0] and snake[1] == apple[1]:
return True
return False
if __name__ == "__main__":
pygame.init() ## inicializuje pygame
clock = pygame.time.Clock()
window = pygame.display.set_mode(config.GAME_RES)
snake = [[config.GAME_RES[0] // 2, config.GAME_RES[1] // 2]]
apple = generate_apple(config.GAME_RES, config.SNAKE_SIZE)
direction = "UP"
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
direction = update_direction(direction, keys)
new_position = update_position(snake[0], direction, config.SNAKE_SIZE)
snake.insert(0, new_position) ## pozícia, prvok
if is_collision(snake[0], apple):
print("Apple eaten!")
apple = generate_apple(config.GAME_RES, config.SNAKE_SIZE)
else:
snake.pop() ## zmaze posledny clanok
if is_out(snake[0], config.GAME_RES): ## snake[0] je akoze hlava
end_game(window)
for part in snake:
pygame.draw.rect(window, config.BODY_COLOR, pygame.Rect(part[0], part[1], config.SNAKE_SIZE, config.SNAKE_SIZE))
pygame.draw.rect(window, config.APPLE_COLOR, pygame.Rect(apple[0], apple[1], config.SNAKE_SIZE, config.SNAKE_SIZE))
pygame.display.update()
window.fill(config.BACKGROUND_COLOR)
clock.tick(config.GAME_FPS)