Changeset 3816
- Timestamp:
- 12/18/2007 08:22:03 AM (1 year ago)
- Files:
-
- trunk/demo/DaboDemo.py (modified) (1 diff)
- trunk/demo/samples/games/bubblet/BubbleBizobj.py (modified) (8 diffs)
- trunk/demo/samples/games/bubblet/BubblePanel.py (modified) (4 diffs)
- trunk/demo/samples/games/bubblet/BubbletForm.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/demo/DaboDemo.py
r3642 r3816 11 11 useSplash = "linux" not in platform.platform().lower() 12 12 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) 15 15 16 16 app = dabo.dApp(showSplashScreen=useSplash, splashTimeout=3000, trunk/demo/samples/games/bubblet/BubbleBizobj.py
r3562 r3816 6 6 import time 7 7 import os 8 8 9 9 10 class BubbleBizobj(biz.dBizobj): … … 26 27 for rr in self.bubbles: 27 28 for cc in rr: 28 cc.Selected = False29 29 cc.setRandomColor(True) 30 cc.Selected = cc.Popped = False 30 31 self.Score = 0 31 32 self.GameOver = False 32 33 self.IsNewHighGame = False 34 self._allBubbles = [] 35 for bubrow in self.bubbles: 36 self._allBubbles.extend(bubrow) 33 37 34 38 … … 52 56 for cc in rr: 53 57 if cc.Selected: 54 cc. Color = None58 cc.Popped = True 55 59 cc.Selected = False 56 60 self.selCount = 0 … … 64 68 rows = len(self.bubbles) 65 69 cols = len(self.bubbles[0]) 66 70 67 71 # Check if the lowest bubble is empty. 68 72 toFill = 0 69 73 for cc in range(cols): 70 if self.bubbles[rows-1][cc]. Color is None:74 if self.bubbles[rows-1][cc].Popped: 71 75 toFill += 1 72 76 … … 75 79 # work is left 76 80 self.__callbackFunc = self.callbackShift 81 self._shiftTime = time.time() 77 82 78 83 # Fill the columns 79 84 for cc in range(toFill): 80 num = random.randrange(rows ) + 185 num = random.randrange(rows-1) + 2 81 86 for ii in range(rows-num, rows): 82 87 bub = self.bubbles[ii][cc] … … 97 102 rows = len(self.bubbles) 98 103 cols = len(self.bubbles[0]) 104 shifted = [] 99 105 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 109 112 # Now shift columns to the right 110 113 for rr in range(rows): 111 gap = False112 114 for cc in range(cols-1, 0, -1): 113 115 currBub = self.bubbles[rr][cc] 114 if currBub. Color is None:116 if currBub.Popped: 115 117 # See if there are any bubbles to the left that are not empty 116 118 for cLeft in range(cc, -1, -1): 117 119 leftBub = self.bubbles[rr][cLeft] 118 if leftBub.Color is not None:120 if not leftBub.Popped: 119 121 currBub.Color = leftBub.Color 120 leftBub.Color = None 122 currBub.Popped = False 123 leftBub.Popped = True 121 124 break 122 125 … … 134 137 """ 135 138 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 144 142 break 143 145 144 if self.GameOver: 146 145 self.NumberOfGames += 1 … … 156 155 def hasMatchingNeighbor(self, bubble): 157 156 """ 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 158 164 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 195 171 196 172 def selectBubbles(self, bubble): 197 173 if bubble.Selected: 198 174 return 199 color = bubble.Color 200 if color is None: 175 if bubble.Popped: 201 176 # They clicked on an empty space 202 177 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) 247 184 248 185 249 186 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 253 191 self.selCount = 0 254 192 self.Message = _("Bubble Points: 0") trunk/demo/samples/games/bubblet/BubblePanel.py
r3562 r3816 3 3 import dabo.dEvents as dEvents 4 4 from dabo.dLocalize import _ 5 import random 5 from dabo.ui import makeProxyProperty 6 6 7 7 8 8 class BubblePanel(dabo.ui.dPanel): 9 9 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 11 28 # Create a background that will change to indicate 12 29 # selected status. … … 14 31 # Create a dummy circle, and store the reference 15 32 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 28 34 self.onResize(None) 29 35 self.update() 30 36 31 37 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() 34 72 35 73 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 = False44 wd = self.Width45 ht = self.Height46 self.back.Width, self.back.Height = wd, ht47 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 = rad52 self.circle.AutoUpdate = self.back.AutoUpdate = True53 self.update()54 55 56 def update(self, evt=None):57 self.circle.AutoUpdate = self.back.AutoUpdate = False58 self.circle.FillColor = self.Color59 if self.Color:60 self.circle.PenWidth = 161 else:62 self.circle.PenWidth = 063 if self.Selected:64 self.back.FillColor = self.selectedBackColor65 else:66 self.back.FillColor = self.unselectedBackColor67 self.circle.AutoUpdate = self.back.AutoUpdate = True68 super(BubblePanel, self).update()69 70 71 74 def onMouseLeftClick(self, evt): 72 75 self.Parent.bubbleClick(self) … … 76 79 return self._color 77 80 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() 83 87 84 88 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 85 98 def _getSelected(self): 86 99 return self._selected 87 100 88 def _setSelected(self, sel): 89 self._selected = sel 101 def _setSelected(self, val): 102 if val != self._selected: 103 self._selected = val 90 104 91 105 … … 93 107 _("Color for this bubble (str or tuple)")) 94 108 109 Popped = property(_getPopped, _setPopped, None, 110 _("Is the bubble popped? (bool)")) 111 95 112 Selected = property(_getSelected, _setSelected, None, 96 113 _("Selection Status (bool)")) 97 114 115 116 _proxyDict = {} 117 Visible = makeProxyProperty(_proxyDict, "Visible", ("circle", )) trunk/demo/samples/games/bubblet/BubbletForm.py
r3562 r3816 1 1 # -*- coding: utf-8 -*- 2 import time 2 3 import dabo 3 4 dabo.ui.loadUI("wx") … … 8 9 from BubbleBizobj import BubbleBizobj 9 10 from StatsForm import StatsForm 10 import time11 11 12 12 … … 78 78 79 79 self.unbindEvent(dEvents.Paint) 80 biz.newGame() 81 dabo.ui.callAfter(self.update) 80 82 81 83 … … 83 85 """Saves a screenshot of the current board.""" 84 86 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() 85 93 86 94 … … 108 116 self.tmr.start(100) 109 117 110 118 111 119 def onTimer(self, evt): 112 120 self.tmr.stop() … … 158 166 159 167 160 # def repaint(self):161 # for obj in self.Children:162 # if isinstance(obj, BubblePanel):163 # obj.repaint(True)164 165 166 168 def _getBizobj(self): 167 169 return self.PrimaryBizobj
