rev |
line source |
martiran@12
|
1 import random as rnd |
martiran@15
|
2 import Tkinter as tk |
Alex@66
|
3 import snake |
martiran@12
|
4 |
martiran@6
|
5 directions = [(0,1), (1,0), (0,-1), (-1,0)] |
martiran@46
|
6 tm = [[0, -1], [1, 0]] |
martiran@1
|
7 |
martiran@5
|
8 class Cell(object): |
martiran@15
|
9 def __init__(self, x, y, canvas = None): |
martiran@1
|
10 self.x = x |
martiran@1
|
11 self.y = y |
martiran@1
|
12 self.canvas = canvas |
martiran@1
|
13 self.snake = None |
martiran@28
|
14 self.type = 'empty' |
martiran@1
|
15 return |
martiran@6
|
16 def redraw(self): |
martiran@54
|
17 field_size = min(self.canvas.winfo_height(), self.canvas.winfo_width()) |
martiran@55
|
18 offset = (self.canvas.winfo_width() - field_size, self.canvas.winfo_height() - field_size) |
martiran@15
|
19 if self.type == 'wall': |
martiran@15
|
20 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="grey") |
martiran@15
|
21 pass |
martiran@15
|
22 elif self.type == 'empty': |
martiran@15
|
23 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill="black") |
martiran@15
|
24 pass |
martiran@15
|
25 elif self.type == 'body': |
martiran@15
|
26 self.canvas.create_rectangle(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color) |
martiran@15
|
27 pass |
martiran@15
|
28 elif self.type == 'head': |
martiran@15
|
29 self.canvas.create_oval(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1] + self.y*field_size/21.0, fill=self.snake.color) |
martiran@15
|
30 pass |
martiran@15
|
31 elif self.type == 'tail': |
martiran@15
|
32 self.canvas.create_polygon(offset[0], offset[1], offset[0] + self.x*field_size/21.0, offset[1], offset[0] + self.x*field_size/(2*21.0), offset[1] + self.y*field_size/21.0, fill=self.snake.color) |
martiran@15
|
33 pass |
martiran@15
|
34 return |
martiran@6
|
35 def __eq__(self, pattern): |
martiran@52
|
36 if pattern.type == 'any': |
martiran@6
|
37 return True |
martiran@52
|
38 if pattern.type != self.type: |
martiran@6
|
39 return False |
martiran@52
|
40 if pattern.snake_type == 'my' and pattern.snake != self.snake: |
martiran@52
|
41 return False |
martiran@52
|
42 elif pattern.snake_type == 'enemy' and pattern.snake == self.snake: |
martiran@52
|
43 return False |
martiran@52
|
44 return True |
martiran@6
|
45 def clear(self): |
martiran@1
|
46 self.snake = None |
martiran@1
|
47 self.type = 'empty' |
martiran@1
|
48 return |
martiran@1
|
49 |
martiran@2
|
50 |
martiran@5
|
51 class Engine(object): |
martiran@6
|
52 def __init__(self, canvas): |
martiran@1
|
53 self.canvas = canvas |
martiran@54
|
54 self.w = min(canvas.winfo_height(), canvas.winfo_width()) |
martiran@54
|
55 self.h = min(canvas.winfo_height(), canvas.winfo_width()) |
martiran@12
|
56 self.snakes = [None, None, None, None] |
martiran@9
|
57 self.init_field() |
martiran@9
|
58 return |
martiran@9
|
59 def init_field (self): |
martiran@9
|
60 self.field = {} |
martiran@9
|
61 for x in range(21): |
martiran@9
|
62 for y in range(21): |
martiran@9
|
63 self.field[x, y] = Cell(x, y, self.canvas) |
martiran@9
|
64 for y in range(21): |
martiran@9
|
65 self.field[0, y].type = 'wall' |
martiran@9
|
66 self.field[20, y].type = 'wall' |
martiran@9
|
67 for x in range(1,20): |
martiran@9
|
68 self.field[x, 0].type = 'wall' |
martiran@9
|
69 self.field[x, 20].type = 'wall' |
Alex@93
|
70 self.redraw() |
martiran@1
|
71 return |
martiran@6
|
72 def step(self): |
Alex@62
|
73 for i, snake in enumerate(self.snakes): |
Alex@60
|
74 if len(snake.cells) == 0: |
Alex@62
|
75 self.snakes[i] = None |
martiran@12
|
76 if snake == None: |
martiran@12
|
77 pass |
martiran@12
|
78 else: |
Alex@66
|
79 self.legal_moves(snake) |
martiran@12
|
80 self.move_snake(snake) |
martiran@12
|
81 self.refill() |
martiran@9
|
82 self.redraw() |
martiran@9
|
83 return |
martiran@9
|
84 def move_snake(self, snake): |
martiran@12
|
85 applied_dir = None |
martiran@12
|
86 for rule in snake.rules: |
martiran@12
|
87 if applied_dir != None: |
martiran@12
|
88 choose_dir = [] |
martiran@12
|
89 for direction in snake.legal_dir: |
martiran@12
|
90 rule.direction = direction |
martiran@12
|
91 if rule.applies() == True: |
martiran@12
|
92 choose_dir.append(direction) |
martiran@12
|
93 pass |
martiran@12
|
94 pass |
martiran@12
|
95 if len(choose_move) != 0: |
Alex@94
|
96 applied_dir = rnd.shuffle(choose_dir)[0] |
martiran@12
|
97 pass |
martiran@12
|
98 else: |
Alex@94
|
99 self.move_do(snake, applied_dir) |
martiran@12
|
100 break |
martiran@12
|
101 if applied_dir == None: |
Alex@94
|
102 applied_dir = snake.legal_dir[0] |
Alex@94
|
103 self.move_do(snake, applied_dir) |
martiran@12
|
104 pass |
martiran@14
|
105 return |
Alex@94
|
106 def move_do(self, snake, applied_dir): |
Alex@94
|
107 dir_cell = self.field[snake.cells[0].y + applied_dir[0], snake.cells[0].x + applied_dir[1]] |
Alex@90
|
108 if dir_cell.type == 'empty': |
Alex@90
|
109 snake.cells.insert(0,dir_cell) |
Alex@90
|
110 del snake.cells[-1] |
Alex@90
|
111 pass |
Alex@90
|
112 elif (dir_cell.type == 'tail' and dir_cell.snake != snake): |
Alex@90
|
113 snake.cells.insert(0,dir_cell) |
Alex@90
|
114 del dir_cell.snake.cells[-1] |
Alex@90
|
115 pass |
Alex@90
|
116 |
martiran@6
|
117 def create_snake(self, snake_number): |
martiran@46
|
118 cells_id = [] |
martiran@46
|
119 for y in range(10): |
martiran@56
|
120 cells_id.append((10, y+1)) |
martiran@46
|
121 for rot_num in range(snake_number - 1): |
Alex@62
|
122 for i, cell in enumerate(cells_id): |
Alex@82
|
123 cells_id[i] = ((tm[0][0]*(cell[0]-10) + tm[0][1]*(cell[1]-10))+10,(tm[1][0]*(cell[0]-10) + tm[1][1]*(cell[1]-10))+10) |
martiran@46
|
124 cells = [] |
martiran@46
|
125 for cell in cells_id: |
Alex@60
|
126 cells.append(self.field[cell]) |
martiran@46
|
127 color_dic = { |
martiran@46
|
128 1:'blue', |
martiran@46
|
129 2:'green', |
martiran@46
|
130 3:'yellow', |
martiran@46
|
131 4:'red',} |
martiran@46
|
132 self.snakes[snake_number-1] = snake.Snake(cells, color_dic[snake_number]) |
Alex@77
|
133 return self.snakes[snake_number-1] |
martiran@6
|
134 def refill(self): |
martiran@12
|
135 for x in range(1,20): |
martiran@12
|
136 for y in range(1,20): |
martiran@12
|
137 self.field[x, y].type = 'empty' |
martiran@31
|
138 self.field[x, y].snake = None |
martiran@12
|
139 pass |
martiran@12
|
140 for snake in self.snakes: |
martiran@12
|
141 if snake == None: |
martiran@12
|
142 pass |
martiran@12
|
143 else: |
martiran@31
|
144 snake.fill() |
martiran@12
|
145 pass |
martiran@14
|
146 return |
martiran@9
|
147 def redraw(self): |
martiran@16
|
148 self.canvas.delete(all) |
martiran@32
|
149 for cell_coord in self.field: |
martiran@32
|
150 self.field[cell_coord].redraw() |
martiran@15
|
151 return |
martiran@6
|
152 def legal_moves(self, snake): |
martiran@6
|
153 snake.legal_dir = [] |
martiran@6
|
154 for direction in directions: |
martiran@17
|
155 dir_cell = self.field[snake.cells[0].y + direction[0], snake.cells[0].x + direction[1]] |
martiran@12
|
156 if (dir_cell.type == 'empty' or (dir_cell.type == 'tail' and dir_cell.snake != snake)): |
martiran@6
|
157 snake.legal_dir.append(direction) |
Alex@90
|
158 rnd.shuffle(snake.legal_dir) |
martiran@6
|
159 return |
martiran@6
|
160
|