From ebea2e093160429794c0ba49a5560c53adbc331c Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 04 Nov 2011 20:11:35 +0000 Subject: general cleanup; removed some unused code --- (limited to 'TurtleArt') diff --git a/TurtleArt/tacanvas.py b/TurtleArt/tacanvas.py index dce31a2..c025d5a 100644 --- a/TurtleArt/tacanvas.py +++ b/TurtleArt/tacanvas.py @@ -36,7 +36,7 @@ from taconstants import BLACK, WHITE def wrap100(n): - """ A variant on mod... 101 -> 99; 199 -> 1 """ + ''' A variant on mod... 101 -> 99; 199 -> 1 ''' n = int(n) n %= 200 if n > 99: @@ -45,7 +45,7 @@ def wrap100(n): def calc_shade(c, s, invert=False): - """ Convert a color to the current shade (lightness/darkness). """ + ''' Convert a color to the current shade (lightness/darkness). ''' # Assumes 16 bit input values if invert: if s < 0: @@ -58,7 +58,7 @@ def calc_shade(c, s, invert=False): def calc_gray(c, g, invert=False): - """ Gray is a psuedo saturation calculation. """ + ''' Gray is a psuedo saturation calculation. ''' # Assumes 16 bit input values if g == 100: return c @@ -98,10 +98,10 @@ COLOR_TABLE = ( class TurtleGraphics: - """ A class for the Turtle graphics canvas """ + ''' A class for the Turtle graphics canvas ''' def __init__(self, tw, width, height): - """ Create a sprite to hold the canvas. """ + ''' Create a sprite to hold the canvas. ''' self.tw = tw self.width = width self.height = height @@ -131,14 +131,14 @@ class TurtleGraphics: self.tw.svg_string = '' def start_fill(self): - """ Start accumulating points of a polygon to fill. """ + ''' Start accumulating points of a polygon to fill. ''' self.fill = True self.poly_points = [] if self.tw.saving_svg: self.tw.svg_string += '' def stop_fill(self): - """ Fill the polygon. """ + ''' Fill the polygon. ''' self.fill = False if len(self.poly_points) == 0: return @@ -148,7 +148,7 @@ class TurtleGraphics: for p in self.poly_points: shared_poly_points.append((self.screen_to_turtle_coordinates( p[0], p[1]))) - event = "F|%s" % (data_to_string([self._get_my_nick(), + event = 'F|%s' % (data_to_string([self._get_my_nick(), shared_poly_points])) self.tw.send_event(event) self.poly_points = [] @@ -156,7 +156,7 @@ class TurtleGraphics: self.tw.svg_string += '' def fill_polygon(self, poly_points): - """ Draw the polygon... """ + ''' Draw the polygon... ''' self.canvas.new_path() for i, p in enumerate(poly_points): if p[0] == 'move': @@ -170,7 +170,7 @@ class TurtleGraphics: self.canvas.close_path() self.canvas.fill() if self.tw.saving_svg and self.pendown: - self.svg.set_fill_color("#%02x%02x%02x" % (self.fgrgb[0], + self.svg.set_fill_color('#%02x%02x%02x' % (self.fgrgb[0], self.fgrgb[1], self.fgrgb[2])) self.tw.svg_string += self.svg.new_path(poly_points[0][0], @@ -179,12 +179,12 @@ class TurtleGraphics: if p > 0: self.tw.svg_string += self.svg.line_to(poly_points[p][0], poly_points[p][1]) - self.tw.svg_string += "\"\n" + self.tw.svg_string += '"\n' self.tw.svg_string += self.svg.style() self.svg.set_fill_color('none') def clearscreen(self, share=True): - """Clear the canvas and reset most graphics attributes to defaults.""" + '''Clear the canvas and reset most graphics attributes to defaults.''' self.canvas.move_to(0, 0) self.canvas.set_source_rgb(self.bgrgb[0] / 255., self.bgrgb[1] / 255., @@ -217,7 +217,7 @@ class TurtleGraphics: self.set_turtle(self.tw.default_turtle_name) def forward(self, n, share=True): - """ Move the turtle forward.""" + ''' Move the turtle forward.''' nn = n * self.tw.coord_scale self.canvas.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255., self.fgrgb[2] / 255.) @@ -226,7 +226,7 @@ class TurtleGraphics: self.xcor += nn * sin(self.heading * DEGTOR) self.ycor += nn * cos(self.heading * DEGTOR) except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return if self.pendown: @@ -235,7 +235,7 @@ class TurtleGraphics: self.move_turtle() if self.tw.sharing() and share: - event = "f|%s" % (data_to_string([self._get_my_nick(), int(n)])) + event = 'f|%s' % (data_to_string([self._get_my_nick(), int(n)])) self.tw.send_event(event) self.tw.window.queue_draw_area(0, 0, @@ -243,37 +243,37 @@ class TurtleGraphics: self.height) def seth(self, n, share=True): - """ Set the turtle heading. """ + ''' Set the turtle heading. ''' try: self.heading = n except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: - event = "r|%s" % (data_to_string([self._get_my_nick(), + event = 'r|%s' % (data_to_string([self._get_my_nick(), round_int(self.heading)])) self.tw.send_event(event) def right(self, n, share=True): - """ Rotate turtle clockwise """ + ''' Rotate turtle clockwise ''' try: self.heading += n except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.heading %= 360 self.turn_turtle() if self.tw.sharing() and share: - event = "r|%s" % (data_to_string([self._get_my_nick(), + event = 'r|%s' % (data_to_string([self._get_my_nick(), round_int(self.heading)])) self.tw.send_event(event) def arc(self, a, r, share=True): - """ Draw an arc """ + ''' Draw an arc ''' self.canvas.set_source_rgb(self.fgrgb[0] / 255., self.fgrgb[1] / 255., self.fgrgb[2] / 255.) try: @@ -282,17 +282,17 @@ class TurtleGraphics: else: self.rarc(a, r) except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.move_turtle() if self.tw.sharing() and share: - event = "a|%s" % (data_to_string([self._get_my_nick(), + event = 'a|%s' % (data_to_string([self._get_my_nick(), [round_int(a), round_int(r)]])) self.tw.send_event(event) def rarc(self, a, r): - """ draw a clockwise arc """ + ''' draw a clockwise arc ''' r *= self.tw.coord_scale if r < 0: r = -r @@ -311,9 +311,9 @@ class TurtleGraphics: self.canvas.stroke() self.inval() - if self.fill and self.poly_points == []: - self.poly_points.append(('move', x, y)) if self.fill: + if self.poly_points == []: + self.poly_points.append(('move', x, y)) self.poly_points.append(('rarc', x, y, r, (self.heading - 180) * DEGTOR, (self.heading - 180 + a) * DEGTOR)) @@ -330,7 +330,7 @@ class TurtleGraphics: self.tw.svg_string += self.svg.style() def larc(self, a, r): - """ draw a counter-clockwise arc """ + ''' draw a counter-clockwise arc ''' r *= self.tw.coord_scale if r < 0: r = -r @@ -349,9 +349,9 @@ class TurtleGraphics: self.canvas.stroke() self.inval() - if self.fill and self.poly_points == []: - self.poly_points.append(('move', x, y)) if self.fill: + if self.poly_points == []: + self.poly_points.append(('move', x, y)) self.poly_points.append(('larc', x, y, r, (self.heading) * DEGTOR, (self.heading - a) * DEGTOR)) @@ -368,14 +368,14 @@ class TurtleGraphics: self.tw.svg_string += self.svg.style() def setxy(self, x, y, share=True, pendown=True): - """ Move turtle to position x,y """ + ''' Move turtle to position x,y ''' oldx, oldy = self.xcor, self.ycor x *= self.tw.coord_scale y *= self.tw.coord_scale try: self.xcor, self.ycor = x, y except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return @@ -388,49 +388,49 @@ class TurtleGraphics: self.move_turtle() if self.tw.sharing() and share: - event = "x|%s" % (data_to_string([self._get_my_nick(), + event = 'x|%s' % (data_to_string([self._get_my_nick(), [round_int(x), round_int(y)]])) self.tw.send_event(event) def setpensize(self, ps, share=True): - """ Set the pen size """ + ''' Set the pen size ''' try: if ps < 0: ps = 0 self.pensize = ps except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.tw.active_turtle.set_pen_size(ps) self.canvas.set_line_width(ps) self.svg.set_stroke_width(self.pensize) if self.tw.sharing() and share: - event = "w|%s" % (data_to_string([self._get_my_nick(), + event = 'w|%s' % (data_to_string([self._get_my_nick(), round_int(ps)])) self.tw.send_event(event) def setcolor(self, c, share=True): - """ Set the pen color """ + ''' Set the pen color ''' try: self.color = c except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.tw.active_turtle.set_color(c) self.set_fgcolor() if self.tw.sharing() and share: - event = "c|%s" % (data_to_string([self._get_my_nick(), + event = 'c|%s' % (data_to_string([self._get_my_nick(), round_int(c)])) self.tw.send_event(event) def setgray(self, g, share=True): - """ Set the gray level """ + ''' Set the gray level ''' try: self.gray = g except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return if self.gray < 0: @@ -440,43 +440,43 @@ class TurtleGraphics: self.set_fgcolor() self.tw.active_turtle.set_gray(self.gray) if self.tw.sharing() and share: - event = "g|%s" % (data_to_string([self._get_my_nick(), + event = 'g|%s' % (data_to_string([self._get_my_nick(), round_int(self.gray)])) self.tw.send_event(event) def set_textcolor(self): - """ Deprecated: Set the text color to foreground color. """ + ''' Deprecated: Set the text color to foreground color. ''' return def settextcolor(self, c): # deprecated - """ Set the text color """ + ''' Set the text color ''' return def settextsize(self, c): # deprecated - """ Set the text size """ + ''' Set the text size ''' try: self.tw.textsize = c except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) def setshade(self, s, share=True): - """ Set the color shade """ + ''' Set the color shade ''' try: self.shade = s except TypeError, ValueError: - debug_output("bad value sent to %s" % (__name__), + debug_output('bad value sent to %s' % (__name__), self.tw.running_sugar) return self.tw.active_turtle.set_shade(s) self.set_fgcolor() if self.tw.sharing() and share: - event = "s|%s" % (data_to_string([self._get_my_nick(), + event = 's|%s' % (data_to_string([self._get_my_nick(), round_int(s)])) self.tw.send_event(event) def fillscreen(self, c, s): - """ Fill screen with color/shade and reset to defaults """ + ''' Fill screen with color/shade and reset to defaults ''' oldc, olds = self.color, self.shade self.setcolor(c, False) self.setshade(s, False) @@ -495,7 +495,7 @@ class TurtleGraphics: self.poly_points = [] def set_fgcolor(self): - """ Set the foreground color """ + ''' Set the foreground color ''' if self.color == WHITE or self.shade == WHITE: r = 0xFF00 g = 0xFF00 @@ -517,20 +517,20 @@ class TurtleGraphics: b = calc_gray(b, self.gray) b = calc_shade(b, sh) self.fgrgb = [r >> 8, g >> 8, b >> 8] - self.svg.set_stroke_color("#%02x%02x%02x" % (self.fgrgb[0], + self.svg.set_stroke_color('#%02x%02x%02x' % (self.fgrgb[0], self.fgrgb[1], self.fgrgb[2])) def setpen(self, bool, share=True): - """ Lower or raise the pen """ + ''' Lower or raise the pen ''' self.pendown = bool self.tw.active_turtle.set_pen_state(bool) if self.tw.sharing() and share: - event = "p|%s" % (data_to_string([self._get_my_nick(), bool])) + event = 'p|%s' % (data_to_string([self._get_my_nick(), bool])) self.tw.send_event(event) def draw_pixbuf(self, pixbuf, a, b, x, y, w, h, path, share=True): - """ Draw a pixbuf """ + ''' Draw a pixbuf ''' # Fix me: rotate image r = sqrt(x * x + y * y) @@ -576,7 +576,7 @@ class TurtleGraphics: height = pixbuf.get_height() width = pixbuf.get_width() x, y = self.screen_to_turtle_coordinates(x, y) - event = "P|%s" % (data_to_string([self._get_my_nick(), + event = 'P|%s' % (data_to_string([self._get_my_nick(), [round_int(a), round_int(b), round_int(x), round_int(y), round_int(w), round_int(h), @@ -586,7 +586,7 @@ class TurtleGraphics: self.tw.send_event(event) def draw_text(self, label, x, y, size, w, share=True): - """ Draw text """ + ''' Draw text ''' w *= self.tw.coord_scale cr = pangocairo.CairoContext(self.canvas) pl = cr.create_layout() @@ -614,66 +614,58 @@ class TurtleGraphics: self.tw.svg_string += self.svg.text(x, # - self.width / 2, y + size, size, w, label) if self.tw.sharing() and share: - event = "W|%s" % (data_to_string([self._get_my_nick(), + event = 'W|%s' % (data_to_string([self._get_my_nick(), [label, round_int(x), round_int(y), round_int(size), round_int(w)]])) self.tw.send_event(event) def turtle_to_screen_coordinates(self, x, y): - """ The origin of turtle coordinates is the center of the screen """ + ''' The origin of turtle coordinates is the center of the screen ''' return self.width / 2. + x, self.invert_y_coordinate(y) def screen_to_turtle_coordinates(self, x, y): - """ The origin of the screen coordinates is the upper left corner """ + ''' The origin of the screen coordinates is the upper left corner ''' return x - self.width / 2., self.invert_y_coordinate(y) def invert_y_coordinate(self, y): - """ Positive y goes up in turtle coordinates, down in sceeen - coordinates """ + ''' Positive y goes up in turtle coordinates, down in sceeen + coordinates ''' return self.height / 2. - y def draw_line(self, x1, y1, x2, y2): - """ Draw a line """ + ''' Draw a line ''' x1, y1 = self.turtle_to_screen_coordinates(x1, y1) x2, y2 = self.turtle_to_screen_coordinates(x2, y2) - if x1 < x2: - minx, maxx = int(x1), int(x2) - else: - minx, maxx = int(x2), int(x1) - if y1 < y2: - miny, maxy = int(y1), int(y2) - else: - miny, maxy = int(y2), int(y1) - w, h = maxx - minx, maxy - miny self.canvas.move_to(x1, y1) self.canvas.line_to(x2, y2) self.canvas.stroke() - if self.fill and self.poly_points == []: - self.poly_points.append(('move', x1, y1)) if self.fill: + if self.poly_points == []: + self.poly_points.append(('move', x1, y1)) self.poly_points.append(('line', x2, y2)) + if self.tw.saving_svg and self.pendown: self.tw.svg_string += self.svg.new_path(x1, y1) self.tw.svg_string += self.svg.line_to(x2, y2) - self.tw.svg_string += "\"\n" + self.tw.svg_string += '"\n' self.tw.svg_string += self.svg.style() def turn_turtle(self): - """ Change the orientation of the turtle """ + ''' Change the orientation of the turtle ''' self.tw.active_turtle.set_heading(self.heading) def move_turtle(self): - """ Move the turtle """ + ''' Move the turtle ''' x, y = self.turtle_to_screen_coordinates(self.xcor, self.ycor) self.tw.active_turtle.move( (int(self.cx + x - self.tw.active_turtle.spr.rect.width / 2.), int(self.cy + y - self.tw.active_turtle.spr.rect.height / 2.))) def get_color_index(self, r, g, b, a=0): - """ Find the closest palette entry to the rgb triplet """ + ''' Find the closest palette entry to the rgb triplet ''' if self.shade != 50 or self.gray != 100: r <<= 8 g <<= 8 @@ -706,7 +698,7 @@ class TurtleGraphics: return closest_color def get_pixel(self): - """ Read the pixel at x, y """ + ''' Read the pixel at x, y ''' # Fix me: Is there a more efficient way of doing this? if self.tw.interactive_mode: x, y = self.turtle_to_screen_coordinates(self.xcor, self.ycor) @@ -730,7 +722,7 @@ class TurtleGraphics: return(-1, -1, -1, -1) def set_turtle(self, k, colors=None): - """ Select the current turtle and associated pen status """ + ''' Select the current turtle and associated pen status ''' if k not in self.tw.turtles.dict: # if it is a new turtle, start it in the center of the screen self.tw.active_turtle = self.tw.turtles.get_turtle(k, True, colors) @@ -755,14 +747,15 @@ class TurtleGraphics: self.setpen(self.tw.active_turtle.get_pen_state(), False) def svg_close(self): - """ Close current SVG graphic """ + ''' Close current SVG graphic ''' if self.tw.svg_string == '': return self.svg.calc_w_h(False) - self.tw.svg_string = "%s%s%s%s" % (self.svg.header(True), - self.svg.background("#%02x%02x%02x" % \ - (self.bgrgb[0], self.bgrgb[1], self.bgrgb[2])), - self.tw.svg_string, self.svg.footer()) + self.tw.svg_string = '%s %s %s %s' % ( + self.svg.header(True), + self.svg.background('#%02x%02x%02x' % ( + self.bgrgb[0], self.bgrgb[1], self.bgrgb[2])), + self.tw.svg_string, self.svg.footer()) def _get_my_nick(self): return self.tw.nick -- cgit v0.9.1