From 96b6b6a92bb1021dd4a818a76e1c59af6d682b74 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 03 Feb 2010 19:16:53 +0000 Subject: saving/restoring multiple turtles; fixed turtle selection bug --- diff --git a/constants.py b/constants.py index e9343dd..f5ab419 100644 --- a/constants.py +++ b/constants.py @@ -51,7 +51,7 @@ PALETTES = [['forward', 'back', 'clean', 'left', 'right', 'show', 'storein', 'start'], ['kbinput', 'push', 'printheap', 'keyboard', 'pop', 'clearheap', 'myfunc', 'nop', 'leftpos', 'toppos', 'width', 'rightpos', - 'bottompos', 'height', 'turtle', 'print'], + 'bottompos', 'height', 'addturtle', 'print'], ['journal', 'audio', 'description', 'templatelist', 'template1x1a', 'template1x1', 'template1x2', 'template2x1', 'template2x2', 'hideblocks', 'showblocks'], @@ -102,7 +102,7 @@ BASIC_STYLE = ['clean', 'penup', 'pendown', 'stack1', 'stack2', 'vspace', BASIC_STYLE_1ARG = ['forward', 'back', 'left', 'right', 'seth', 'show', 'setscale', 'setpensize', 'setcolor', 'setshade', 'print', 'settextsize', 'settextcolor', 'print', 'wait', 'storeinbox1', - 'storeinbox2', 'wait', 'stack', 'push', 'nop', 'turtle'] + 'storeinbox2', 'wait', 'stack', 'push', 'nop', 'addturtle'] BASIC_STYLE_2ARG = ['arc', 'setxy', 'fillscreen', 'storein'] BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pensize', 'color', 'shade', 'textcolor', 'textsize', 'box1', 'box2', 'string', 'leftpos', 'scale', @@ -164,6 +164,7 @@ CONTENT_BLOCKS = ['number', 'string', 'description', 'audio', 'journal'] # block name dictionary used for labels # BLOCK_NAMES = { + 'addturtle':[_('turtle')], 'and2':[_('and')], 'arc':[_('arc'),_('angle'),_('radius')], 'audio':[' '], @@ -266,6 +267,7 @@ BLOCK_NAMES = { # PRIMITIVES = { + 'addturtle':'turtle', 'and2':'and', 'arc':'arc', 'back':'back', @@ -351,7 +353,6 @@ PRIMITIVES = { 'template2x2':'t2x2', 'textsize':'textsize', 'toppos':'tpos', - 'turtle':'turtle', 'vspace':'nop', 'wait':'wait', 'width':'hres', @@ -364,6 +365,7 @@ PRIMITIVES = { # DEFAULTS = { + 'addturtle':[1], 'arc':[90,100], 'audio':[None], 'back':[100], @@ -402,7 +404,6 @@ DEFAULTS = { 'template2x1':[_('Title'), 'None', 'None'], 'template2x2':[_('Title'), 'None', 'None', 'None', 'None'], 'templatelist':[_('Title'), '∙ '], - 'turtle':[1], 'wait':[1]} # @@ -490,6 +491,7 @@ SPECIAL_NAMES = { # Help messages # HELP_STRINGS = { + 'addturtle':_("choose which turtle to command"), 'and2':_("logical AND operator"), 'arc':_("move turtle along an arc"), 'audio':_("Sugar Journal audio object"), diff --git a/talogo.py b/talogo.py index 09c18b4..ec63d42 100644 --- a/talogo.py +++ b/talogo.py @@ -402,7 +402,6 @@ class LogoCode: self.procstop = False list = self.readline(str) self.step = self.start_eval(list) - # self.tw.turtle_list.show_all() """ Convert the pseudocode into a list of commands. @@ -574,11 +573,11 @@ class LogoCode: print "step is None" return False except StopIteration: - self.tw.active_turtle.show() + self.tw.turtle_list.show_all() return False except logoerror, e: self.showlabel(str(e)[1:-1]) - self.tw.active_turtle.show() + self.tw.turtle_list.show_all() return False return True diff --git a/taturtle.py b/taturtle.py index acf051e..9170413 100644 --- a/taturtle.py +++ b/taturtle.py @@ -47,6 +47,9 @@ class Turtles: else: return(self.list[i]) + def get_turtle_index(self, turtle): + return(self.list.index(turtle)) + def turtle_count(self): return(len(self.list)) @@ -110,13 +113,12 @@ class Turtle: def set_heading(self, heading): self.heading = heading i = (int(self.heading+5)%360)/10 - try: - if self.hidden is False: + if self.hidden is False: + try: self.spr.set_shape(self.shapes[i]) - except IndexError: - if self.hidden is False: + except IndexError: self.spr.set_shape(self.shapes[0]) - print "Turtle shape IndexError %f -> %d" % (heading, i) + print "Turtle shape IndexError %f -> %d" % (heading, i) def set_color(self, color): self.pen_color = color diff --git a/tawindow.py b/tawindow.py index 4309912..e598824 100644 --- a/tawindow.py +++ b/tawindow.py @@ -902,6 +902,7 @@ class TurtleArtWindow(): t = self.turtle_list.spr_to_turtle(spr) if t is not None: self.selected_turtle = t + self.canvas.set_turtle(self.turtle_list.get_turtle_index(t)) self._turtle_pressed(x, y) return True @@ -1499,8 +1500,8 @@ class TurtleArtWindow(): t = 0 for b in data: if b[1] == 'turtle': - self.load_turtle(b) - t = 1 + self.load_turtle(b, t) + t += 1 else: blk = self.load_block(b) blocks.append(blk) @@ -1659,8 +1660,9 @@ class TurtleArtWindow(): """ Restore a turtle from its saved state """ - def load_turtle(self, b): + def load_turtle(self, b, i=0): id, name, xcor, ycor, heading, color, shade, pensize = b + self.canvas.set_turtle(i) self.canvas.setxy(xcor, ycor) self.canvas.seth(heading) self.canvas.setcolor(color) @@ -1724,10 +1726,13 @@ class TurtleArtWindow(): data.append((b.id, name, sx-self.canvas.cx, sy-self.canvas.cy, connections)) if save_turtle is True: - data.append((-1,'turtle', - self.canvas.xcor, self.canvas.ycor, self.canvas.heading, - self.canvas.color, self.canvas.shade, - self.canvas.pensize)) + for t in range(self.turtle_list.turtle_count()): + self.canvas.set_turtle(t) + data.append((-1,'turtle', + self.canvas.xcor, self.canvas.ycor, + self.canvas.heading, + self.canvas.color, self.canvas.shade, + self.canvas.pensize)) return data """ -- cgit v0.9.1