diff options
author | Walter Bender <walter@walter-laptop.(none)> | 2010-02-04 17:01:55 (GMT) |
---|---|---|
committer | Walter Bender <walter@walter-laptop.(none)> | 2010-02-04 17:01:55 (GMT) |
commit | 93d8d0137c32baeeffb3bfaf20d622d9143b3541 (patch) | |
tree | 0d8a598c18de95630b0a5a3f9085c49e6b25e65f /taturtle.py | |
parent | 0e8294575bddcde8e77f347f13033407142ef8f6 (diff) |
multiturtle refactoring
Diffstat (limited to 'taturtle.py')
-rw-r--r-- | taturtle.py | 101 |
1 files changed, 54 insertions, 47 deletions
diff --git a/taturtle.py b/taturtle.py index 0933c0c..889374f 100644 --- a/taturtle.py +++ b/taturtle.py @@ -21,66 +21,77 @@ from taconstants import * from sprite_factory import SVG, svg_str_to_pixbuf -import sprites +from sprites import Sprite from gettext import gettext as _ +def generate_turtle_pixbufs(colors): + shapes = [] + svg = SVG() + svg.set_scale(1.0) + for i in range(36): + svg.set_orientation(i*10) + shapes.append(svg_str_to_pixbuf(svg.turtle(colors))) + return shapes + # # A class for the list of blocks and everything they share in common # class Turtles: def __init__(self, sprite_list): - self.list = [] + self.dict = dict() self.sprite_list = sprite_list + self.default_pixbufs = [] - def get_turtle(self, i, append=False): - if i < 0: - print "IndexError: Turtles %d" % (i) - return self.list[0] - elif i > len(self.list)-1: - if append is False: - print "IndexError: Turtles %d" % (i) - return self.list[0] - else: - for t in range(i-len(self.list)+1): - Turtle(self) - return(self.list[i]) + def get_turtle(self, k, append=False): + if self.dict.has_key(k): + return self.dict[k] + elif append is False: + return None else: - return(self.list[i]) + Turtle(self, k) + return self.dict[k] - def get_turtle_index(self, turtle): - return(self.list.index(turtle)) + def get_turtle_key(self, turtle): + for k in iter(self.dict): + if self.dict[k] == turtle: + return k + return None def turtle_count(self): - return(len(self.list)) + return(len(self.dict)) - def append_to_list(self, turtle): - self.list.append(turtle) + def add_to_dict(self, k, turtle): + self.dict[k] = turtle - def remove_from_list(self, turtle): - if turtle in self.list: - self.list.remove(turtle) + def remove_from_dict(self, k): + if self.dict.has_key(k): + del(self.dict[k]) def show_all(self): - for turtle in self.list: - turtle.show() + for k in iter(self.dict): + self.dict[k].show() # # sprite utilities # def spr_to_turtle(self, spr): - for turtle in self.list: - if spr == turtle.spr: - return turtle + for k in iter(self.dict): + if spr == self.dict[k].spr: + return self.dict[k] return None + def get_pixbufs(self): + if self.default_pixbufs == []: + self.default_pixbufs = generate_turtle_pixbufs( + ["#008000", "#00A000", "#D0D000", "#808000"]) + return(self.default_pixbufs) + # # A class for the individual turtles # class Turtle: # The turtle is not a block, just a sprite with an orientation - def __init__(self, turtle_list, - colors=["#008000", "#00A000", "#D0D000", "#808000"], - scale=1.0): + def __init__(self, turtles, key, colors=None): self.x = 0 self.y = 0 self.hidden = False @@ -92,23 +103,19 @@ class Turtle: self.pen_size = 5 self.pen_state = True - if len(colors) == 2: - _colors = colors[:] - _colors.append(colors[0]) - _colors.append(colors[1]) - elif len(colors) == 4: - _colors=colors[:] + if colors is None: + self.shapes = turtles.get_pixbufs() else: - _colors=["#008000", "#00A000", "#D0D000", "#808000"] - - _svg = SVG() - _svg.set_scale(scale) - self.spr = sprites.Sprite(turtle_list.sprite_list, self.x, self.y, - svg_str_to_pixbuf(_svg.turtle(_colors))) - turtle_list.append_to_list(self) - for i in range(36): - _svg.set_orientation(i*10) - self.shapes.append(svg_str_to_pixbuf(_svg.turtle(_colors))) + if len(colors) == 2: + self.colors = colors[:] + self.colors.append(colors[0]) + self.colors.append(colors[1]) + elif len(colors) == 4: + self.colors=colors[:] + self.shapes = generate_turtle_pixbufs(self.colors) + + self.spr = Sprite(turtles.sprite_list, 0, 0, self.shapes[0]) + turtles.add_to_dict(key, self) def set_heading(self, heading): self.heading = heading |