[Edu-sig] Re: Student teaching and tutorials

Lee Harr missive@hotmail.com
Wed, 09 Jul 2003 13:33:39 +0000


>>This is my book that is based on pygame:
>>
>>http://staff.easthighschool.net/lee/computers/book/
>>
>Looks very interesting!
>BTW: yesterday (no joke!) I discovered, that ther is a function
>hypot(x,y) in module math. Maybe helpful for young students ;-)
>

Good point. I found about 10 places in my code where I had
used sqrt() instead.


>Do you think it would be easy to implement the
>mini - Lsystem - example (posted 12 hours ago) using pygsear
>(instead of my extended turtle.py) ?
>

I just made a new version (http://savannah.nongnu.org/files/?group=pygsear)
which includes translations of your code.  I cannot guarantee it does
exactly the same thing it did before, but it does something interesting  :o)

Here is an excerpt from examples/penguin.py   :

    def cellular(self, code="04444333221", cellsize=4):
        """Adapted from code posted to
        edu-sig@python.org by Gregor Lindl"""

        w, h = conf.WINSIZE
        cols = w/cellsize
        lines = h/cellsize

        code = code[:13]    # What does the 13 mean?
        code = code + "0"*(13-len(code))
        world = []
        topline = [0]*cols
        topline[cols/2] = 1
        for z in range(lines):
            world.append(topline)
            newline = [0]*cols
            for s in range(1, cols-1):
                sum = topline[s-1] + topline[s] + topline[s+1]
                newline[s] = int(code[sum])
            topline = newline

        for y in range(lines):
            for x in range(cols):
                color = COLORS[world[y][x]]
                self.moveTo((x*cellsize, y*cellsize))
                self.square(cellsize, color=color, width=0)


    def lindenmayer(self, axiom="F",
                              regulator={"F":"FF+[+F-F-F]-[-F+F+F]"},
                              deep=3, walked=9, angle=180/8.0,
                              startpos=None, startdeg=None):
        """Adapted from code posted to
        edu-sig@python.org by Gregor Lindl"""

        self.tstack = []

        def l(angle=angle):
            self.left(angle)
        def r(angle=angle):
            self.right(angle)
        def turn():
            self.left(180)

        def f(n=walked):
            self.penUp()
            self.forward(n)
        def F(n=walked):
            self.penDown()
            self.forward(n)

        def save():
            self.tstack.append((self.getPosition(), self.getDeg()))
        def load():
            position, deg = self.tstack.pop()
            self.moveTo(position)
            self.turnTo(deg)

        regulate = {"-":l, "+":r, "F": F, "f":f,
                        "|": turn, "[":save, "]":load}

        def replace(follow, regulator, n):
            for i in range(n):
                newfollow = ""
                for element in follow:
                    newfollow = newfollow + regulator.get(element, element)
                follow = newfollow
            return follow

        if startpos is not None:
            self.moveTo(startpos)
        if startdeg is not None:
            self.turnTo(startdeg)

        self.step, self.angle = walked, angle
        design = replace(axiom, regulator, deep)
        for b in design:
            try:
                regulate[b]()
            except KeyError:
                pass

_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*. 
http://join.msn.com/?page=features/featuredemail