From: Neil Date: Sat, 20 Apr 2013 10:17:14 +0000 (+0200) Subject: Redo draw logic to create fewer widgets, which seems the correct approach for kivy X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=2859fd8320d12da71ef1296fe9cf8e6e55598949;p=erdslangetjie.git Redo draw logic to create fewer widgets, which seems the correct approach for kivy --- diff --git a/erdslangetjie/__main__.py b/erdslangetjie/__main__.py index 4805419..bc64f00 100644 --- a/erdslangetjie/__main__.py +++ b/erdslangetjie/__main__.py @@ -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)