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

snake

diff main.py @ 160:56e7d0bfd362

changed numeration of the snakes edited documentation changed button placement added restart button added UI.step_length
author Alex Martynov
date Wed, 22 Dec 2010 19:35:56 +0300
parents 7a4853ff834f
children 82de1358ded2
line diff
     1.1 --- a/main.py	Tue Dec 21 23:01:07 2010 +0300
     1.2 +++ b/main.py	Wed Dec 22 19:35:56 2010 +0300
     1.3 @@ -6,7 +6,16 @@
     1.4  
     1.5  
     1.6  class UI(object):
     1.7 -    """User Interface:"""
     1.8 +    """User Interface:
     1.9 +
    1.10 +    Atributes:
    1.11 +
    1.12 +    - 'root' - root Window game placed at
    1.13 +    - 'engine' - engine of the game
    1.14 +    - 'canvas' - Widget field is pictured at
    1.15 +    - 'step_id' - current step of the game
    1.16 +    - 'after_id' - identificator of runing game process
    1.17 +    - 'step_legth' - fime of the step"""
    1.18      def __init__ (self):
    1.19          """Create Python Battle game window.
    1.20          Initialyze engige of the game."""
    1.21 @@ -18,21 +27,24 @@
    1.22          self.step_id = 0
    1.23          self.engine = engine.Engine(self.canvas)
    1.24          self.after_id = None
    1.25 +        self.step_length = 150
    1.26          return
    1.27      
    1.28      def buttons_pack(self, root):
    1.29          """Packing the buttons in root frame.
    1.30          Definition of button functions."""
    1.31          buttons = tk.Frame(root)
    1.32 -        load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(1))
    1.33 +        load_1 = tk.Button(buttons, text="Load 1", command=lambda: self.load(0))
    1.34          load_1.grid(row=1, column=2, stick="news")
    1.35 -        load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(2))
    1.36 +        load_2 = tk.Button(buttons, text="Load 2", command=lambda: self.load(1))
    1.37          load_2.grid(row=2, column=3, stick="news")
    1.38          run_b = tk.Button(buttons, text="Run", command=lambda: self.run())
    1.39 -        run_b.grid(row=1, column=5, stick="news")
    1.40 -        load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(3))
    1.41 +        run_b.grid(row=2, column=2, stick="news")
    1.42 +        restart_b = tk.Button(buttons, text="Restart", command=lambda: self.restart())
    1.43 +        restart_b.grid(row=1, column=5, stick="news")
    1.44 +        load_3 = tk.Button(buttons, text="Load 3", command=lambda: self.load(2))
    1.45          load_3.grid(row=3, column=2, stick="news")
    1.46 -        load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(4))
    1.47 +        load_4 = tk.Button(buttons, text="Load 4", command=lambda: self.load(3))
    1.48          load_4.grid(row=2, column=1, stick="news")
    1.49          step_b = tk.Button(buttons, text="Step", command=lambda: self.step())
    1.50          step_b.grid(row=2, column=5, stick="news")
    1.51 @@ -49,7 +61,6 @@
    1.52          if self.step_id == 666:
    1.53              self.step_id = 0
    1.54              self.engine.snakes = [None, None, None, None]
    1.55 -            self.engine.psnakes = [None, None, None, None]
    1.56              pass
    1.57          if self.step_id == 0:
    1.58              file = tkfd.askopenfile(title="Open file")
    1.59 @@ -63,12 +74,10 @@
    1.60          return
    1.61  
    1.62      def run (self):
    1.63 -        """Run the game with 150 ms step"""
    1.64 -        if self.step_id == 666:
    1.65 -            self.step_id = 0
    1.66 -            for i, snake in enumerate(self.engine.psnakes):
    1.67 -                self.engine.snakes[i] = snake
    1.68 -                self.engine.create_snake(i, snake)
    1.69 +        """Run the game with 'step_length' ms step
    1.70 +        After the end of the game - restarts it with snakes survived in
    1.71 +        previous game"""
    1.72 +        self.restart()
    1.73          if self.dead_snake_check() == False:
    1.74              return
    1.75          if self.step_id > 200:
    1.76 @@ -76,8 +85,9 @@
    1.77              return
    1.78          self.step_id = self.step_id+1
    1.79          self.engine.step()
    1.80 -        self.after_id = self.canvas.after(150, self.run)
    1.81 +        self.after_id = self.canvas.after(self.step_length, self.run)
    1.82          return
    1.83 +    
    1.84      def step (self):
    1.85          """Do the next game step"""
    1.86          if self.dead_snake_check() == False:
    1.87 @@ -105,6 +115,19 @@
    1.88          if dead_snakes >= 3:
    1.89              self.end()
    1.90              return False
    1.91 +        
    1.92 +    def restart(self):
    1.93 +        """"Restarts the game after the end of the game with snakes survived"""
    1.94 +        if self.step_id == 666:
    1.95 +            print "her"
    1.96 +            self.step_id = 0
    1.97 +            for i, snake in enumerate(self.engine.psnakes):
    1.98 +                if self.engine.psnakes[i] != None:
    1.99 +                    self.engine.snakes[i] = snake
   1.100 +                    self.engine.create_snake(i, snake)
   1.101 +            self.engine.refill()
   1.102 +            self.engine.redraw()
   1.103 +        self.engine.psnakes = self.engine.snakes
   1.104  
   1.105      def end (self):
   1.106          """End the game and raise the window that tels about it."""