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

snake

view main.py @ 168:0f7df983d610

fixed bag of unlimited run added screen
author Alex Martynov
date Thu, 23 Dec 2010 20:31:09 +0300
parents 36756bd7e7ed
children 4eec473f445b
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' - 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
35 def buttons_pack(self, root):
36 """Packing the buttons in root frame.
37 Definition of button functions."""
38 buttons = tk.Frame(root)
39 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
40 load_1.grid(row=1, column=2, stick="news")
41 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
42 load_2.grid(row=2, column=3, stick="news")
43 run_b = tk.Button(buttons, text="Run", command=lambda: self.start())
44 run_b.grid(row=2, column=2, stick="news")
45 restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart(survived="no"))
46 restart_b.grid(row=1, column=5, stick="news")
47 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
48 load_3.grid(row=3, column=2, stick="news")
49 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
50 load_4.grid(row=2, column=1, stick="news")
51 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
52 step_b.grid(row=2, column=5, stick="news")
53 end_b = tk.Button(buttons, text="End", command=lambda: self.end())
54 end_b.grid(row=3, column=5, stick="news")
55 for column in range(1, 6):
56 buttons.grid_columnconfigure(column, weight=1)
57 return buttons
59 def load (self, snake_number):
60 """Ask for snake file loading.
61 Initialyzing snake and draw it on the field.
62 Return field back to default (without snakes) after end of the game."""
63 if self.step_id == self.game_length + 666:
64 self.step_id = 0
65 self.engine.snakes = [None, None, None, None]
66 pass
67 if self.step_id == 0:
68 file = tkfd.askopenfile(title="Open file")
69 if file == None:
70 return
71 snake = self.engine.create_snake(snake_number)
72 snake.load(file)
73 pass
74 self.engine.refill()
75 self.engine.redraw()
76 return
78 def start (self):
79 """Init running of the game."""
80 if self.step_id == self.game_length + 666:
81 self.restart(survived="yes")
82 if self.dead_snake_check() == False:
83 return
84 self.engine.psnakes = self.engine.snakes[:]
85 self.run()
87 def run (self):
88 """Run the game with 'step_length' ms step
89 After the end of the game - restarts it with snakes survived in
90 previous game"""
91 if self.step_id > self.game_length:
92 self.end()
93 return
94 self.step_id = self.step_id+1
95 self.engine.step()
96 self.after_id = self.canvas.after(self.step_length, self.run)
97 return
99 def step (self):
100 """Do the next game step"""
101 if self.dead_snake_check() == False:
102 return
103 if self.step_id <= self.game_length:
104 if self.after_id != None:
105 self.canvas.after_cancel(self.after_id)
106 pass
107 self.step_id = self.step_id+1
108 self.engine.step()
109 pass
110 else:
111 self.end()
112 pass
113 return
115 def dead_snake_check(self):
116 """Check the number of snakes alive.
117 End the game if alive snake number is less than two."""
118 dead_snakes = 0
119 for snake in self.engine.snakes:
120 if snake == None:
121 dead_snakes=dead_snakes+1
122 pass
123 if dead_snakes >= 3:
124 self.end()
125 return False
127 def restart(self, survived):
128 """"Restarts snakes positions after the end of the game
130 Options:
131 survived = "yes" - restarts next round only with snakes survived in previous round
132 survived = "no" - restart next roun with all snakes played in previous round"""
133 if survived == "yes":
134 snake_set = self.engine.snakes
135 else:
136 snake_set = self.engine.psnakes
137 self.step_id = 0
138 for i, snake in enumerate(snake_set):
139 if snake_set[i] != None:
140 self.engine.snakes[i] = snake
141 self.engine.create_snake(i, snake)
142 self.engine.refill()
143 self.engine.redraw()
145 def end (self):
146 """End the game and raise the window that tels about it."""
147 if self.after_id != None:
148 self.canvas.after_cancel(self.after_id)
149 pass
150 self.step_id = self.game_length + 666
151 root = tk.Tk()
152 end_label = tk.Label(root, text="End")
153 end_label.pack()
154 root.mainloop()
155 pass
157 if __name__ == "__main__":
158 snake_batle = UI()
159 snake_batle.root.mainloop()