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

snake

view main.py @ 158:7fc18f23ae89

Added rule to evade trapping yourself while eating for e4 snake
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Wed, 22 Dec 2010 00:06:14 +0300
parents 4e9a51811716
children 3c909a161978
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:"""
10 def __init__ (self):
11 """Create Python Battle game window.
12 Initialyze engige of the game."""
13 self.root = tk.Tk()
14 self.root.title("Python Battle")
15 self.canvas = tk.Canvas(self.root, background = "black")
16 self.canvas.pack(side ="top", fill="both", expand="yes")
17 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no")
18 self.step_id = 0
19 self.engine = engine.Engine(self.canvas)
20 self.after_id = None
21 return
23 def buttons_pack(self, root):
24 """Packing the buttons in root frame.
25 Definition of button functions."""
26 buttons = tk.Frame(root)
27 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(1))
28 load_1.grid(row=1, column=2, stick="news")
29 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(2))
30 load_2.grid(row=2, column=3, stick="news")
31 run_b = tk.Button(buttons, text="Run", command=lambda: self.run())
32 run_b.grid(row=1, column=5, stick="news")
33 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3))
34 load_3.grid(row=3, column=2, stick="news")
35 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(4))
36 load_4.grid(row=2, column=1, stick="news")
37 step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
38 step_b.grid(row=2, column=5, stick="news")
39 end_b = tk.Button(buttons, text="End", command=lambda: self.end())
40 end_b.grid(row=3, column=5, stick="news")
41 for column in range(1, 6):
42 buttons.grid_columnconfigure(column, weight=1)
43 return buttons
45 def load (self, snake_number):
46 """Ask for snake file loading.
47 Initialyzing snake and draw it on the field.
48 Return field back to default after end of the game."""
49 if self.step_id >= 200:
50 self.step_id = 0
51 self.engine.snakes = [None, None, None, None]
52 pass
53 if self.step_id == 0:
54 file = tkfd.askopenfile(title="Open file")
55 if file == None:
56 return
57 snake = self.engine.create_snake(snake_number)
58 snake.load(file)
59 pass
60 self.engine.refill()
61 self.engine.redraw()
62 return
64 def run (self):
65 """Run the game with 150 ms step"""
66 if self.dead_snake_check() == False:
67 return
68 if self.step_id > 200:
69 self.end()
70 return
71 self.step_id = self.step_id+1
72 self.engine.step()
73 self.after_id = self.canvas.after(150, self.run)
74 return
75 def step (self):
76 """Do the next game step"""
77 if self.dead_snake_check() == False:
78 return
79 if self.step_id <= 200:
80 if self.after_id != None:
81 self.canvas.after_cancel(self.after_id)
82 pass
83 self.step_id = self.step_id+1
84 self.engine.step()
85 pass
86 else:
87 self.end()
88 pass
89 return
91 def dead_snake_check(self):
92 """Check the number of snakes alive.
93 End the game if alive snake number is less than two."""
94 dead_snakes = 0
95 for snake in self.engine.snakes:
96 if snake == None:
97 dead_snakes=dead_snakes+1
98 pass
99 if dead_snakes >= 3:
100 self.end()
101 return False
103 def end (self):
104 """End the game and raise the window that tels about it."""
105 if self.after_id != None:
106 self.canvas.after_cancel(self.after_id)
107 pass
108 self.step_id = 201
109 root = tk.Tk()
110 end_label = tk.Label(root, text="End")
111 end_label.pack()
112 root.mainloop()
113 pass
115 if __name__ == "__main__":
116 snake_batle = UI()
117 snake_batle.root.mainloop()