-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_manager.py
68 lines (49 loc) · 1.68 KB
/
game_manager.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
import consts
from cell import Cell
class GameManager:
def __init__(self, size, screen, sx, sy, block_cells):
self.screen = screen
self.size = size
self.cells = []
self.sx = sx
self.sy = sy
self.snakes = list()
self.turn = 0
for i in range(self.size):
tmp = []
for j in range(self.size):
tmp.append(Cell(screen, sx + i * consts.cell_size, sy + j * consts.cell_size))
self.cells.append(tmp)
for cell in block_cells:
self.get_cell(cell).set_color(consts.block_color)
def add_snake(self, snake):
self.snakes.append(snake)
def get_cell(self, pos):
try:
return self.cells[pos[0]][pos[1]]
except:
return None
def kill(self, killed_snake):
self.snakes.remove(killed_snake)
def get_next_fruit_pos(self):
ret = -1, -1
mx = -100
for i in range(0, self.size):
for j in range(0, self.size):
mn = 100000000
for x in range(0, self.size):
for y in range(0, self.size):
if self.get_cell((x, y)).color != consts.back_color:
mn = min(mn, int( abs(x-i) + abs(y-j) ))
if mn > mx:
mx = mn
ret = i, j
return ret
def handle(self, keys):
for snake in self.snakes:
snake.handle(keys)
for snake in self.snakes:
snake.next_move()
self.turn += 1
if self.turn%10 == 0:
self.get_cell(self.get_next_fruit_pos()).set_color(consts.fruit_color)