Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/grid.py
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2011-10-28 17:14:38 (GMT)
committer Walter Bender <walter.bender@gmail.com>2011-10-28 17:14:38 (GMT)
commit22051a4756dd106a464f4cdc17f3cc78950a3423 (patch)
tree360936bb7eacd0f5c316f964cda21c4617431547 /grid.py
parentd4fa536e32059964306778f618cafd860b4a3ca0 (diff)
animate moving match to discard stack
Diffstat (limited to 'grid.py')
-rw-r--r--grid.py68
1 files changed, 44 insertions, 24 deletions
diff --git a/grid.py b/grid.py
index 79acd9e..dcc448e 100644
--- a/grid.py
+++ b/grid.py
@@ -4,15 +4,17 @@
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the
-# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-# Boston, MA 02111-1307, USA.
+
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
+
import pygtk
pygtk.require('2.0')
import gtk
+import gobject
+
import random
from constants import ROW, COL, MATCH_POSITION, DEAL
@@ -21,11 +23,17 @@ import logging
_logger = logging.getLogger('visualmatch-activity')
+def _distance_squared(pos1, pos2):
+ ''' simple distance function '''
+ return (pos1[0] - pos2[0]) * (pos1[0] - pos2[0]) + \
+ (pos1[1] - pos2[1]) * (pos1[1] - pos2[1])
+
+
class Grid:
- """ Class for managing ROWxCOL matrix of cards """
+ ''' Class for managing ROWxCOL matrix of cards '''
def __init__(self, width, height, card_width, card_height):
- """ Initialize the playing surface """
+ ''' Initialize the playing surface '''
self.grid = []
for i in range(ROW * COL):
self.grid.append(None)
@@ -34,9 +42,11 @@ class Grid:
self.xinc = int(card_width * 1.2)
self.top = 10
self.yinc = int(card_height * 1.33)
+ self.dx = [0, 0, 0]
+ self.dy = [0, 0, 0]
def deal(self, deck):
- """ Deal an initial set of cards. """
+ ''' Deal an initial set of cards. '''
for i in range(ROW * COL):
self.grid[i] = None
if i < (ROW - 1) * COL:
@@ -46,10 +56,10 @@ class Grid:
self.grid_to_xy(i)[1])
def deal_extra_cards(self, deck):
- """ Add cards to the bottom row when there is no match.
+ ''' Add cards to the bottom row when there is no match.
But only if there are still cards in the deck
and only 12 cards in the grid
- """
+ '''
if not deck.empty() and self.cards_in_grid() == DEAL:
for c in range(0, COL):
i = self.grid.index(None)
@@ -58,11 +68,11 @@ class Grid:
self.grid_to_xy(i)[1])
def cards_in_grid(self):
- """ How many cards are on the grid? """
+ ''' How many cards are on the grid? '''
return ROW * COL - self.grid.count(None)
def restore(self, deck, saved_card_index):
- """ Restore cards to grid upon resume or share. """
+ ''' Restore cards to grid upon resume or share. '''
self.hide()
j = 0
for i in saved_card_index:
@@ -74,7 +84,7 @@ class Grid:
self.show()
def remove_and_replace(self, clicked_set, deck):
- """ Remove a match from the grid and replace with new cards. """
+ ''' Remove a match from the grid and replace with new cards. '''
for a in clicked_set:
# Move the match to the match display area
self.display_match(a, clicked_set.index(a))
@@ -95,14 +105,24 @@ class Grid:
self.grid[i] = None
def display_match(self, spr, i):
- """ Move card to the match area. """
- spr.move((MATCH_POSITION, self.top + i * self.yinc))
+ ''' Move card to the match area. '''
spr.set_layer(2000)
+ self.dx[i] = int((MATCH_POSITION - spr.get_xy()[0]) / 10)
+ self.dy[i] = int(((self.top + i * self.yinc) - spr.get_xy()[1]) / 10)
+ timeout_id = gobject.timeout_add(100, self._move, spr, i)
+
+ def _move(self, spr, i):
+ spr.move_relative((self.dx[i], self.dy[i]))
+ if _distance_squared(spr.get_xy(),
+ (MATCH_POSITION, self.top + i * self.yinc)) < 100:
+ spr.move((MATCH_POSITION, self.top + i * self.yinc))
+ else:
+ timeout_id = gobject.timeout_add(100, self._move, spr, i)
def consolidate(self):
- """ If we have removed cards from an expanded grid,
+ ''' If we have removed cards from an expanded grid,
we have to consolidate.
- """
+ '''
for j in range((ROW - 1) * COL, ROW * COL):
i = 0
while(self.grid[j] is not None):
@@ -115,40 +135,40 @@ class Grid:
i += 1
def place_a_card(self, c, x, y):
- """ Place a card at position x,y and display it. """
+ ''' Place a card at position x,y and display it. '''
if c is not None:
c.spr.move((x, y))
c.show_card()
def xy_to_grid(self, x, y):
- """ Convert from sprite x,y to grid index. """
+ ''' Convert from sprite x,y to grid index. '''
return int(COL * (y - self.top) / self.yinc)\
+ int((x - self.left) / self.xinc)
def grid_to_xy(self, i):
- """ Convert from grid index to sprite x,y. """
+ ''' Convert from grid index to sprite x,y. '''
return ((self.left + i % COL * self.xinc),
(self.top + (i / COL) * self.yinc))
def grid_to_spr(self, i):
- """ Return the sprite in grid-position i. """
+ ''' Return the sprite in grid-position i. '''
return self.grid[i].spr
def spr_to_grid(self, spr):
- """ Return the index of a sprite in grid. """
+ ''' Return the index of a sprite in grid. '''
for i in range(ROW * COL):
if self.grid[i] is not None and self.grid[i].spr == spr:
return(i)
return None
def hide(self):
- """ Hide all of the cards on the grid. """
+ ''' Hide all of the cards on the grid. '''
for i in range(ROW * COL):
if self.grid[i] is not None:
self.grid[i].hide_card()
def show(self):
- """ Restore all card on the grid to their x,y positions. """
+ ''' Restore all card on the grid to their x,y positions. '''
for i in range(ROW * COL):
self.place_a_card(self.grid[i], self.grid_to_xy(i)[0],
self.grid_to_xy(i)[1])