Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter@walter-laptop.(none)>2009-12-22 01:11:27 (GMT)
committer Walter Bender <walter@walter-laptop.(none)>2009-12-22 01:11:27 (GMT)
commit3ff461ab767d026fe27520c23aa856ea7275c399 (patch)
tree987bf8a2b536682d00fc820e2fd321cc39e83c92
parentbf28eb1b239ebb2d0102e3de07b86d62dd94bcc6 (diff)
better abstraction barriers
-rw-r--r--card.py33
-rw-r--r--deck.py51
-rw-r--r--grid.py32
-rw-r--r--window.py58
4 files changed, 92 insertions, 82 deletions
diff --git a/card.py b/card.py
index d5d0b13..8e7d94f 100644
--- a/card.py
+++ b/card.py
@@ -41,37 +41,36 @@ from sprites import *
# if shape == SELECTMASK then generate special card-selected overlay
#
class Card:
- def __init__(self,vmw,shape,color,num,fill):
+ def __init__(self, vmw, path, cardtype, width, height, attributes):
# what do we need to know about each card?
- if shape == SELECTMASK:
+ if attributes[0] == SELECTMASK:
self.spr = sprNew(vmw,0,0,self.load_image(
- vmw.path,
+ path,
"selected",
- vmw.card_w*vmw.scale,
- vmw.card_h*vmw.scale))
+ width,
+ height))
self.index = SELECTMASK
- elif shape == MATCHMASK:
+ elif attributes[0] == MATCHMASK:
self.spr = sprNew(vmw,0,0,self.load_image(
- vmw.path,
+ path,
"match",
- vmw.card_w*vmw.scale,
- vmw.card_h*vmw.scale))
+ width,
+ height))
self.index = MATCHMASK
else:
- self.shape = shape
- self.color = color
- self.num = num
- self.fill = fill
+ self.shape = attributes[0]
+ self.color = attributes[1]
+ self.num = attributes[2]
+ self.fill = attributes[3]
self.index = self.shape*COLORS*NUMBER*FILLS+\
self.color*NUMBER*FILLS+\
self.num*FILLS+\
self.fill
# create sprite from svg file
self.spr = sprNew(vmw,0,0,self.load_image(
- vmw.path,
- vmw.cardtype+"-"+str(self.index),
- vmw.card_w*vmw.scale,
- vmw.card_h*vmw.scale))
+ path,
+ cardtype+"-"+str(self.index),
+ width, height))
self.spr.label = ""
def show_card(self):
diff --git a/deck.py b/deck.py
index b643c4f..402df3a 100644
--- a/deck.py
+++ b/deck.py
@@ -32,28 +32,30 @@ from card import *
# class for defining deck of cards
#
class Deck:
- def __init__(self, vmw):
+ def __init__(self, vmw, path, card_type, width, height):
# create the deck of cards
- self.deck = {}
+ self.cards = {}
# remember the position in the deck
self.index = 0
# how many cards are in the deck?
self.count = 0
# Initialize the deck of cards by looping through all the patterns
- for shape in range(0,SHAPES):
- for color in range(0,COLORS):
- for num in range(0,NUMBER):
- for fill in range(0,FILLS):
- self.deck[self.count] = Card(vmw,shape,color,num,fill)
+ for shape in range(0, SHAPES):
+ for color in range(0, COLORS):
+ for num in range(0, NUMBER):
+ for fill in range(0, FILLS):
+ self.cards[self.count] = Card(vmw, path, card_type,
+ width, height,
+ [shape,color,num,fill])
self.count += 1
# shuffle the deck
def shuffle(self):
# hide all the cards
- for c in self.deck:
- self.deck[c].hide_card()
+ for c in self.cards:
+ self.cards[c].hide_card()
# randomize the deck
- for n in range(0,DECKSIZE*4):
+ for n in range(0, DECKSIZE*4):
i = random.randrange(DECKSIZE)
j = random.randrange(DECKSIZE)
self.swap_cards(i,j)
@@ -63,35 +65,42 @@ class Deck:
# swap the position of two cards in the deck
def swap_cards(self,i,j):
- tmp = self.deck[j]
- self.deck[j] = self.deck[i]
- self.deck[i] = tmp
+ tmp = self.cards[j]
+ self.cards[j] = self.cards[i]
+ self.cards[i] = tmp
return
# given a sprite, 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]
+ for c in self.cards:
+ if self.cards[c].spr == spr:
+ return self.cards[c]
return None
# deal the next card from the deck
def deal_next_card(self):
if self.empty():
return None
- c = self.deck[self.index]
+ next_card = self.cards[self.index]
self.index += 1
- return c
+ return next_card
# is the deck empty?
def empty(self):
- if self.index < self.count:
+ if self.cards_remaining() > 0:
return False
else:
return True
+ # cards remaining in the deck
+ def cards_remaining(self):
+ return(self.count-self.index)
+
# hide the deck
def hide(self):
- for c in self.deck:
- self.deck[c].hide_card()
+ for c in self.cards:
+ self.cards[c].hide_card()
+
+
+
diff --git a/grid.py b/grid.py
index fb05d63..bd0f665 100644
--- a/grid.py
+++ b/grid.py
@@ -37,28 +37,28 @@ COL = 3
# class for managing 3x5 matrix of cards
#
class Grid:
- def __init__(self, vmw):
+ def __init__(self, width, height, card_width, card_height, scale):
# the playing surface is a 3x5 grid
self.grid = []
# how many cards are on the playing field
self.cards = 0
# card spacing
- self.left = int((vmw.width-(vmw.card_w*2*vmw.scale))/2)
- self.xinc = int(vmw.card_w*1.2*vmw.scale)
- # self.top = int((vmw.height-(vmw.card_h*3.5*vmw.scale))/2)
+ self.left = int((width-(card_width*2*scale))/2)
+ self.xinc = int(card_width*1.2*scale)
+ # self.top = int((height-(card_height*3.5*card_scale))/2)
self.top = 10
- self.yinc = int(vmw.card_h*1.33*vmw.scale)
+ self.yinc = int(card_height*1.33*scale)
# deal the initial deck of cards
- def deal(self, vmw):
+ def deal(self, deck):
# find upper left corner of grid
self.cards = 0
self.grid = []
x = self.left
y = self.top
- for r in range(0,ROW-1):
- for c in range(0,COL):
- a = vmw.deck.deal_next_card()
+ for r in range(0, ROW-1):
+ for c in range(0, COL):
+ a = deck.deal_next_card()
self.grid.append(a)
self.place_a_card(a,x,y)
x += self.xinc
@@ -70,30 +70,30 @@ class Grid:
self.grid.append(None)
# add cards when there is no match
- def deal_extra_cards(self, vmw):
+ def deal_extra_cards(self, deck):
# if there are still cards in the deck and only 12 cards in the grid
- if vmw.deck.empty() is False and self.cards == DEAL:
+ if deck.empty() is False and self.cards == DEAL:
# add three extra cards to the playing field
for c in range(0,COL):
i = self.grid.index(None)
- self.grid[i] = vmw.deck.deal_next_card()
+ self.grid[i] = deck.deal_next_card()
x = self.left+self.xinc*(i%COL)
y = self.top+self.yinc*int(i/COL)
self.place_a_card(self.grid[i],x,y)
self.cards += 1
# remove a match from the grid and deal new cards from the deck
- def remove_and_replace(self, clicked_set, vmw):
+ def remove_and_replace(self, clicked_set, deck):
for a in clicked_set:
# find the position in the grid of the clicked card
i = self.xy_to_grid(a.x,a.y)
# only add new cards if we are down to 12 cards
if self.cards == DEAL:
- if vmw.deck.empty():
+ if deck.empty():
self.grid[i] = None
else:
# save card in grid position of card we are replacing
- self.grid[i] = vmw.deck.deal_next_card()
+ self.grid[i] = deck.deal_next_card()
self.place_a_card(self.grid[i],a.x,a.y)
else:
self.cards -= 1
@@ -102,7 +102,7 @@ class Grid:
# move clicked card to the match area
a.x = 10
a.y = self.top + clicked_set.index(a)*self.yinc
- vmw.deck.spr_to_card(a).show_card()
+ deck.spr_to_card(a).show_card()
# place a card at position x,y and display it
def place_a_card(self,c,x,y):
diff --git a/window.py b/window.py
index 9372280..40335dc 100644
--- a/window.py
+++ b/window.py
@@ -70,34 +70,35 @@ def new_window(canvas, path, cardtype, parent=None):
vmw.canvas.connect("key_press_event", _keypress_cb, vmw)
vmw.width = gtk.gdk.screen_width()
vmw.height = gtk.gdk.screen_height()-GRID_CELL_SIZE
- vmw.card_w = CARD_W
- vmw.card_h = CARD_H
+ vmw.card_width = CARD_W
+ vmw.card_height = CARD_H
vmw.cardtype = cardtype
- vmw.scale = 0.8 * vmw.height/(vmw.card_h*5.5)
+ vmw.scale = 0.8 * vmw.height/(vmw.card_height*5.5)
vmw.area = vmw.canvas.window
vmw.gc = vmw.area.new_gc()
vmw.cm = vmw.gc.get_colormap()
vmw.msgcolor = vmw.cm.alloc_color('black')
vmw.sprites = []
vmw.selected = []
+ vmw.match_display_area = []
- # a place to put the matched cards
- vmw.match_field = [Card(vmw,MATCHMASK,0,0,0),\
- Card(vmw,MATCHMASK,0,0,0),\
- Card(vmw,MATCHMASK,0,0,0)]
-
- # create a deck of cards, shuffle, and then deal
- vmw.deck = Deck(vmw)
- vmw.grid = Grid(vmw)
+ # create a deck of cards and a grid for the playing field
+ vmw.deck = Deck(vmw, vmw.path, vmw.cardtype, vmw.card_width*vmw.scale,
+ vmw.card_height*vmw.scale)
+ vmw.grid = Grid(vmw.width,vmw.height,vmw.card_width,vmw.card_height,
+ vmw.scale)
+ # initialize three card-selected overlays and
+ # a place to put the matched cards
for i in range(0,3):
- vmw.match_field[i].spr.x = 10
- vmw.match_field[i].spr.y = vmw.grid.top+i*vmw.grid.yinc
- vmw.match_field[i].show_card()
-
- # initialize three card-selected overlays
- for i in range(0,3):
- vmw.selected.append(Card(vmw,SELECTMASK,0,0,0))
+ vmw.selected.append(Card(vmw,vmw.path,"",vmw.card_width*vmw.scale,
+ vmw.card_height*vmw.scale,[SELECTMASK,0,0,0]))
+ vmw.match_display_area.append(Card(vmw,vmw.path,"",
+ vmw.card_width*vmw.scale,
+ vmw.card_height*vmw.scale,[MATCHMASK,0,0,0]))
+ vmw.match_display_area[i].spr.x = 10
+ vmw.match_display_area[i].spr.y = vmw.grid.top+i*vmw.grid.yinc
+ vmw.match_display_area[i].show_card()
# make an array of three cards that are clicked
vmw.clicked = [None, None, None]
@@ -115,15 +116,16 @@ def new_game(vmw,cardtype):
vmw.deck.hide()
if vmw.cardtype is not cardtype:
vmw.cardtype = cardtype
- vmw.deck = Deck(vmw)
+ vmw.deck = Deck(vmw, vmw.path, vmw.cardtype, vmw.card_width*vmw.scale,
+ vmw.card_height*vmw.scale)
vmw.deck.shuffle()
- vmw.grid.deal(vmw)
+ vmw.grid.deal(vmw.deck)
if find_a_match(vmw) is False:
- vmw.grid.deal_extra_cards(vmw)
+ vmw.grid.deal_extra_cards(vmw.deck)
vmw.matches = 0
vmw.total_time = 0
set_label(vmw, "deck", "%d %s" %
- (vmw.deck.count-vmw.deck.index, _("cards remaining")))
+ (vmw.deck.cards_remaining(), _("cards remaining")))
set_label(vmw,"match","%d %s" % (vmw.matches,_("matches")))
vmw.start_time = gobject.get_current_time()
vmw.timeout_id = None
@@ -183,9 +185,9 @@ def _button_release_cb(win, event, vmw):
gobject.source_remove(vmw.timeout_id)
vmw.total_time += gobject.get_current_time()-vmw.start_time
# out with the old and in with the new
- vmw.grid.remove_and_replace(vmw.clicked, vmw)
+ vmw.grid.remove_and_replace(vmw.clicked, vmw.deck)
set_label(vmw, "deck", "%d %s" %
- (vmw.deck.count-vmw.deck.index, _("cards remaining")))
+ (vmw.deck.cards_remaining(), _("cards remaining")))
# test to see if the game is over
if vmw.deck.empty():
if find_a_match(vmw) is False:
@@ -208,7 +210,7 @@ def _button_release_cb(win, event, vmw):
return True
# test to see if we need to deal extra cards
if find_a_match(vmw) is False:
- vmw.grid.deal_extra_cards(vmw)
+ vmw.grid.deal_extra_cards(vmw.deck)
else:
# set_label(vmw,"status",vmw.msg)
set_label(vmw,"status",_("match"))
@@ -306,7 +308,9 @@ def match_check(cardarray, cardtype):
return False
if (cardarray[0].color + cardarray[1].color + cardarray[2].color)%3 != 0:
return False
- # special case for the word game
+ if (cardarray[0].shape + cardarray[1].shape + cardarray[2].shape)%3 != 0:
+ return False
+ # special case for the word game:
# only check fill when numbers are the same
if cardtype == 'word':
if cardarray[0].num == cardarray[1].num and \
@@ -316,8 +320,6 @@ def match_check(cardarray, cardtype):
else:
if (cardarray[0].fill + cardarray[1].fill + cardarray[2].fill)%3 != 0:
return False
- if (cardarray[0].shape + cardarray[1].shape + cardarray[2].shape)%3 != 0:
- return False
return True
#