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

source: main.py @ 176:e967a9b10fce

Revision 176:e967a9b10fce, 6.6 KB checked in by Alex Martynov, 5 years ago (diff)

edited the way of presenting round end, fixes #20

  • 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.dead_snake_check()š==šFalse:
102š š š š š š return
103š š š š self.step_id =šself.step_id+1
104š š š š self.engine.step()
105š š š š self.after_id =šself.canvas.after(self.step_length,šself.run)
106š š š š return
107š š
108š š defšstepš(self):
109š š š š """Do the next game step"""
110š š š š ifšself.dead_snake_check()š==šFalse:
111š š š š š š return
112š š š š ifšself.step_id ==š0:
113š š š š š š self.engine.psnakes =šself.engine.snakes[:]
114š š š š ifšself.step_id <=šself.game_length:
115š š š š š š self.run_cancel()
116š š š š š š self.step_id =šself.step_id+1
117š š š š š š self.engine.step()
118š š š š š š pass
119š š š š else:
120š š š š š š self.end()
121š š š š š š pass
122š š š š return
123
124š š defšrun_cancel(self):
125š š š š """Stops runnin of the game"""
126š š š š ifšself.after_id !=šNone:
127š š š š š š self.canvas.after_cancel(self.after_id)
128š š š š š š self.after_id =šNone
129š š
130š š defšdead_snake_check(self):
131š š š š """Check the number of snakes alive.
132š š š š End the game if alive snake number is less than two."""
133š š š š dead_snakes =š0
134š š š š foršsnake inšself.engine.snakes:
135š š š š š š ifšsnake ==šNone:
136š š š š š š š š dead_snakes=dead_snakes+1
137š š š š š š š š pass
138š š š š ifšdead_snakes >=š3:
139š š š š š š self.end()
140š š š š š š returnšFalse
141š š š š
142š š defšrestart(self,šsurvived):
143š š š š """"Restarts snakes positions after the end of the game
144
145š š š š Options:
146š š š š survived = "yes" - restarts next round only with snakes survived in previous round
147š š š š survived = "no" - restart next roun with all snakes played in previous round"""
148š š š š ifšsurvived ==š"yes":
149š š š š š š snake_set =šself.engine.snakes
150š š š š else:
151š š š š š š snake_set =šself.engine.psnakesš š š š š
152š š š š self.step_id =š0
153š š š š forši,šsnake inšenumerate(snake_set):
154š š š š š š ifšsnake_set[i]š!=šNone:
155š š š š š š š š self.engine.snakes[i]š=šsnake
156š š š š š š š š self.engine.create_snake(i,šsnake)
157š š š š self.engine.refill()
158š š š š self.engine.redraw()
159
160š š defšendš(self):
161š š š š """End the round and raise the label that tels about it."""
162š š š š self.run_cancel()
163š š š š self.step_id =šself.game_length +š666
164š š š š w =šself.canvas.winfo_width()
165š š š š h =šself.canvas.winfo_height()
166š š š š cw =šw/float(self.engine.field.w)
167š š š š ch =šh/float(self.engine.field.h)
168š š š š c =šmin(cw,šch)
169š š š š field_geometry =š(self.engine.field.w*c,self.engine.field.h*c)
170š š š š offset =š((w -šfield_geometry[0])/2.0,š(h -šfield_geometry[1])/2.0)
171š š š š self.canvas.create_text(offset[0]+šfield_geometry[0]/2.0,šoffset[1]+field_geometry[1]/2.0,štext="End of the round",šfill="white",šfont="bold")
172š š š š pass
173
174ifš__name__ ==š"__main__":
175š š snake_batle =šUI()
176š š snake_batle.root.mainloop()
Note: See TracBrowser for help on using the repository browser.