rev |
line source |
martiran@18
|
1 import Tkinter as tk |
Alex@23
|
2 import tkFileDialog as tkfd |
martiran@20
|
3 import engine |
martiran@32
|
4 import snake |
martiran@18
|
5 |
martiran@18
|
6 |
martiran@18
|
7 |
martiran@18
|
8 class UI(object): |
Alex@160
|
9 """User Interface: |
Alex@160
|
10 |
Alex@160
|
11 Atributes: |
Alex@160
|
12 |
Alex@160
|
13 - 'root' - root Window game placed at |
Alex@160
|
14 - 'engine' - engine of the game |
Alex@160
|
15 - 'canvas' - Widget field is pictured at |
Alex@160
|
16 - 'step_id' - current step of the game |
Alex@160
|
17 - 'after_id' - identificator of runing game process |
Alex@166
|
18 - 'step_legth' - length of the step (in ms) |
Alex@166
|
19 - 'game_length' - number of the steps in one round of the game""" |
martiran@19
|
20 def __init__ (self): |
martiran@136
|
21 """Create Python Battle game window. |
martiran@136
|
22 Initialyze engige of the game.""" |
martiran@29
|
23 self.root = tk.Tk() |
martiran@29
|
24 self.root.title("Python Battle") |
martiran@29
|
25 self.canvas = tk.Canvas(self.root, background = "black") |
martiran@19
|
26 self.canvas.pack(side ="top", fill="both", expand="yes") |
me@96
|
27 self.buttons_pack(self.root).pack(side ="bottom", fill="both", expand="no") |
Alex@165
|
28 self.step_id = 0 |
martiran@19
|
29 self.engine = engine.Engine(self.canvas) |
martiran@32
|
30 self.after_id = None |
Alex@160
|
31 self.step_length = 150 |
Alex@166
|
32 self.game_length = 200 |
martiran@18
|
33 return |
martiran@136
|
34 |
me@96
|
35 def buttons_pack(self, root): |
martiran@136
|
36 """Packing the buttons in root frame. |
martiran@136
|
37 Definition of button functions.""" |
me@96
|
38 buttons = tk.Frame(root) |
Alex@160
|
39 load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0)) |
martiran@132
|
40 load_1.grid(row=1, column=2, stick="news") |
Alex@160
|
41 load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1)) |
martiran@132
|
42 load_2.grid(row=2, column=3, stick="news") |
Alex@165
|
43 run_b = tk.Button(buttons, text="Run", command=lambda: self.start()) |
Alex@160
|
44 run_b.grid(row=2, column=2, stick="news") |
Alex@167
|
45 restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart(survived="no")) |
Alex@160
|
46 restart_b.grid(row=1, column=5, stick="news") |
Alex@160
|
47 load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2)) |
martiran@132
|
48 load_3.grid(row=3, column=2, stick="news") |
Alex@160
|
49 load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3)) |
martiran@132
|
50 load_4.grid(row=2, column=1, stick="news") |
me@96
|
51 step_b = tk.Button(buttons, text="Step", command=lambda: self.step()) |
martiran@151
|
52 step_b.grid(row=2, column=5, stick="news") |
martiran@151
|
53 end_b = tk.Button(buttons, text="End", command=lambda: self.end()) |
martiran@151
|
54 end_b.grid(row=3, column=5, stick="news") |
me@133
|
55 for column in range(1, 6): |
me@133
|
56 buttons.grid_columnconfigure(column, weight=1) |
me@96
|
57 return buttons |
martiran@29
|
58 |
Alex@23
|
59 def load (self, snake_number): |
martiran@136
|
60 """Ask for snake file loading. |
martiran@136
|
61 Initialyzing snake and draw it on the field. |
Alex@164
|
62 Return field back to default (without snakes) after end of the game.""" |
Alex@168
|
63 if self.step_id == self.game_length + 666: |
|