From: Alex T. <al...@tw...> - 2005-09-28 14:43:38
|
Andrew P wrote: > Hi Alex, > > Let me explain a little more what I'm trying to do. First off. here > is a screenshot of what I did today: > Cool. Thanks Andrew - I have a much better idea now. Here's what I believe (tell me if I'm wrong), followed by what I conclude from those beliefs. You're doing CA where each row depends on the previous row (or perhaps in the future on previous rows). Once a cell's value has been determined, it never changes. You need to scroll as the cells are determined - not backwards and forwards under user control. From that I conclude: You should calculate one row at a time, draw it, and then move on to the next one. You should use one one-dim arrays (lists) to hold the previous and current rows You should scroll by using bit-blits, not by re-drawing. so (after the initial phase to fill the window), what you'll do in each cycle through the loop is calculate the next line scroll the current window up by one line draw in newly calculated row at bottom of window. Here's the core part of a sample program to do that .... no doubt it could be improved, but I kept it as simple so I could figure it out. I use a button called "Change" so I can single-step through the process, and when I run with the timer, I simply call the single-step function - could certainly be cleaned up :-) (And for the boundary condition at start/end of the list, I used a try... except ... - very lazy of me). > def draw_row(self, y): > points = [] > pens = [] > for i in range(self.width): > if self.cells[i]: > points.append( (i,y) ) > pens.append( self.basicpen ) > self.components.bmpCanvas.drawPointList( points, pens ) > > > def on_Change_mouseClick(self, event): > if self.row == 0: > self.draw_row(0) > self.row += 1 > return > > start = time.clock() > > if self.row >= self.height: > cv = self.components.bmpCanvas > cv.blit( (0,0), (self.width,self.height), cv._bufImage, (0,1)) > cv.drawRectangleList( [ (0,self.height, self.width,1)], > [self.whitepen]) > y = self.height > else: > y = self.row > > cells = self.change(self.cells) > self.cells = cells > > self.draw_row(y) > self.row += 1 > > ## print "took ", time.clock()-start > return > > def on_bmpCanvas_timer(self, event): > self.on_Change_mouseClick(event) > return > > > def change(self, cells): > new = [] > rule = [ 0, 1, 1, 0, 1, 0, 0, 1 ] > > for i in range(len(cells)): > try: > newval = 4*cells[i-1] + 2*cells[i] + cells[i+1] > except: > newval = 0 > new.append( rule[newval] ) > return new If you want the entire sample, let me know .... -- Alex Tweedly http://www.tweedly.net -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.344 / Virus Database: 267.11.6/111 - Release Date: 23/09/2005 |