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