Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://kodomo.cmm.msu.su/trac/snake/browser/main.py
Äàòà èçìåíåíèÿ: Unknown
Äàòà èíäåêñèðîâàíèÿ: Sun Apr 10 02:07:58 2016
Êîäèðîâêà: UTF-8
main.py ? Python Battle

source: main.py @ 187:954cc2774637

Revision 187:954cc2774637, 8.0 KB checked in by Alex Martynov, 5 years ago (diff)

done UI.snake_move_check() function

fixes #17

  • Property exe set to *
Lineš
1importšTkinteršasštk
2importštkFileDialogšasštkfd
3importšengine
4importšsnake
5
6
7
8classšUI(object):
9š š """User Interface:
10
11š š Atributes:
12
13š š - 'root' - root Window game placed at
14š š - 'engine' - engine of the game
15š š - 'canvas' - Widget field is pictured at
16š š - 'step_id' - current step of the game
17š š - 'after_id' - identificator of runing game process
18š š - 'step_legth' - length of the step (in ms)
19š š - 'game_length' - number of the steps in one round of the game"""
20š š defš__init__š(self):
21š š š š """Create Python Battle game window.
22š š š š Initialyze engige of the game."""
23š š š š self.root =štk.Tk()
24š š š š self.root.title("Python Battle")
25š š š š self.canvas =štk.Canvas(self.root,šbackground =š"black")
26š š š š self.canvas.pack(side ="top",šfill="both",šexpand="yes")
27š š š š self.buttons_pack(self.root).pack(side ="bottom",šfill="both",šexpand="no")
28š š š š self.step_id =š0
29š š š š self.engine =šengine.Engine(self.canvas)
30š š š š self.after_id =šNone
31š š š š self.step_length =š150
32š š š š self.game_length =š200
33š š š š return
34š š
35š š defšbuttons_pack(self,šroot):
36š š š š """Packing the buttons in root frame.
37š š š š Definition of button functions.
38
39š š š š 'Load' - ask for snake file load
40š š š š 'Run' - runs the game/next round. Next round starts with snakes survived in previous
41š š š š 'Step' - do the next dtep of the game
42š š š š 'End' - manual end of the game
43š š š š 'Restart" - restart the field with snakes of previous round"""
44š š š š buttons =štk.Frame(root)
45š š š š load_1 =štk.Button(buttons,štext="Load 1",šcommand=lambda:šself.load(0))
46š š š š load_1.grid(row=1,šcolumn=2,šstick="news")
47š š š š load_2 =štk.Button(buttons,štext="Load 2",šcommand=lambda:šself.load(1))
48š š š š load_2.grid(row=2,šcolumn=3,šstick="news")
49š š š š run_b =štk.Button(buttons,štext="Run",šcommand=lambda:šself.start())
50š š š š run_b.grid(row=2,šcolumn=2,šstick="news")
51š š š š restart_b =štk.Button(buttons,štext="Restart",šcommand=lambda:šself.restart(survived="no"))
52š š š š restart_b.grid(row=1,šcolumn=5,šstick="news")
53š š š š load_3 =štk.Button(buttons,štext="Load 3",šcommand=lambda:šself.load(2))
54š š š š load_3.grid(row=3,šcolumn=2,šstick="news")
55š š š š load_4 =štk.Button(buttons,štext="Load 4",šcommand=lambda:šself.load(3))
56š š š š load_4.grid(row=2,šcolumn=1,šstick="news")
57š š š š step_b =štk.Button(buttons,štext="Step",šcommand=lambda:šself.step())
58š š š š step_b.grid(row=2,šcolumn=5,šstick="news")
59š š š š end_b =štk.Button(buttons,štext="End",šcommand=lambda:šself.end())
60š š š š end_b.grid(row=3,šcolumn=5,šstick="news")
61š š š š foršcolumn inšrange(1,š6):
62š š š š š buttons.grid_columnconfigure(column,šweight=1)
63š š š š returnšbuttons
64š š
65š š defšloadš(self,šsnake_number):
66š š š š """Ask for snake file loading.
67š š š š Initialyzing snake and draw it on the field.
68š š š š Return field back to default (without snakes) after end of the game."""
69š š š š ifšself.step_id ==šself.game_length +š666:
70š š š š š š self.step_id =š0
71š š š š š š self.engine.snakes =š[None,šNone,šNone,šNone]
72š š š š š š pass
73š š š š ifšself.step_id ==š0:
74š š š š š š fileš=štkfd.askopenfile(title="Open file")
75š š š š š š ifšfileš==šNone:
76š š š š š š š š return
77š š š š š š snake =šself.engine.create_snake(snake_number)
78š š š š š š snake.load(file)
79š š š š š š pass
80š š š š self.engine.refill()
81š š š š self.engine.redraw()
82š š š š return
83
84š š defšstartš(self):
85š š š š """Init running of the game."""
86š š š š ifšself.after_id !=šNone:
87š š š š š š return
88š š š š ifšself.step_id ==šself.game_length +š666:
89š š š š š š self.restart(survived="yes")
90š š š š ifšself.step_id ==š0:
91š š š š š š self.engine.psnakes =šself.engine.snakes[:]
92š š š š self.run()
93š š š š
94š š defšrunš(self):
95š š š š """Run the game with 'step_length' ms step
96š š š š After the end of the game - restarts it with snakes survived in
97š š š š previous game"""
98š š š š ifšself.step_id >šself.game_length:
99š š š š š š self.end()
100š š š š š š return
101š š š š ifšself.snake_dead_check()š==šFalse:
102š š š š š š return
103š š š š ifšself.snake_move_check()š==šFalse:
104š š š š š š return
105š š š š self.step_id =šself.step_id+1
106š š š š self.engine.step()
107š š š š self.after_id =šself.canvas.after(self.step_length,šself.run)
108š š š š return
109š š
110š š defšstepš(self):
111š š š š """Do the next game step"""
112š š š š ifšself.snake_dead_check()š==šFalse:
113š š š š š š return
114š š š š ifšself.snake_move_check()š==šFalse:
115š š š š š š return
116š š š š ifšself.step_id ==š0:
117š š š š š š self.engine.psnakes =šself.engine.snakes[:]
118š š š š ifšself.step_id <=šself.game_length:
119š š š š š š self.run_cancel()
120š š š š š š self.step_id =šself.step_id+1
121š š š š š š self.engine.step()
122š š š š š š pass
123š š š š else:
124š š š š š š self.end()
125š š š š š š pass
126š š š š return
127
128š š defšrun_cancel(self):
129š š š š """Stops runnin of the game"""
130š š š š ifšself.after_id !=šNone:
131š š š š š š self.canvas.after_cancel(self.after_id)
132š š š š š š self.after_id =šNone
133š š
134š š defšsnake_dead_check(self):
135š š š š """Check the number of snakes alive.
136š š š š End the game if alive snake number is less than two."""
137š š š š dead_snakes =š0
138š š š š foršsnake inšself.engine.snakes:
139š š š š š š ifšsnake ==šNone:
140š š š š š š š š dead_snakes=dead_snakes+1
141š š š š š š š š pass
142š š š š ifšdead_snakes >=š3:
143š š š š š š self.end()
144š š š š š š returnšFalse
145š š š š
146š š defšsnake_len_check(self):
147š š š š """Get the snake with maximum length - the winer
148
149š š š š Return:
150š š š š winer - list of snake or snakes with max length
151š š š š length - this maximum length"""
152š š š š length=0
153š š š š winer =š[]
154š š š š foršsnake inšself.engine.snakes:
155š š š š š š ifšsnake !=šNone:
156š š š š š š š š ifšlen(snake.cells)š>šlength:
157š š š š š š š š š š length =šlen(snake.cells)
158š š š š š š š š š š winer =š[snake]
159š š š š š š š š elifšlen(snake.cells)š==šlength:
160š š š š š š š š š š winer.append(snake)š š
161š š š š returnšwiner,šlength
162
163š š defšsnake_move_check(self):
164š š š š """Get possible movements of the snakes.
165š š š š If all anakes cant move - return False."""
166š š š š total_moves =š0
167š š š š foršsnake inšself.engine.snakes:
168š š š š š š ifšsnake !=šNone:
169š š š š š š š š legal_moves =šself.engine.legal_moves(snake)
170š š š š š š š š foršmove inšlegal_moves:
171š š š š š š š š š š total_moves =štotal_moves+1
172š š š š ifštotal_moves ==š0:
173š š š š š š self.end()
174š š š š š š returnšFalse
175š š š š š š
176š š š š
177š š defšrestart(self,šsurvived):
178š š š š """"Restarts snakes positions after the end of the game
179
180š š š š Options:
181š š š š survived = "yes" - restarts next round only with snakes survived in previous round
182š š š š survived = "no" - restart next roun with all snakes played in previous round"""
183š š š š ifšsurvived ==š"yes":
184š š š š š š snake_set =šself.engine.snakes
185š š š š else:
186š š š š š š snake_set =šself.engine.psnakesš š š š š
187š š š š self.step_id =š0
188š š š š forši,šsnake inšenumerate(snake_set):
189š š š š š š ifšsnake_set[i]š!=šNone:
190š š š š š š š š self.engine.snakes[i]š=šsnake
191š š š š š š š š self.engine.create_snake(i,šsnake)
192š š š š self.engine.refill()
193š š š š self.engine.redraw()
194
195š š defšendš(self):
196š š š š """End the round and raise the label that tels about it."""
197š š š š self.run_cancel()
198š š š š self.step_id =šself.game_length +š666
199š š š š (w,šh),š(x,šy),š_ =šself.engine.field_geometry_calc()
200š š š š self.engine.redraw()
201š š š š self.canvas.create_text(x+w/2.0,šy+h/2.0,štext="End of the round",šfill="white",šfont="bold")
202š š š š winer,šlength =šself.snake_len_check()
203š š š š ifšlen(winer)š>š1:
204š š š š š š self.canvas.create_text(x+w/2.0,šy+h*2.0/3.0,štext="Number of winers: %s"š%(len(winer)),šfill="white",šfont="bold")
205š š š š else:
206š š š š š š self.canvas.create_text(x+w/2.0,šy+h*2.0/3.0,štext="Winer: %s(%s)"š%(winer[0].name,šwiner[0].color),šfill="white",šfont="bold")
207š š š š self.canvas.create_text(x+w/2.0,šy+h*3.0/4.0,štext="Total winer length: %s"š%(length),šfill="white",šfont="bold")
208š š š š pass
209
210ifš__name__ ==š"__main__":
211š š snake_batle =šUI()
212š š snake_batle.root.mainloop()
Note: See TracBrowser for help on using the repository browser.