Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/snake/file/82de1358ded2/main.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 06:21:08 2013
Кодировка:
snake: 82de1358ded2 main.py

snake

view main.py @ 163:82de1358ded2

added new screen, restart now can operate at any moment
author Alex Martynov
date Thu, 23 Dec 2010 01:08:08 +0300
parents 56e7d0bfd362
children de9d0b9071da
line source
1 import Tkinter as tk
2 import tkFileDialog as tkfd
3 import engine
4 import snake
8 class UI(object):
9 """User Interface:
11 Atributes:
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' - fime of the step"""
19 def __init__ (self):
20 """Create Python Battle game window.
21 Initialyze engige of the game."""
22 self.root = tk.Tk()
23 self.root.title("Python Battle")
24 self.canvas = tk.Canvas(self.root, background = "black")
25 self.canvas.pack(side ="top", fill="both", expand="yes")
26 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
27 self.step_id = 666
28 self.engine = engine.Engine(self.canvas)
29 self.after_id = None
30 self.step_length = 150
31 return
33 def buttons_pack(self, root):
34 """Packing the buttons in root frame.
35 Definition of button functions."""
36 buttons = tk.Frame(root)
37 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
38 load_1.grid(row=1, column=2, stick="news")
39 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
40 load_2.grid(row=2, column=3, stick="news")
41 run_b = tk.Button(buttons, text="Run", command=lambda: self.run())
42 run_b.grid(row=2, column=2, stick="news")
43 restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart())
44 restart_b.grid(row=1, column=5, stick="news")
45 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
46 load_3.grid(row=3, column=2, stick="news")
47 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
48 load_4.grid(row=2, column=1, stick="news")
49 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
50 step_b.grid(row=2, column=5, stick="news")
51 end_b = tk.Button(buttons, text="End", command=lambda: self.end())
52 end_b.grid(row=3, column=5, stick="news")
53 for column in range(1, 6):
54 buttons.grid_columnconfigure(column, weight=1)
55 return buttons
57 def load (self, snake_number):
58 """Ask for snake file loading.
59 Initialyzing snake and draw it on the field.
60 Return field back to default after end of the game."""
61 if self.step_id == 666:
62 self.step_id = 0
63 self.engine.snakes = [None, None, None, None]
64 pass
65 if self.step_id == 0:
66 file = tkfd.askopenfile(title="Open file")
67 if file == None:
68 return
69 snake = self.engine.create_snake(snake_number)
70 snake.load(file)
71 pass
72 self.engine.refill()
73 self.engine.redraw()
74 return
76 def run (self):
77 """Run the game with 'step_length' ms step
78 After the end of the game - restarts it with snakes survived in
79 previous game"""
80 self.engine.psnakes[:] = self.engine.snakes[:]
81 if self.step_id == 666:
82 self.restart()
83 if self.dead_snake_check() == False:
84 return
85 if self.step_id > 200:
86 self.end()
87 return
88 self.step_id = self.step_id+1
89 self.engine.step()
90 self.after_id = self.canvas.after(self.step_length, self.run)
91 return
93 def step (self):
94 """Do the next game step"""
95 if self.dead_snake_check() == False:
96 return
97 if self.step_id <= 200:
98 if self.after_id != None:
99 self.canvas.after_cancel(self.after_id)
100 pass
101 self.step_id = self.step_id+1
102 self.engine.step()
103 pass
104 else:
105 self.end()
106 pass
107 return
109 def dead_snake_check(self):
110 """Check the number of snakes alive.
111 End the game if alive snake number is less than two."""
112 dead_snakes = 0
113 for snake in self.engine.snakes:
114 if snake == None:
115 dead_snakes=dead_snakes+1
116 pass
117 if dead_snakes >= 3:
118 self.end()
119 return False
121 def restart(self):
122 """"Restarts the game after the end of the game with snakes survived"""
123 self.step_id = 0
124 for i, snake in enumerate(self.engine.psnakes):
125 if self.engine.psnakes[i] != None:
126 self.engine.snakes[i] = snake
127 self.engine.create_snake(i, snake)
128 self.engine.refill()
129 self.engine.redraw()
131 def end (self):
132 """End the game and raise the window that tels about it."""
133 if self.after_id != None:
134 self.canvas.after_cancel(self.after_id)
135 pass
136 self.step_id = 666
137 root = tk.Tk()
138 end_label = tk.Label(root, text="End")
139 end_label.pack()
140 root.mainloop()
141 pass
143 if __name__ == "__main__":
144 snake_batle = UI()
145 snake_batle.root.mainloop()