diff options
author | Walter Bender <walter@sugarlabs.org> | 2010-07-07 12:04:52 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2010-07-07 12:04:52 (GMT) |
commit | 1a369d53def607f526bc5c9af29b699d68086837 (patch) | |
tree | fe2efa9d100a9fdec5428fad1ef03e3651e4bda0 | |
parent | 75b7f49c067862dc1bccbd9c93c38b9606292d25 (diff) |
pep8
-rw-r--r-- | NEWS | 8 | ||||
-rw-r--r-- | VisualMatchActivity.py | 8 | ||||
-rw-r--r-- | activity/activity-visualmatch.svg | 6 | ||||
-rw-r--r-- | deck.py | 2 | ||||
-rw-r--r-- | game.py | 5 | ||||
-rwxr-xr-x | gencards.py | 6 | ||||
-rw-r--r-- | grid.py | 2 | ||||
-rw-r--r-- | sprites.py | 141 |
8 files changed, 92 insertions, 86 deletions
@@ -1,3 +1,11 @@ +23 + +* general code cleanup + +22 + +* fixed SVG bug in old Sugar builds + 21 * general clean up of sprite library diff --git a/VisualMatchActivity.py b/VisualMatchActivity.py index 8bc513f..4979dab 100644 --- a/VisualMatchActivity.py +++ b/VisualMatchActivity.py @@ -136,7 +136,7 @@ class VisualMatchActivity(activity.Activity): if not hasattr(self, '_saved_state'): self._saved_state = None self.vmw.new_game(self._saved_state, self._deck_index) - if self._editing_word_list is True: + if self._editing_word_list: self.vmw.editing_word_list = True self.vmw.edit_word_list() @@ -481,17 +481,17 @@ class VisualMatchActivity(activity.Activity): """ Dump game data to the journal.""" data = [] for i in self.vmw.grid.grid: - if i is None or self.vmw.editing_word_list == True: + if i is None or self.vmw.editing_word_list: data.append(None) else: data.append(i.index) for i in self.vmw.clicked: - if i is None or self.vmw.editing_word_list == True: + if i is None or self.vmw.editing_word_list: data.append(None) else: data.append(self.vmw.deck.spr_to_card(i).index) for i in self.vmw.deck.cards: - if i is None or self.vmw.editing_word_list == True: + if i is None or self.vmw.editing_word_list: data.append(None) else: data.append(i.index) diff --git a/activity/activity-visualmatch.svg b/activity/activity-visualmatch.svg index 082a3db..fa3f393 100644 --- a/activity/activity-visualmatch.svg +++ b/activity/activity-visualmatch.svg @@ -12,7 +12,7 @@ id="g2925" style="fill:&stroke_color;;fill-opacity:1;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"> <path - d="m 33.3585,62.5035 10.102,10.1 c 0.752,0.75 1.217,1.783 1.217,2.932 0,2.287 -1.855,4.143 -4.146,4.143 -1.145,0 -2.178,-0.463 -2.932,-1.211 l -10.102,-10.103 -10.1,10.1 c -0.75,0.75 -1.787,1.211 -2.934,1.211 -2.284,0 -4.143,-1.854 -4.143,-4.141 0,-1.146 0.465,-2.184 1.212,-2.934 l 10.104,-10.102 -10.102,-10.1 c -0.747,-0.748 -1.212,-1.785 -1.212,-2.93 0,-2.289 1.854,-4.146 4.146,-4.146 1.143,0 2.18,0.465 2.93,1.214 l 10.099,10.102 10.102,-10.103 c 0.754,-0.749 1.787,-1.214 2.934,-1.214 2.289,0 4.146,1.856 4.146,4.145 0,1.146 -0.467,2.18 -1.217,2.932 l -10.104,10.105 z" + d="m 33.3585,62.5035 l 10.102,10.1 c 0.752,0.75 1.217,1.783 1.217,2.932 0,2.287 -1.855,4.143 -4.146,4.143 -1.145,0 -2.178,-0.463 -2.932,-1.211 l -10.102,-10.103 -10.1,10.1 c -0.75,0.75 -1.787,1.211 -2.934,1.211 -2.284,0 -4.143,-1.854 -4.143,-4.141 0,-1.146 0.465,-2.184 1.212,-2.934 l 10.104,-10.102 -10.102,-10.1 c -0.747,-0.748 -1.212,-1.785 -1.212,-2.93 0,-2.289 1.854,-4.146 4.146,-4.146 1.143,0 2.18,0.465 2.93,1.214 l 10.099,10.102 10.102,-10.103 c 0.754,-0.749 1.787,-1.214 2.934,-1.214 2.289,0 4.146,1.856 4.146,4.145 0,1.146 -0.467,2.18 -1.217,2.932 l -10.104,10.105 z" transform="translate(10,1.5)" id="path2927" style="fill:&stroke_color;;fill-opacity:1;stroke:&stroke_color;;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none" /> @@ -22,7 +22,7 @@ id="g2925-4" style="fill:&stroke_color;;fill-opacity:1;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"> <path - d="m 33.3585,62.5035 10.102,10.1 c 0.752,0.75 1.217,1.783 1.217,2.932 0,2.287 -1.855,4.143 -4.146,4.143 -1.145,0 -2.178,-0.463 -2.932,-1.211 l -10.102,-10.103 -10.1,10.1 c -0.75,0.75 -1.787,1.211 -2.934,1.211 -2.284,0 -4.143,-1.854 -4.143,-4.141 0,-1.146 0.465,-2.184 1.212,-2.934 l 10.104,-10.102 -10.102,-10.1 c -0.747,-0.748 -1.212,-1.785 -1.212,-2.93 0,-2.289 1.854,-4.146 4.146,-4.146 1.143,0 2.18,0.465 2.93,1.214 l 10.099,10.102 10.102,-10.103 c 0.754,-0.749 1.787,-1.214 2.934,-1.214 2.289,0 4.146,1.856 4.146,4.145 0,1.146 -0.467,2.18 -1.217,2.932 l -10.104,10.105 z" + d="m 33.3585,62.5035 l 10.102,10.1 c 0.752,0.75 1.217,1.783 1.217,2.932 0,2.287 -1.855,4.143 -4.146,4.143 -1.145,0 -2.178,-0.463 -2.932,-1.211 l -10.102,-10.103 -10.1,10.1 c -0.75,0.75 -1.787,1.211 -2.934,1.211 -2.284,0 -4.143,-1.854 -4.143,-4.141 0,-1.146 0.465,-2.184 1.212,-2.934 l 10.104,-10.102 -10.102,-10.1 c -0.747,-0.748 -1.212,-1.785 -1.212,-2.93 0,-2.289 1.854,-4.146 4.146,-4.146 1.143,0 2.18,0.465 2.93,1.214 l 10.099,10.102 10.102,-10.103 c 0.754,-0.749 1.787,-1.214 2.934,-1.214 2.289,0 4.146,1.856 4.146,4.145 0,1.146 -0.467,2.18 -1.217,2.932 l -10.104,10.105 z" transform="translate(10,1.5)" id="path2927-7" /> </g> @@ -32,7 +32,7 @@ id="g3063" style="fill:none;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none"> <path - d="m 28.3575,70.160499 -5.861,5.861 -5.861,-5.866001 -4.102,-4.1 c -0.747,-0.747999 -1.212,-1.784999 -1.212,-2.93 0,-2.288998 1.854,-4.145998 4.146,-4.145998 1.143,0 2.18,0.465 2.93,1.214 l 4.099,4.101999 14.102,-14.102998 c 0.754,-0.749 1.787,-1.214 2.934,-1.214 2.289,0 4.146,1.856001 4.146,4.145001 0,1.146 -0.467,2.18 -1.217,2.932 l -14.104,14.104997 z" + d="m 28.3575,70.160499 l -5.861,5.861 -5.861,-5.866001 -4.102,-4.1 c -0.747,-0.747999 -1.212,-1.784999 -1.212,-2.93 0,-2.288998 1.854,-4.145998 4.146,-4.145998 1.143,0 2.18,0.465 2.93,1.214 l 4.099,4.101999 14.102,-14.102998 c 0.754,-0.749 1.787,-1.214 2.934,-1.214 2.289,0 4.146,1.856001 4.146,4.145001 0,1.146 -0.467,2.18 -1.217,2.932 l -14.104,14.104997 z" transform="translate(10,1.5)" id="path3065" style="fill:none;stroke:&stroke_color;;stroke-width:3;stroke-miterlimit:4;stroke-dasharray:none" /> @@ -15,7 +15,7 @@ pygtk.require('2.0') import gtk from random import randrange -from constants import HIGH, FILLS, SHAPES, NUMBER, COLORS +from constants import HIGH, FILLS, SHAPES, NUMBER, COLORS, COLOR_PAIRS from card import Card from gencards import generate_pattern_card, generate_number_card, \ generate_word_card @@ -126,7 +126,7 @@ class Game(): self.deck.hide() self.deck.shuffle() self.grid.deal(self.deck) - if self._find_a_match() is False: + if not self._find_a_match(): self.grid.deal_extra_cards(self.deck) self.matches = 0 self.robot_matches = 0 @@ -149,8 +149,7 @@ class Game(): def _sharing(self): """ Are we sharing? """ - if self.sugar is True and \ - hasattr(self.activity, 'chattube') and \ + if self.sugar and hasattr(self.activity, 'chattube') and \ self.activity.chattube is not None: return True return False diff --git a/gencards.py b/gencards.py index 3022cee..cd16e9a 100755 --- a/gencards.py +++ b/gencards.py @@ -76,8 +76,8 @@ class SVG: def _svg_check(self, x): svg_string = "%s%s%s%s%s%f%s" %\ - ("<path d=\"m 22.5,76.1 -5.9,-5.9 -4.1,-4.1 c -0.7,-0.7 -1.2,-1.8 ", - "-1.2,-2.9 0,-2.3 1.9,-4.1 4.1,-4.1 1.1,0 2.2,0.5 2.9,1.2 l ", + ("<path d=\"m 22.5,76.1 l -5.9,-5.9 -4.1,-4.1 c -0.7,-0.7 -1.2,", + "-1.8 -1.2,-2.9 0,-2.3 1.9,-4.1 4.1,-4.1 1.1,0 2.2,0.5 2.9,1.2 l ", "4.1,4.1 14.1,-14.1 c 0.8,-0.7 1.8,-1.2 2.9,-1.2 2.3,0 4.1,1.9 ", "4.1,4.1 0,1.1 -0.5,2.2 -1.2,2.9 l -14.1,14.1 -5.7,5.9 z\"\n", " transform=\"translate(",x-10,", -25)\"\n") @@ -86,7 +86,7 @@ class SVG: def _svg_cross(self, x): svg_string = "%s%s%s%s%s%s%s%s" % ( - "<path d=\"m 33.4,62.5 10.1,10.1 c 0.8,0.8 1.2,1.8 1.2,2.9 ", + "<path d=\"m 33.4,62.5 l 10.1,10.1 c 0.8,0.8 1.2,1.8 1.2,2.9 ", "0,2.3 -1.9,4.1 -4.1,4.1 -1.1,0 -2.2,-0.5 -2.9,-1.2 l ", "-10.1,-10.1 -10.1,10.1 c -0.8,0.8 -1.8,1.2 -2.9,1.2 -2.3,0 ", "-4.1,-1.9 -4.1,-4.1 0,-1.1 0.5,-2.2 1.2,-2.9 l 10.1,-10.1 ", @@ -47,7 +47,7 @@ class Grid: But only if there are still cards in the deck and only 12 cards in the grid """ - if deck.empty() is False and self.cards_in_grid() == DEAL: + if not deck.empty() and self.cards_in_grid() == DEAL: for c in range(0, COL): i = self.grid.index(None) self.grid[i] = deck.deal_next_card() @@ -75,14 +75,12 @@ def svg_str_to_pixbuf(svg_string): import pygtk pygtk.require('2.0') import gtk -import gobject import pango -# -# A class for the list of sprites and everything they share in common -# class Sprites: + """ A class for the list of sprites and everything they share in common """ + def __init__(self, canvas, area=None, gc=None): self.canvas = canvas if area == None: @@ -109,7 +107,7 @@ class Sprites: def insert_in_list(self, spr, i): if i < 0: self.list.insert(0, spr) - elif i > len(self.list)-1: + elif i > len(self.list) - 1: self.list.append(spr) else: self.list.insert(i, spr) @@ -122,17 +120,18 @@ class Sprites: list = self.list[:] list.reverse() for spr in list: - if spr.hit(pos): return spr + if spr.hit(pos): + return spr return None def redraw_sprites(self): for spr in self.list: spr.draw() -# -# A class for the individual sprites -# + class Sprite: + """ A class for the individual sprites """ + def __init__(self, sprites, x, y, image): self._sprites = sprites self._x = int(x) @@ -147,20 +146,20 @@ class Sprite: self._color = None self._width = 0 self._height = 0 - self._margins = [0,0,0,0] + self._margins = [0, 0, 0, 0] self.layer = 100 self.labels = [] self.images = [] - self._dx = [] # image offsets + self._dx = [] # image offsets self._dy = [] self.set_image(image) self._sprites.append_to_list(self) def set_image(self, image, i=0, dx=0, dy=0): - while len(self.images) < i+1: - self.images.append(None) - self._dx.append(0) - self._dy.append(0) + while len(self.images) < i + 1: + self.images.append(None) + self._dx.append(0) + self._dy.append(0) self.images[i] = image self._dx[i] = dx self._dy[i] = dy @@ -169,7 +168,7 @@ class Sprite: _h = self.images[i].get_height() else: _w, _h = self.images[i].get_size() - if i == 0: # Always reset width and height when base image changes. + if i == 0: # Always reset width and height when base image changes. self._width = _w + dx self._height = _h + dy else: @@ -180,7 +179,7 @@ class Sprite: def move(self, pos): self.inval() - self._x,self._y = int(pos[0]),int(pos[1]) + self._x, self._y = int(pos[0]), int(pos[1]) self.inval() def move_relative(self, pos): @@ -218,25 +217,25 @@ class Sprite: self._extend_labels_array(i) if type(new_label) is str or type(new_label) is unicode: # pango doesn't like nulls - self.labels[i] = new_label.replace("\0"," ") + self.labels[i] = new_label.replace("\0", " ") else: self.labels[i] = str(new_label) self.inval() def set_margins(self, l=0, t=0, r=0, b=0): - self._margins = [l,t,r,b] + self._margins = [l, t, r, b] def _extend_labels_array(self, i): if self._fd is None: - self.set_font('Sans') + self.set_font('Sans') if self._color is None: - self._color = self._sprites.cm.alloc_color('black') - while len(self.labels) < i+1: - self.labels.append(" ") - self._scale.append(self._scale[0]) - self._rescale.append(self._rescale[0]) - self._horiz_align.append(self._horiz_align[0]) - self._vert_align.append(self._vert_align[0]) + self._color = self._sprites.cm.alloc_color('black') + while len(self.labels) < i + 1: + self.labels.append(" ") + self._scale.append(self._scale[0]) + self._rescale.append(self._rescale[0]) + self._horiz_align.append(self._horiz_align[0]) + self._vert_align.append(self._vert_align[0]) def set_font(self, font): self._fd = pango.FontDescription(font) @@ -257,19 +256,19 @@ class Sprite: self._sprites.remove_from_list(self) def inval(self): - self._sprites.area.invalidate_rect( - gtk.gdk.Rectangle(self._x,self._y,self._width,self._height), False) + self._sprites.area.invalidate_rect(gtk.gdk.Rectangle(self._x, self._y, + self._width, self._height), False) def draw(self): - for i,img in enumerate(self.images): + for i, img in enumerate(self.images): if isinstance(img, gtk.gdk.Pixbuf): self._sprites.area.draw_pixbuf( - self._sprites.gc, img, 0, 0, self._x+self._dx[i], - self._y+self._dy[i]) + self._sprites.gc, img, 0, 0, self._x + self._dx[i], + self._y + self._dy[i]) elif img is not None: self._sprites.area.draw_drawable( - self._sprites.gc, img, 0, 0, self._x+self._dx[i], - self._y+self._dy[i], -1, -1) + self._sprites.gc, img, 0, 0, self._x + self._dx[i], + self._y + self._dy[i], -1, -1) if len(self.labels) > 0: self.draw_label() @@ -277,52 +276,52 @@ class Sprite: x, y = pos if x < self._x: return False - if x > self._x+self._width: + if x > self._x + self._width: return False if y < self._y: return False - if y > self._y+self._height: + if y > self._y + self._height: return False return True def draw_label(self): - my_width = self._width-self._margins[0]-self._margins[2] + my_width = self._width - self._margins[0] - self._margins[2] if my_width < 0: my_width = 0 - my_height = self._height-self._margins[1]-self._margins[3] + my_height = self._height - self._margins[1] - self._margins[3] for i in range(len(self.labels)): pl = self._sprites.canvas.create_pango_layout(str(self.labels[i])) - self._fd.set_size(int(self._scale[i]*pango.SCALE)) + self._fd.set_size(int(self._scale[i] * pango.SCALE)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE if w > my_width: - if self._rescale[i] is True: + if self._rescale[i]: self._fd.set_size( - int(self._scale[i]*pango.SCALE*my_width/w)) + int(self._scale[i] * pango.SCALE * my_width / w)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE else: - j = len(self.labels[i])-1 + j = len(self.labels[i]) - 1 while(w > my_width and j > 0): pl = self._sprites.canvas.create_pango_layout( - "…"+self.labels[i][len(self.labels[i])-j:]) - self._fd.set_size(int(self._scale[i]*pango.SCALE)) + "…" + self.labels[i][len(self.labels[i]) - j:]) + self._fd.set_size(int(self._scale[i] * pango.SCALE)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE j -= 1 if self._horiz_align[i] == "center": - x = int(self._x+self._margins[0]+(my_width-w)/2) + x = int(self._x + self._margins[0] + (my_width - w) / 2) elif self._horiz_align[i] == 'left': - x = int(self._x+self._margins[0]) + x = int(self._x + self._margins[0]) else: # right - x = int(self._x+self._width-w-self._margins[2]) - h = pl.get_size()[1]/pango.SCALE + x = int(self._x + self._width - w - self._margins[2]) + h = pl.get_size()[1] / pango.SCALE if self._vert_align[i] == "middle": - y = int(self._y+self._margins[1]+(my_height-h)/2) + y = int(self._y + self._margins[1] + (my_height - h) / 2) elif self._vert_align[i] == "top": - y = int(self._y+self._margins[1]) + y = int(self._y + self._margins[1]) else: # bottom - y = int(self._y+self._height-h-self._margins[3]) + y = int(self._y + self._height - h - self._margins[3]) self._sprites.gc.set_foreground(self._color) self._sprites.area.draw_layout(self._sprites.gc, x, y, pl) @@ -330,37 +329,37 @@ class Sprite: max = 0 for i in range(len(self.labels)): pl = self._sprites.canvas.create_pango_layout(self.labels[i]) - self._fd.set_size(int(self._scale[i]*pango.SCALE)) + self._fd.set_size(int(self._scale[i] * pango.SCALE)) pl.set_font_description(self._fd) - w = pl.get_size()[0]/pango.SCALE + w = pl.get_size()[0] / pango.SCALE if w > max: max = w return max def label_safe_width(self): - return self._width-self._margins[0]-self._margins[2] - + return self._width - self._margins[0] - self._margins[2] + def label_safe_height(self): - return self._height-self._margins[1]-self._margins[3] - + return self._height - self._margins[1] - self._margins[3] + def label_left_top(self): - return (self._margins[0], self._margins[1]) + return(self._margins[0], self._margins[1]) def get_pixel(self, pos, i=0): x, y = pos - x = x-self._x - y = y-self._y - if y > self.images[i].get_height()-1: - return (-1,-1,-1,-1) + x = x - self._x + y = y - self._y + if y > self.images[i].get_height() - 1: + return(-1, -1, -1, -1) try: array = self.images[i].get_pixels() if array is not None: - offset = (y*self.images[i].get_width()+x)*4 - r,g,b,a = ord(array[offset]), ord(array[offset+1]),\ - ord(array[offset+2]), ord(array[offset+3]) - return (r,g,b,a) + offset = (y * self.images[i].get_width() + x) * 4 + r, g, b, a = ord(array[offset]), ord(array[offset + 1]),\ + ord(array[offset + 2]), ord(array[offset + 3]) + return(r, g, b, a) else: - return (-1,-1,-1,-1) + return(-1, -1, -1, -1) except IndexError: print "Index Error: %d %d" % (len(array), offset) - return (-1,-1,-1,-1) + return(-1, -1, -1, -1) |