diff options
author | Walter Bender <walter@walter-laptop.(none)> | 2009-11-26 17:39:45 (GMT) |
---|---|---|
committer | Walter Bender <walter@walter-laptop.(none)> | 2009-11-26 17:39:45 (GMT) |
commit | 56ef70d8eaf8b36d703326aeefc6525d3285f23c (patch) | |
tree | 0c192f427dc05b0efafd13f71047704e0bb0f173 | |
parent | ad99d2921a78047c7690c2b86a5196d073589fc2 (diff) |
added graphical feedback for card selection
-rw-r--r-- | card.py | 49 | ||||
-rw-r--r-- | grid.py | 22 | ||||
-rw-r--r-- | images/card-selected.svg | 20 | ||||
-rw-r--r-- | window.py | 54 |
4 files changed, 103 insertions, 42 deletions
@@ -38,29 +38,38 @@ from sprites import * for num in range(0,3): for fill in range(0,3): """ +# if shape == -1 then generate special card-selected overlay # class Card: - def __init__(self,tw,shape,color,num,fill): - # what do we need to know about each card? - self.shape = shape - self.color = color - self.num = num - self.fill = fill - self.index = self.shape*4*3*3+self.color*3*3+self.num*3+self.fill+1 - # create sprite from svg file - self.spr = sprNew(tw, 0, 0,\ - self.load_image(tw.path+str(self.index),tw.card_w*tw.scale, - tw.card_h*tw.scale)) - self.spr.label = "" + def __init__(self,tw,shape,color,num,fill): + # what do we need to know about each card? + if shape == -1: + self.spr = sprNew(tw, 0, 0, self.load_image(tw.path+"selected", + tw.card_w*tw.scale, + tw.card_h*tw.scale)) + self.index = 0 + else: + self.shape = shape + self.color = color + self.num = num + self.fill = fill + self.index = self.shape*4*3*3+self.color*3*3+self.num*3+self.fill+1 + # create sprite from svg file + self.spr = sprNew(tw, 0, 0, self.load_image(tw.path+\ + str(self.index), + tw.card_w*tw.scale, + tw.card_h*tw.scale)) + self.spr.label = "" + def draw_card(self): + setlayer(self.spr,2000) + draw(self.spr) + def hide_card(self): + hide(self.spr) - def draw_card(self): - setlayer(self.spr,2000) - draw(self.spr) - - def load_image(self, file, w, h): - return gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(file + \ - '.svg'), \ - int(w), int(h)) + def load_image(self, file, w, h): + return gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(file+".svg"), + int(w), + int(h)) @@ -50,16 +50,14 @@ class Grid: self.deck[self.count] = Card(tw,shape,color,num,fill) self.count += 1 - # shuffle the deck - self.shuffle() - + def deal(self, tw): # layout the initial 12 cards from the deck # find upper left corner of grid x = int((tw.width-(tw.card_w*5.5*tw.scale))/2) y = int((tw.height-(tw.card_h*3*tw.scale))/2) for r in range(0,3): for c in range(0,4): - print "dealing card " + str(self.index) + # print "dealing card " + str(self.index) self.deck[self.index].spr.x = x self.deck[self.index].spr.y = y self.deck[self.index].draw_card() @@ -82,16 +80,12 @@ class Grid: self.deck[i] = tmp return - # initial layout of 12 cards on the table - def start(self, tw): - return + # given a spr, find the corresponding card in the deck + def spr_to_card(self, spr): + for c in self.deck: + if self.deck[c].spr == spr: + return self.deck[c] + return None - # draw a card from the deck - def draw_a_card(self, tw): - return - - # find a set - def find_a_set(self, tw): - return diff --git a/images/card-selected.svg b/images/card-selected.svg new file mode 100644 index 0000000..4d231ab --- /dev/null +++ b/images/card-selected.svg @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Emacs --> + +<svg + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + version="1.0" + width="75" + height="125" + id="svg2"> + <rect + width="71.5" + height="121.5" + rx="10.5" + ry="8.75" + x="1.5" + y="1.5" + id="rect4" + style="fill:none;stroke:#000000;stroke-width:3.5;" /> +</svg> @@ -77,10 +77,19 @@ def new_window(canvas, path, parent=None): tw.cm = tw.gc.get_colormap() tw.msgcolor = tw.cm.alloc_color('black') tw.sprites = [] + tw.selected = [] - # make the cards, the deck and start playing... + # create a deck of cards, shuffle, and then deal tw.deck = Grid(tw) - tw.deck.start(tw) + tw.deck.shuffle() + tw.deck.deal(tw) + + # initialize three card-selected overlays + for i in range(0,3): + tw.selected.append(Card(tw,-1,0,0,0)) + + # make an array of three cards that are clicked + tw.clicked = [None, None, None] # Start doing something tw.keypress = "" @@ -88,9 +97,6 @@ def new_window(canvas, path, parent=None): tw.release = -1 tw.start_drag = [0,0] - # make an array of three cards that are clicked - tw.clicked = [None, None, None] - return tw # @@ -122,14 +128,45 @@ def _button_release_cb(win, event, tw): return True # take note of card under button release tw.release = spr + + # check to make sure that the current card isn't already selected + for a in tw.clicked: + if a is spr: + return True + + # add the selected card to the list for a in tw.clicked: - if (a is None) and (tw.clicked.index(a) <= 2): - tw.clicked[tw.clicked.index(a)]= spr + if a is None: + if tw.deck.spr_to_card(spr).index is not None: + print "selecting card " + str(tw.deck.spr_to_card(spr).index) + else: + print "selected card not found" + i = tw.clicked.index(a) + print "filling slot " + str(i) + tw.clicked[i] = spr + print "using selection mask " + str(i) + tw.selected[i].spr.x = spr.x + tw.selected[i].spr.y = spr.y + tw.selected[i].draw_card() + # we only want to add the card to the list once + break + # if we have three cards selected, test for a set #check to see if it's a set - if (set_check()): + try: + tw.clicked.index(None) + except ValueError: + if set_check([tw.deck.spr_to_card(tw.clicked[0]), + tw.deck.spr_to_card(tw.clicked[1]), + tw.deck.spr_to_card(tw.clicked[2])]): + print "found a set" + else: + print "not a set" + print "reseting board" for a in tw.clicked: a = None + for a in tw.selected: + a.hide_card() return True # @@ -158,6 +195,7 @@ def _destroy_cb(win, event, tw): # def set_check(cardarray): + print cardarray for a in cardarray: if a is None: return False |