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

source: main.py @ 174:55aeb245c503

Revision 174:55aeb245c503, 6.2 KB checked in by Alex Martynov, 5 years ago (diff)

fixed bug of lost previous snakes memory,

while turning from run mode to step and back

fixes #19

  • 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.step_id ==šself.game_length +š666:
87š š š š š š self.restart(survived="yes")
88š š š š ifšself.step_id ==š0:
89š š š š š š self.engine.psnakes =šself.engine.snakes[:]
90š š š š self.run()
91š š š š
92š š defšrunš(self):
93š š š š """Run the game with 'step_length' ms step
94š š š š After the end of the game - restarts it with snakes survived in
95š š š š previous game"""
96š š š š ifšself.step_id >šself.game_length:
97š š š š š š self.end()
98š š š š š š return
99š š š š ifšself.dead_snake_check()š==šFalse:
100š š š š š š return
101š š š š self.step_id =šself.step_id+1
102š š š š self.engine.step()
103š š š š self.after_id =šself.canvas.after(self.step_length,šself.run)
104š š š š return
105š š
106š š defšstepš(self):
107š š š š """Do the next game step"""
108š š š š ifšself.dead_snake_check()š==šFalse:
109š š š š š š return
110š š š š ifšself.step_id ==š0:
111š š š š š š self.engine.psnakes =šself.engine.snakes[:]
112š š š š ifšself.step_id <=šself.game_length:
113š š š š š š ifšself.after_id !=šNone:
114š š š š š š š š self.canvas.after_cancel(self.after_id)
115š š š š š š š š pass
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šdead_snake_check(self):
125š š š š """Check the number of snakes alive.
126š š š š End the game if alive snake number is less than two."""
127š š š š dead_snakes =š0
128š š š š foršsnake inšself.engine.snakes:
129š š š š š š ifšsnake ==šNone:
130š š š š š š š š dead_snakes=dead_snakes+1
131š š š š š š š š pass
132š š š š ifšdead_snakes >=š3:
133š š š š š š self.end()
134š š š š š š returnšFalse
135š š š š
136š š defšrestart(self,šsurvived):
137š š š š """"Restarts snakes positions after the end of the game
138
139š š š š Options:
140š š š š survived = "yes" - restarts next round only with snakes survived in previous round
141š š š š survived = "no" - restart next roun with all snakes played in previous round"""
142š š š š ifšsurvived ==š"yes":
143š š š š š š snake_set =šself.engine.snakes
144š š š š else:
145š š š š š š snake_set =šself.engine.psnakesš š š š š
146š š š š self.step_id =š0
147š š š š forši,šsnake inšenumerate(snake_set):
148š š š š š š ifšsnake_set[i]š!=šNone:
149š š š š š š š š self.engine.snakes[i]š=šsnake
150š š š š š š š š self.engine.create_snake(i,šsnake)
151š š š š self.engine.refill()
152š š š š self.engine.redraw()
153
154š š defšendš(self):
155š š š š """End the game and raise the window that tels about it."""
156š š š š ifšself.after_id !=šNone:
157š š š š š š self.canvas.after_cancel(self.after_id)
158š š š š š š pass
159š š š š self.step_id =šself.game_length +š666
160š š š š root =štk.Tk()
161š š š š end_label =štk.Label(root,štext="End")
162š š š š end_label.pack()
163š š š š root.mainloop()
164š š š š pass
165
166ifš__name__ ==š"__main__":
167š š snake_batle =šUI()
168š š snake_batle.root.mainloop()
Note: See TracBrowser for help on using the repository browser.