#Copyright (c) 2009, Walter Bender #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal #in the Software without restriction, including without limitation the rights #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the Software is #furnished to do so, subject to the following conditions: #The above copyright notice and this permission notice shall be included in #all copies or substantial portions of the Software. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. import pygtk pygtk.require('2.0') import gtk import gobject import random from sprites import * from card import * from deck import * from constants import * ROW = 5 COL = 3 # # class for managing 3x5 matrix of cards # class Grid: def __init__(self, width, height, card_width, card_height): # 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((width-(card_width*2))/2) self.xinc = int(card_width*1.2) # self.top = int((height-(card_height*3.5))/2) self.top = 10 self.yinc = int(card_height*1.33) # deal the initial deck of cards 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 = deck.deal_next_card() self.grid.append(a) self.place_a_card(a, x, y) x += self.xinc self.cards += 1 x = self.left y += self.yinc for c in range(0,COL): # leave a blank row for extra cards self.grid.append(None) # add cards when there is no match def deal_extra_cards(self, deck): # if there are still cards in the deck and only 12 cards in the grid 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] = 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, 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 deck.empty(): self.grid[i] = None else: # save card in grid position of card we are replacing self.grid[i] = deck.deal_next_card() self.place_a_card(self.grid[i], a.x, a.y) else: self.cards -= 1 # mark grid positions of cards we are not replacing self.grid[i] = None # move clicked card to the match area a.x = 10 a.y = self.top + clicked_set.index(a)*self.yinc deck.spr_to_card(a).show_card() # if we have removed cards from a grid of 15, we may need to consolidate def consolidate(self): for j in range(12,15): i = 0 while(self.grid[j] is not None): if self.grid[i] is None: self.grid[i] = self.grid[j] self.grid[i].spr.move(self.grid_to_xy(i)) self.grid[j] = None else: i+=1 # place a card at position x,y and display it def place_a_card(self, c, x, y): if c is not None: c.spr.x = x c.spr.y = y c.show_card() # convert from sprite x,y to grid index def xy_to_grid(self, x, y): return int(COL*(y-self.top)/self.yinc) + int((x-self.left)/self.xinc) # convert from grid index to sprite x,y def grid_to_xy(self, i): return ((self.left+i%COL*self.xinc),(self.top+(i/COL)*self.yinc)) # return sprite in grid element i def grid_to_spr(self, i): print self.grid[i].spr return self.grid[i].spr