-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
94 lines (85 loc) · 3.6 KB
/
model.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
93
94
import random
from objects.snake import *
from objects.vector import *
from objects.map_parser import *
import time
import pygame
# noinspection PyTypeChecker
sym_by_direction = dict([[vector_zero, '*'],
[vector_left, '←'],
[vector_right, '→'],
[vector_up, '↓'],
[vector_down, '↑']])
level0 = [' ' * 5] * 5
level1 = [' ',
' #### ',
' # ',
' # ',
' ',
' ',
' # ',
' ### ',
' # ',
' ### ',
' # ## ',
' #### ',
' ']
class GameEngine:
def __init__(self):
self.map = parse_map(level1)
self.map_size = [len(self.map[0]), len(self.map)]
self.snake = Snake(self, Vector(1, 1))
self.view = None
self.stop_flag = False
self.pause_flag = False
self.empty_cells = set()
for x in range(self.map_size[0]):
for y in range(self.map_size[1]):
if self.map[y][x] == MapCell.empty:
self.empty_cells.add(Vector(x, y))
self.spawn_food()
self.spawn_food()
self.spawn_food()
def update(self):
self.snake.move()
def update_map(self, updates):
for upd in updates:
x, y = upd[0].x, upd[0].y
self.map[y][x] = upd[1]
def draw(self, dir_flag=False):
output = [[' '] * self.map_size[0] for _ in range(self.map_size[1])]
for y in range(self.map_size[1]):
for x in range(self.map_size[0]):
output[y][x] = sym_by_mapcell[self.map[y][x]]
for s in self.snake.get_segmets():
if dir_flag:
output[s.pos.y][s.pos.x] = sym_by_direction[s.dir]
else:
output[s.pos.y][s.pos.x] = sym_by_mapcell[s.segment_type]
fin_output = ""
for line in output:
fin_output += ' '.join(line) + "\n"
print(fin_output)
def spawn_food(self):
self.empty_cells.add(self.snake.head.pos)
not_available_cells = set()
for seg in self.snake.get_segmets():
not_available_cells.add(seg.pos)
available_cells = self.empty_cells - not_available_cells
if self.empty_cells:
spawn_pos = list(available_cells)[random.randrange(0, len(available_cells))]
self.map[spawn_pos.y][spawn_pos.x] = [MapCell.food,
MapCell.food,
MapCell.food,
MapCell.food,
MapCell.speed_food,
MapCell.neg_speed_food][random.randrange(0, 6)]
def run(self):
while not self.stop_flag:
if not self.pause_flag:
pygame.time.delay(self.snake.move_delays[self.snake.move_delay_index])
self.update()
self.view.update()
# self.draw() # debug
if self.snake.health <= 0:
self.view.game_over()