Redo draw logic to create fewer widgets, which seems the correct approach for kivy
authorNeil <neil@dip.sun.ac.za>
Sat, 20 Apr 2013 10:17:14 +0000 (12:17 +0200)
committerNeil <neil@dip.sun.ac.za>
Sat, 20 Apr 2013 10:17:14 +0000 (12:17 +0200)
erdslangetjie/__main__.py

index 48054197543eb14059ffadfe796ab5604102430a..bc64f000d61c3b3177d48feaf389ccc16b19b3c3 100644 (file)
@@ -6,7 +6,7 @@ from kivy.uix.relativelayout import RelativeLayout
 from kivy.uix.scrollview import ScrollView
 from kivy.uix.label import Label
 from kivy.uix.popup import Popup
-from kivy.graphics import Color, Rectangle
+from kivy.graphics import Rectangle
 from kivy.utils import platform
 from kivy.clock import Clock
 from kivy.config import Config
@@ -27,7 +27,6 @@ class GameWindow(RelativeLayout):
         self.level_list = level_list
         self.level_obj = self.level_list.get_current_level()
         self.level_obj.load_tiles()
-        self.tiles = {}
         self.app = app
 
         cols, rows = self.level_obj.get_size()
@@ -52,6 +51,7 @@ class GameWindow(RelativeLayout):
         self.player.pos = self.level_obj.enter_pos
         self.keyboard = None
         self._key_bound = False
+        self._background = None
 
     def build(self):
         if platform() != 'android' and not self.keyboard:
@@ -66,7 +66,7 @@ class GameWindow(RelativeLayout):
             self._key_bound = True
             self.keyboard.bind(on_key_down=self._on_key_down)
         self.clear_widgets()
-        self.tiles = {}
+        self._background = Widget(size=self.size, pos=(0, 0))
         tiles = self.level_obj.get_tiles()
         bx, by = 0, 0
         for tile_line in tiles:
@@ -75,18 +75,12 @@ class GameWindow(RelativeLayout):
                 self.draw_tile((bx, by), tile)
                 bx += TILE_SIZE
             by += TILE_SIZE
+        self.add_widget(self._background)
 
     def draw_tile(self, pos, tile):
-        if pos in self.tiles:
-            self.remove_widget(self.tiles[pos])
-        node = Widget(size=(TILE_SIZE, TILE_SIZE),
-                pos=pos, size_hint=(None, None))
-        self.add_widget(node)
-        with node.canvas:
-            Color(1, 1, 1)
-            Rectangle(pos=node.pos, size=node.size,
+        with self._background.canvas:
+            Rectangle(pos=pos, size=(TILE_SIZE, TILE_SIZE),
                     texture=tile.texture)
-        self.tiles[pos] = node
 
     def fix_scroll_margins(self):
         # We need to call this after app.root is set
@@ -102,7 +96,6 @@ class GameWindow(RelativeLayout):
         self.player_tile = Widget(size=(TILE_SIZE, TILE_SIZE),
                 pos=sprite_pos)
         with self.player_tile.canvas:
-            Color(1, 1, 1)
             Rectangle(pos=sprite_pos, size=self.player_tile.size,
                     texture=self.player.get_texture())
         self.add_widget(self.player_tile)
@@ -164,7 +157,6 @@ class GameWindow(RelativeLayout):
         self.nemesis_tile = Widget(size=(TILE_SIZE, TILE_SIZE),
                 pos=sprite_pos)
         with self.nemesis_tile.canvas:
-            Color(1, 1, 1)
             Rectangle(pos=sprite_pos, size=self.nemesis_tile.size,
                     texture=self.nemesis.get_texture())
         self.add_widget(self.nemesis_tile)