Changeset 3816

Show
Ignore:
Timestamp:
12/18/2007 08:22:03 AM (1 year ago)
Author:
ed
Message:

Lots of changes to the Bubblet game. Most center on the drawing refresh problem (Ticket #1124), but I took the opportunity to overhaul a lot of the logic, using the new makeProxyProperty() method.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/demo/DaboDemo.py

    r3642 r3816  
    1111    useSplash = "linux" not in platform.platform().lower() 
    1212    mfc = "DaboDemo.cdxml" 
    13     if not os.path.exists(mfc): 
    14         mfc = os.path.join(os.path.split(sys.argv[0])[0], mfc) 
     13    if not os.path.exists(os.path.join(os.getcwd(), mfc)): 
     14        mfc = os.path.join(os.getcwd(), os.path.split(sys.argv[0])[0], mfc) 
    1515 
    1616    app = dabo.dApp(showSplashScreen=useSplash, splashTimeout=3000, 
  • trunk/demo/samples/games/bubblet/BubbleBizobj.py

    r3562 r3816  
    66import time 
    77import os 
     8 
    89 
    910class BubbleBizobj(biz.dBizobj): 
     
    2627        for rr in self.bubbles: 
    2728            for cc in rr: 
    28                 cc.Selected = False 
    2929                cc.setRandomColor(True) 
     30                cc.Selected = cc.Popped = False 
    3031        self.Score = 0 
    3132        self.GameOver = False 
    3233        self.IsNewHighGame = False 
     34        self._allBubbles = [] 
     35        for bubrow in self.bubbles: 
     36            self._allBubbles.extend(bubrow) 
    3337     
    3438     
     
    5256            for cc in rr: 
    5357                if cc.Selected: 
    54                     cc.Color = Non
     58                    cc.Popped = Tru
    5559                    cc.Selected = False 
    5660        self.selCount = 0 
     
    6468        rows = len(self.bubbles) 
    6569        cols = len(self.bubbles[0]) 
    66          
     70 
    6771        # Check if the lowest bubble is empty. 
    6872        toFill = 0 
    6973        for cc in range(cols): 
    70             if self.bubbles[rows-1][cc].Color is None
     74            if self.bubbles[rows-1][cc].Popped
    7175                toFill += 1 
    7276         
     
    7579            # work is left 
    7680            self.__callbackFunc = self.callbackShift 
     81            self._shiftTime = time.time() 
    7782             
    7883            # Fill the columns 
    7984            for cc in range(toFill): 
    80                 num = random.randrange(rows) + 1 
     85                num = random.randrange(rows-1) + 2 
    8186                for ii in range(rows-num, rows): 
    8287                    bub = self.bubbles[ii][cc] 
     
    97102        rows = len(self.bubbles) 
    98103        cols = len(self.bubbles[0]) 
     104        shifted = [] 
    99105        for cc in range(cols): 
    100             gap = False 
    101             for rr in range(rows): 
    102                 if self.bubbles[rr][cc].Color is not None: 
    103                     gap = True 
    104                 else: 
    105                     if gap: 
    106                         for rAbove in range(rr, 0, -1): 
    107                             self.bubbles[rAbove][cc].Color = self.bubbles[rAbove-1][cc].Color 
    108                         self.bubbles[0][cc].Color = None 
     106            for rr in range(1, rows): 
     107                if self.bubbles[rr][cc].Popped: 
     108                    for rAbove in range(rr, 0, -1): 
     109                        self.bubbles[rAbove][cc].Color = self.bubbles[rAbove-1][cc].Color 
     110                        self.bubbles[rAbove][cc].Popped = self.bubbles[rAbove-1][cc].Popped 
     111                    self.bubbles[0][cc].Popped = True 
    109112        # Now shift columns to the right 
    110113        for rr in range(rows): 
    111             gap = False 
    112114            for cc in range(cols-1, 0, -1): 
    113115                currBub = self.bubbles[rr][cc] 
    114                 if currBub.Color is None
     116                if currBub.Popped
    115117                    # See if there are any bubbles to the left that are not empty 
    116118                    for cLeft in range(cc, -1, -1): 
    117119                        leftBub = self.bubbles[rr][cLeft] 
    118                         if leftBub.Color is not None
     120                        if not leftBub.Popped
    119121                            currBub.Color = leftBub.Color 
    120                             leftBub.Color = None 
     122                            currBub.Popped = False 
     123                            leftBub.Popped = True 
    121124                            break    
    122125         
     
    134137        """ 
    135138        self.GameOver = True 
    136         rows = len(self.bubbles) 
    137         cols = len(self.bubbles[0]) 
    138         for rr in range(rows-1, -1,-1): 
    139             for cc in range(cols-1, -1, -1): 
    140                 if self.hasMatchingNeighbor(self.bubbles[rr][cc]): 
    141                     self.GameOver = False 
    142                     break 
    143             if not self.GameOver: 
     139        for bub in self._allBubbles: 
     140            if self.hasMatchingNeighbor(bub): 
     141                self.GameOver = False 
    144142                break 
     143 
    145144        if self.GameOver: 
    146145            self.NumberOfGames += 1 
     
    156155    def hasMatchingNeighbor(self, bubble):       
    157156        """ Need to try for matches above, below, left and right. """ 
     157        if bubble.Popped: 
     158            return False 
     159        return bool(self.getMatchingNeighbors(bubble)) 
     160 
     161 
     162    def getMatchingNeighbors(self, bubble): 
     163        color = bubble.Color 
    158164        rr, cc = bubble.row, bubble.col 
    159         color = bubble.Color 
    160         if color is None: 
    161             return False 
    162         rows = len(self.bubbles) 
    163         cols = len(self.bubbles[0]) 
    164          
    165         # Above 
    166         if rr > 0: 
    167             try: 
    168                 bub = self.bubbles[rr-1][cc] 
    169                 if bub.Color == color: 
    170                     return True 
    171             except: pass 
    172         # Below 
    173         if rr < rows: 
    174             try: 
    175                 bub = self.bubbles[rr+1][cc] 
    176                 if bub.Color == color: 
    177                     return True 
    178             except: pass 
    179         # Left 
    180         if cc > 0: 
    181             try: 
    182                 bub = self.bubbles[rr][cc-1] 
    183                 if bub.Color == color: 
    184                     return True 
    185             except: pass 
    186         # Right 
    187         if cc < cols: 
    188             try: 
    189                 bub = self.bubbles[rr][cc+1] 
    190                 if bub.Color == color: 
    191                     return True 
    192             except: pass 
    193         return False 
    194          
     165        return [neighbor for neighbor in self._allBubbles 
     166                if neighbor.Color == color and  
     167                neighbor.Popped is False and ( 
     168                ((abs(neighbor.row - rr) == 1) and (neighbor.col == cc)) or 
     169                ((abs(neighbor.col - cc) == 1) and (neighbor.row == rr)))] 
     170 
    195171     
    196172    def selectBubbles(self, bubble): 
    197173        if bubble.Selected: 
    198174            return 
    199         color = bubble.Color 
    200         if color is None: 
     175        if bubble.Popped: 
    201176            # They clicked on an empty space 
    202177            return 
    203         bubble.Selected = True 
    204         hasMatch = False 
    205         rr, cc = bubble.row, bubble.col 
    206         rows = len(self.bubbles) 
    207         cols = len(self.bubbles[0]) 
    208          
    209         # We need to check the bubbles on top, bottom, left and right. 
    210         # Above 
    211         if rr > 0: 
    212             try: 
    213                 bub = self.bubbles[rr-1][cc] 
    214                 if bub.Color == color: 
    215                     hasMatch = True 
    216                     self.selectBubbles(bub) 
    217             except: pass 
    218         # Below 
    219         if rr < rows: 
    220             try: 
    221                 bub = self.bubbles[rr+1][cc] 
    222                 if bub.Color == color: 
    223                     hasMatch = True 
    224                     self.selectBubbles(bub) 
    225             except: pass 
    226         # Left 
    227         if cc > 0: 
    228             try: 
    229                 bub = self.bubbles[rr][cc-1] 
    230                 if bub.Color == color: 
    231                     hasMatch = True 
    232                     self.selectBubbles(bub) 
    233             except: pass 
    234         # Right 
    235         if cc < cols: 
    236             try: 
    237                 bub = self.bubbles[rr][cc+1] 
    238                 if bub.Color == color: 
    239                     hasMatch = True 
    240                     self.selectBubbles(bub) 
    241             except: pass 
    242          
    243         if not hasMatch: 
    244             bubble.Selected = False 
    245         else: 
    246             self.selCount += 1 
     178        mn = self.getMatchingNeighbors(bubble) 
     179        bubble.Selected = bool(mn) 
     180        self.selCount += {True: 1, False: 0}[bool(mn)] 
     181        for match in mn: 
     182            if not match.Selected: 
     183                self.selectBubbles(match) 
    247184 
    248185     
    249186    def unselectBubbles(self): 
    250         for rr in self.bubbles: 
    251             for cc in rr: 
    252                 cc.Selected = False 
     187        selBubs = (bub for bub in self._allBubbles 
     188                if bub.Selected) 
     189        for sel in selBubs: 
     190            sel.Selected = False 
    253191        self.selCount = 0 
    254192        self.Message = _("Bubble Points: 0") 
  • trunk/demo/samples/games/bubblet/BubblePanel.py

    r3562 r3816  
    33import dabo.dEvents as dEvents 
    44from dabo.dLocalize import _ 
    5 import random 
     5from dabo.ui import makeProxyProperty 
    66 
    77 
    88class BubblePanel(dabo.ui.dPanel): 
    99    def afterInit(self): 
    10         self.Buffered = True 
     10        self.Buffered = False 
     11         
     12        self._popped = False 
     13        self._selected = False 
     14        self._colors = ["blue", "green", "red", "yellow", "purple"] 
     15        self._color = dabo.dColors.randomColor() 
     16        # Used to detect size changes 
     17        self._sizeCache = (0, 0) 
     18         
     19        plat = self.Application.Platform 
     20        if plat == "Win": 
     21            self.selectedBackColor = (192,192,255) 
     22        else: 
     23            self.selectedBackColor = (128,128,192) 
     24        self.unselectedBackColor = (255,255,255) 
     25        self.autoClearDrawings = (plat in ("Win", "Gtk")) 
     26        self.row = -1 
     27        self.col = -1 
    1128        # Create a background that will change to indicate 
    1229        # selected status.  
     
    1431        # Create a dummy circle, and store the reference 
    1532        self.circle = self.drawCircle(0,0,1) 
    16          
    17         self._selected = False 
    18         self._colors = ["blue", "green", "red", "yellow", "purple"] 
    19         self._color = self.randColor() 
    20          
    21         if self.Application.Platform == "Win": 
    22             self.selectedBackColor = (192,192,255) 
    23         else: 
    24             self.selectedBackColor = (128,128,192) 
    25         self.unselectedBackColor = (255,255,255) 
    26         self.row = -1 
    27         self.col = -1 
     33        self.DynamicVisible = lambda: not self.Popped 
    2834        self.onResize(None) 
    2935        self.update() 
    3036         
    3137     
    32     def randColor(self): 
    33         return random.choice(self._colors) 
     38    def setRandomColor(self, repaint=False): 
     39        self.Color = dabo.dColors.randomColor() 
     40        if repaint: 
     41            self.Popped = False 
     42         
     43     
     44    def update(self): 
     45        dabo.ui.callAfterInterval(50, self._delayedUpdate) 
     46    def _delayedUpdate(self): 
     47        circ = self.circle 
     48        back = self.back 
     49        circ.AutoUpdate = back.AutoUpdate = False 
     50        selct = self.Selected 
     51        poppd = self.Popped 
     52        back.FillColor = {True: self.selectedBackColor, False: self.unselectedBackColor}[selct] 
     53        circ.FillColor = {True: "white", False: self.Color}[poppd] 
     54        circ.PenWidth = {True: 0, False: 1}[poppd] 
     55        wd, ht = self.Size 
     56        back.Width, back.Height = wd, ht 
     57        pos = ( (wd/2), (ht/2) ) 
     58        rad = (min(wd, ht) / 2) 
     59        circ.Xpos = int(wd/2) 
     60        circ.Ypos = int(ht/2) 
     61        circ.Radius = rad 
     62        circ.AutoUpdate = back.AutoUpdate = True 
     63        self._needRedraw = True 
     64        super(BubblePanel, self).update() 
     65         
     66 
     67    def onResize(self, evt): 
     68        sz = self.Size 
     69        if sz != self._sizeCache: 
     70            self._sizeCache = sz 
     71            self.update() 
    3472     
    3573     
    36     def setRandomColor(self, repaint=False): 
    37         self.Color = self.randColor() 
    38         if repaint: 
    39             self.update() 
    40          
    41      
    42     def onResize(self, evt): 
    43         self.circle.AutoUpdate = self.back.AutoUpdate = False 
    44         wd = self.Width 
    45         ht = self.Height 
    46         self.back.Width, self.back.Height = wd, ht 
    47         pos = ( (wd/2), (ht/2) ) 
    48         rad = (min(ht, wd) / 2) 
    49         self.circle.Xpos = int(wd/2) 
    50         self.circle.Ypos = int(ht/2) 
    51         self.circle.Radius = rad 
    52         self.circle.AutoUpdate = self.back.AutoUpdate = True 
    53         self.update() 
    54      
    55      
    56     def update(self, evt=None): 
    57         self.circle.AutoUpdate = self.back.AutoUpdate = False 
    58         self.circle.FillColor = self.Color 
    59         if self.Color: 
    60             self.circle.PenWidth = 1 
    61         else: 
    62             self.circle.PenWidth = 0 
    63         if self.Selected: 
    64             self.back.FillColor = self.selectedBackColor 
    65         else: 
    66             self.back.FillColor = self.unselectedBackColor           
    67         self.circle.AutoUpdate = self.back.AutoUpdate = True 
    68         super(BubblePanel, self).update()        
    69  
    70  
    7174    def onMouseLeftClick(self, evt): 
    7275        self.Parent.bubbleClick(self) 
     
    7679        return self._color 
    7780         
    78     def _setColor(self, color): 
    79         if color is None: 
    80             self._color = None 
    81         else: 
    82             self._color = color.lower() 
     81    def _setColor(self, val): 
     82        if val != self._color: 
     83            if val is None: 
     84                self._color = None 
     85            else: 
     86                self._color = val.lower() 
    8387 
    8488             
     89    def _getPopped(self): 
     90        return self._popped 
     91 
     92    def _setPopped(self, val): 
     93        if val != self._popped: 
     94            self._popped = self.Visible = val 
     95            self.update() 
     96 
     97 
    8598    def _getSelected(self): 
    8699        return self._selected 
    87100 
    88     def _setSelected(self, sel): 
    89         self._selected = sel 
     101    def _setSelected(self, val): 
     102        if val != self._selected: 
     103            self._selected = val 
    90104 
    91105 
     
    93107            _("Color for this bubble  (str or tuple)")) 
    94108 
     109    Popped = property(_getPopped, _setPopped, None, 
     110            _("Is the bubble popped?  (bool)")) 
     111 
    95112    Selected = property(_getSelected, _setSelected, None, 
    96113            _("Selection Status  (bool)")) 
    97114     
     115 
     116    _proxyDict = {} 
     117    Visible = makeProxyProperty(_proxyDict, "Visible", ("circle", )) 
  • trunk/demo/samples/games/bubblet/BubbletForm.py

    r3562 r3816  
    11# -*- coding: utf-8 -*- 
     2import time 
    23import dabo 
    34dabo.ui.loadUI("wx") 
     
    89from BubbleBizobj import BubbleBizobj 
    910from StatsForm import StatsForm 
    10 import time 
    1111 
    1212 
     
    7878     
    7979        self.unbindEvent(dEvents.Paint) 
     80        biz.newGame() 
     81        dabo.ui.callAfter(self.update) 
    8082 
    8183     
     
    8385        """Saves a screenshot of the current board.""" 
    8486        dabo.ui.saveScreenShot(self) 
     87 
     88 
     89    def refresh(self): 
     90        dabo.ui.callAfterInterval(100, self._refresh) 
     91    def _refresh(self): 
     92        super(BubbletForm, self).refresh() 
    8593 
    8694 
     
    108116        self.tmr.start(100) 
    109117         
    110         
     118     
    111119    def onTimer(self, evt): 
    112120        self.tmr.stop() 
     
    158166                 
    159167 
    160 #   def repaint(self): 
    161 #       for obj in self.Children: 
    162 #           if isinstance(obj, BubblePanel): 
    163 #               obj.repaint(True) 
    164  
    165  
    166168    def _getBizobj(self): 
    167169        return self.PrimaryBizobj