Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.cmm.msu.su/trac/snake/browser/engine.py?rev=156
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 21:14:08 2016
Êîäèðîâêà: IBM-866
engine.py òÀÓ Python Battle

source: engine.py @ 156:7a4853ff834f

Revision 156:7a4853ff834f, 8.3 KB checked in by Alex Martynov, 5 years ago (diff)

added possible option old_snake for engine.create_snake()
anyway does not work properly

  • Property exe set to *
Lineˆà
1importˆàrandomˆàasˆàrnd
2importˆàTkinterˆàasˆàtk
3importˆàsnake
4
5directions =ˆà[(0,1),ˆà(1,0),ˆà(0,-1),ˆà(-1,0)]
6
7classˆàDict(dict):
8ˆà ˆà """Create a dictionary."""
9ˆà ˆà pass
10
11classˆàCell(object):
12ˆà ˆà """Cells.
13
14ˆà ˆà Atributes:
15ˆà ˆà - 'x' - absciss of the cell in field
16ˆà ˆà - 'y' - ordinate of the cell in field
17ˆà ˆà - 'canvas' - Widget the cell belongs to
18ˆà ˆà - 'snake' - snake the cell belongs to, possible values:
19ˆà ˆà ˆà ˆà snake
20ˆà ˆà ˆà ˆà None
21ˆà ˆà ˆà ˆà 'my'ˆà ˆà ˆà ˆà \
22ˆà ˆà ˆà ˆà 'enemy'ˆà ˆà ˆà/ˆà for patterns only
23
24ˆà ˆà - 'type' - type of the cell, possible values:
25ˆà ˆà ˆà ˆà 'empty'
26ˆà ˆà ˆà ˆà 'wall'
27ˆà ˆà ˆà ˆà 'body'
28ˆà ˆà ˆà ˆà 'head'
29ˆà ˆà ˆà ˆà 'tail'
30ˆà ˆà ˆà ˆà 'any'ˆà } for patterns only
31ˆà ˆà ˆà ˆà 'void' } for cells out of the field
32ˆà ˆà """
33ˆà ˆà defˆà__init__(self,ˆàx,ˆày,ˆàcanvas =ˆàNone):
34ˆà ˆà ˆà ˆà """Initialyze the cell with default parameters:
35ˆà ˆà ˆà ˆà ˆà ˆà type = 'empty'
36ˆà ˆà ˆà ˆà ˆà ˆà snake = None"""
37ˆà ˆà ˆà ˆà self.x =ˆàx
38ˆà ˆà ˆà ˆà self.y =ˆày
39ˆà ˆà ˆà ˆà self.canvas =ˆàcanvas
40ˆà ˆà ˆà ˆà self.snake =ˆàNone
41ˆà ˆà ˆà ˆà self.type =ˆà'empty'
42ˆà ˆà ˆà ˆà return
43ˆà ˆà
44ˆà ˆà defˆàredraw(self,ˆàoffset,ˆàc_size):
45ˆà ˆà ˆà ˆà """Draw a cell based on it content"""
46ˆà ˆà ˆà ˆà x0=offset[0]ˆà+ˆàself.x*c_size
47ˆà ˆà ˆà ˆà y0=offset[1]ˆà+ˆàself.y*c_size
48ˆà ˆà ˆà ˆà x1=offset[0]ˆà+ˆà(self.x+1)*c_size
49ˆà ˆà ˆà ˆà y1=offset[1]ˆà+ˆà(self.y+1)*c_size
50ˆà ˆà ˆà ˆà x2=offset[0]ˆà+ˆà(self.x+1/2.0)*c_size
51ˆà ˆà ˆà ˆà ifˆàself.type ==ˆà'wall':
52ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill="grey")
53ˆà ˆà ˆà ˆà ˆà ˆà pass
54ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'empty':
55ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill="black")
56ˆà ˆà ˆà ˆà ˆà ˆà pass
57ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'body':
58ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_rectangle(x0,ˆày0,ˆàx1,ˆày1,ˆàfill=self.snake.color)
59ˆà ˆà ˆà ˆà ˆà ˆà pass
60ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'head':
61ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_oval(x0,ˆày0,ˆàx1,ˆày1,ˆàfill=self.snake.color)
62ˆà ˆà ˆà ˆà ˆà ˆà pass
63ˆà ˆà ˆà ˆà elifˆàself.type ==ˆà'tail':
64ˆà ˆà ˆà ˆà ˆà ˆà self.canvas.create_polygon(x0,ˆày0,ˆàx1,ˆày0,ˆàx2,ˆày1,ˆàfill=self.snake.color)
65ˆà ˆà ˆà ˆà ˆà ˆà pass
66ˆà ˆà ˆà ˆà return
67ˆà ˆà
68ˆà ˆà defˆà__eq__(self,ˆàpattern):
69ˆà ˆà ˆà ˆà """Check the equaliation of the cell to the pattern cell."""
70ˆà ˆà ˆà ˆà ifˆàpattern.type ==ˆà'any':
71ˆà ˆà ˆà ˆà ˆà ˆà returnˆàTrue
72ˆà ˆà ˆà ˆà ifˆàpattern.type !=ˆàself.type:
73ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
74ˆà ˆà ˆà ˆà ifˆàpattern.snake_type ==ˆà'my'ˆàandˆàpattern.snake !=ˆàself.snake:
75ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
76ˆà ˆà ˆà ˆà elifˆàpattern.snake_type ==ˆà'enemy'ˆàandˆàpattern.snake ==ˆàself.snake:
77ˆà ˆà ˆà ˆà ˆà ˆà returnˆàFalse
78ˆà ˆà ˆà ˆà returnˆàTrue
79ˆà ˆà
80ˆà ˆà defˆà__ne__(self,ˆàpattern):
81ˆà ˆà ˆà ˆà """Check the discrepancy of the cell to the pattern cell."""
82ˆà ˆà ˆà ˆà returnˆànotˆàselfˆà==ˆàpattern
83ˆà ˆà
84ˆà ˆà defˆàclear(self):
85ˆà ˆà ˆà ˆà """Change the cell parameters back to default."""
86ˆà ˆà ˆà ˆà self.snake =ˆàNone
87ˆà ˆà ˆà ˆà self.type =ˆà'empty'
88ˆà ˆà ˆà ˆà return
89
90
91classˆàEngine(object):
92ˆà ˆà """Engine
93
94ˆà ˆà Atributes:
95
96ˆà ˆà - 'field' - game field:
97ˆà ˆà ˆà ˆà 'field.w' - width of the field count in cells
98ˆà ˆà ˆà ˆà 'field.h' - hight of the field count in cells
99ˆà ˆà - 'canvas' - Widget game field is showing on
100ˆà ˆà - 'snakes' - list of snakes loaded
101ˆà ˆà - 'psnakes' - list of snakes loaded in previous match, if other snakes are not loaded
102ˆà ˆà - 'start_snake_length' - starting length of the snake"""
103ˆà ˆà
104ˆà ˆà defˆà__init__(self,ˆàcanvas):
105ˆà ˆà ˆà ˆà """Initialyze the engine:
106ˆà ˆà ˆà ˆà start_snake_length = 10"""
107ˆà ˆà ˆà ˆà self.canvas =ˆàcanvas
108ˆà ˆà ˆà ˆà self.snakes =ˆà[None,ˆàNone,ˆàNone,ˆàNone]
109ˆà ˆà ˆà ˆà self.psnakes =ˆà[None,ˆàNone,ˆàNone,ˆàNone]
110ˆà ˆà ˆà ˆà self.init_field()
111ˆà ˆà ˆà ˆà self.start_snake_length =ˆà10
112ˆà ˆà ˆà ˆà return
113ˆà ˆà
114ˆà ˆà defˆàinit_fieldˆà(self):
115ˆà ˆà ˆà ˆà """Initialyze the field:
116ˆà ˆà ˆà ˆà width = 31
117ˆà ˆà ˆà ˆà hieght = 31
118ˆà ˆà ˆà ˆà perimeter is made by walls"""
119ˆà ˆà ˆà ˆà self.field =ˆàDict()
120ˆà ˆà ˆà ˆà self.field.w =ˆà31
121ˆà ˆà ˆà ˆà self.field.h =ˆà31
122ˆà ˆà ˆà ˆà f_w =ˆàself.field.w
123ˆà ˆà ˆà ˆà f_h =ˆàself.field.h
124ˆà ˆà ˆà ˆà forˆàx inˆàrange(f_w):
125ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(f_h):
126ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆày]ˆà=ˆàCell(x,ˆày,ˆàself.canvas)
127ˆà ˆà ˆà ˆà forˆày inˆàrange(f_h):
128ˆà ˆà ˆà ˆà ˆà ˆà self.field[0,ˆày].type =ˆà'wall'
129ˆà ˆà ˆà ˆà ˆà ˆà self.field[f_w-1,ˆày].type =ˆà'wall'
130ˆà ˆà ˆà ˆà forˆàx inˆàrange(1,f_w-1):
131ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆà0].type =ˆà'wall'
132ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆàf_h-1].type =ˆà'wall'
133ˆà ˆà ˆà ˆà self.refill()
134ˆà ˆà ˆà ˆà self.redraw()
135ˆà ˆà ˆà ˆà return
136ˆà ˆà
137ˆà ˆà defˆàstep(self):
138ˆà ˆà ˆà ˆà """Do the step of the game."""
139ˆà ˆà ˆà ˆà forˆài,ˆàsnake inˆàenumerate(self.snakes):
140ˆà ˆà ˆà ˆà ˆà ˆà ifˆàsnake !=ˆàNone:
141ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàlen(snake.cells)ˆà==ˆà0:
142ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.snakes[i]ˆà=ˆàNone
143ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà continue
144ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.legal_moves(snake)
145ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.move_snake(snake)
146ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.refill()
147ˆà ˆà ˆà ˆà self.redraw()ˆà ˆà
148ˆà ˆà ˆà ˆà return
149ˆà ˆà
150ˆà ˆà defˆàmove_snake(self,ˆàsnake):
151ˆà ˆà ˆà ˆà """Check of movement direction based on the snake rule list and actual
152ˆà ˆà ˆà ˆà enviroment."""
153ˆà ˆà ˆà ˆà head =ˆàsnake.cells[0]
154ˆà ˆà ˆà ˆà forˆàrule inˆàsnake.rules:
155ˆà ˆà ˆà ˆà ˆà ˆà forˆàdirection inˆàsnake.legal_dir:
156ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà rule.rotate(direction)
157ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ifˆàrule.applies(self.field,ˆàhead.x,ˆàhead.y)ˆà==ˆàTrue:
158ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.move_do(snake,ˆàdirection)
159ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà return
160ˆà ˆà ˆà ˆà ifˆàsnake.legal_dir !=ˆà[]:
161ˆà ˆà ˆà ˆà ˆà ˆà self.move_do(snake,ˆàsnake.legal_dir[0])
162ˆà ˆà ˆà ˆà ˆà ˆà pass
163ˆà ˆà ˆà ˆà return
164ˆà ˆà
165ˆà ˆà defˆàmove_do(self,ˆàsnake,ˆàapplied_dir):
166ˆà ˆà ˆà ˆà """Do the move of the snake."""
167ˆà ˆà ˆà ˆà head =ˆàsnake.cells[0]
168ˆà ˆà ˆà ˆà dir_cell =ˆàself.field[head.x +ˆàapplied_dir[0],ˆàhead.y +ˆàapplied_dir[1]]
169ˆà ˆà ˆà ˆà ifˆàdir_cell.type ==ˆà'empty':
170ˆà ˆà ˆà ˆà ˆà ˆà snake.cells.insert(0,dir_cell)
171ˆà ˆà ˆà ˆà ˆà ˆà delˆàsnake.cells[-1]
172ˆà ˆà ˆà ˆà ˆà ˆà pass
173ˆà ˆà ˆà ˆà elifˆà(dir_cell.type ==ˆà'tail'ˆàandˆàdir_cell.snake !=ˆàsnake):
174ˆà ˆà ˆà ˆà ˆà ˆà snake.cells.insert(0,dir_cell)
175ˆà ˆà ˆà ˆà ˆà ˆà delˆàdir_cell.snake.cells[-1]
176ˆà ˆà ˆà ˆà ˆà ˆà pass
177
178ˆà ˆà defˆàcreate_snake(self,ˆàsnake_number,ˆàold_snake =ˆàNone):
179ˆà ˆà ˆà ˆà """Create the snake:
180ˆà ˆà ˆà ˆà position choice is based on number or placement of 'Load' button
181ˆà ˆà ˆà ˆà ˆà ˆà snakes are placed with tails turned to the wall.
182ˆà ˆà ˆà ˆà color is chosen accorting to fen shui tradition"""
183ˆà ˆà ˆà ˆà cells_id =ˆà[]
184ˆà ˆà ˆà ˆà f_h =ˆàself.field.h
185ˆà ˆà ˆà ˆà f_w =ˆàself.field.w
186ˆà ˆà ˆà ˆà forˆày inˆàrange(self.start_snake_length):
187ˆà ˆà ˆà ˆà ˆà ˆà cells_id.insert(0,((f_w-1)/2,ˆày+1))
188ˆà ˆà ˆà ˆà forˆàrot_num inˆàrange(snake_number -ˆà1):
189ˆà ˆà ˆà ˆà ˆà ˆà forˆài,ˆàcell inˆàenumerate(cells_id):
190ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà cells_id[i]ˆà=ˆà(min(f_h,ˆàf_w)-1-cell[1],cell[0])
191ˆà ˆà ˆà ˆà cells =ˆà[]
192ˆà ˆà ˆà ˆà forˆàcell inˆàcells_id:
193ˆà ˆà ˆà ˆà ˆà ˆà cells.append(self.field[cell])
194ˆà ˆà ˆà ˆà color_dic =ˆà{
195ˆà ˆà ˆà ˆà ˆà ˆà 1:'blue',
196ˆà ˆà ˆà ˆà ˆà ˆà 2:'green',
197ˆà ˆà ˆà ˆà ˆà ˆà 3:'yellow',
198ˆà ˆà ˆà ˆà ˆà ˆà 4:'red',}
199ˆà ˆà ˆà ˆà ifˆàold_snake ==ˆàNone:
200ˆà ˆà ˆà ˆà ˆà ˆà self.snakes[snake_number-1]ˆà=ˆàsnake.Snake(cells,ˆàcolor_dic[snake_number])
201ˆà ˆà ˆà ˆà else:
202ˆà ˆà ˆà ˆà ˆà ˆà old_snake.cells =ˆàcells
203ˆà ˆà ˆà ˆà self.psnakes[snake_number-1]ˆà=ˆàself.snakes[snake_number-1]
204ˆà ˆà ˆà ˆà returnˆàself.snakes[snake_number-1]
205ˆà ˆà
206ˆà ˆà defˆàrefill(self):
207ˆà ˆà ˆà ˆà """Refill the field cells types and snakes according to the actual
208ˆà ˆà ˆà ˆà position"""
209ˆà ˆà ˆà ˆà f_w =ˆàself.field.w
210ˆà ˆà ˆà ˆà f_h =ˆàself.field.h
211ˆà ˆà ˆà ˆà forˆàx inˆàrange(1,f_w-1):
212ˆà ˆà ˆà ˆà ˆà ˆà forˆày inˆàrange(1,f_h-1):
213ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆày].type =ˆà'empty'
214ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà self.field[x,ˆày].snake =ˆàNone
215ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass
216ˆà ˆà ˆà ˆà forˆàsnake inˆàself.snakes:
217ˆà ˆà ˆà ˆà ˆà ˆà ifˆàsnake ==ˆàNone:
218ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass
219ˆà ˆà ˆà ˆà ˆà ˆà else:
220ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà snake.fill()
221ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà pass
222ˆà ˆà ˆà ˆà return
223ˆà ˆà
224ˆà ˆà defˆàredraw(self):
225ˆà ˆà ˆà ˆà """Clear the field Widget and redraw cells images on it"""
226ˆà ˆà ˆà ˆà self.canvas.delete("all")
227ˆà ˆà ˆà ˆà w =ˆàself.canvas.winfo_width()
228ˆà ˆà ˆà ˆà h =ˆàself.canvas.winfo_height()
229ˆà ˆà ˆà ˆà cw =ˆàw/float(self.field.w)
230ˆà ˆà ˆà ˆà ch =ˆàh/float(self.field.h)
231ˆà ˆà ˆà ˆà c =ˆàmin(cw,ˆàch)
232ˆà ˆà ˆà ˆà field_geometry =ˆà(self.field.w*c,self.field.h*c)
233ˆà ˆà ˆà ˆà offset =ˆà((w -ˆàfield_geometry[0])/2.0,ˆà(h -ˆàfield_geometry[1])/2.0)
234ˆà ˆà ˆà ˆà forˆàcell_coord inˆàself.field:
235ˆà ˆà ˆà ˆà ˆà ˆà self.field[cell_coord].redraw(offset,ˆàc)
236ˆà ˆà ˆà ˆà return
237ˆà ˆà
238ˆà ˆà defˆàlegal_moves(self,ˆàsnake):
239ˆà ˆà ˆà ˆà """Check for snake legal move directions according to the game rules."""
240ˆà ˆà ˆà ˆà snake.legal_dir =ˆà[]
241ˆà ˆà ˆà ˆà head =ˆàsnake.cells[0]
242ˆà ˆà ˆà ˆà forˆàdirection inˆàdirections:
243ˆà ˆà ˆà ˆà ˆà ˆà dir_cell =ˆàself.field[head.x +ˆàdirection[0],ˆàhead.y +ˆàdirection[1]]
244ˆà ˆà ˆà ˆà ˆà ˆà ifˆà(dir_cell.type ==ˆà'empty'ˆàorˆà(dir_cell.type ==ˆà'tail'ˆàandˆàdir_cell.snake !=ˆàsnake)):
245ˆà ˆà ˆà ˆà ˆà ˆà ˆà ˆà snake.legal_dir.append(direction)
246ˆà ˆà ˆà ˆà rnd.shuffle(snake.legal_dir)
247ˆà ˆà ˆà ˆà return
248
Note: See TracBrowser for help on using the repository browser.