Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter@sugarlabs.org>2010-07-07 04:10:34 (GMT)
committer Walter Bender <walter@sugarlabs.org>2010-07-07 04:10:34 (GMT)
commit9fea6204fa6d53d7b8a6c892ce45f0cefb9a3429 (patch)
tree3f143537614c012b9b57d95c921c0f6ada35b0c9
parentcb3226fdd296808f66ce4cf6a4247b1f8764b950 (diff)
pep8
-rw-r--r--grid.py87
1 files changed, 43 insertions, 44 deletions
diff --git a/grid.py b/grid.py
index ee93683..32d812d 100644
--- a/grid.py
+++ b/grid.py
@@ -13,57 +13,53 @@
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, COL
-from constants import *
-#
-# Class for managing ROWxCOL matrix of cards
-#
class Grid:
+ """ Class for managing ROWxCOL matrix of cards """
+
def __init__(self, width, height, card_width, card_height):
- # The playing surface
+ """ Initialize the playing surface """
self.grid = []
- for i in range(ROW*COL):
+ for i in range(ROW * COL):
self.grid.append(None)
# Card spacing
- self.left = int((width-(card_width*2))/2)
- self.xinc = int(card_width*1.2)
+ self.left = int((width - (card_width * 2)) / 2)
+ self.xinc = int(card_width * 1.2)
self.top = 10
- self.yinc = int(card_height*1.33)
+ self.yinc = int(card_height * 1.33)
- # Deal an initial set of cards.
def deal(self, deck):
- for i in range(ROW*COL):
- if i < (ROW-1)*COL:
+ """ Deal an initial set of cards. """
+ for i in range(ROW * COL):
+ if i < (ROW - 1) * COL:
self.grid[i] = deck.deal_next_card()
self.place_a_card(self.grid[i], self.grid_to_xy(i)[0],
self.grid_to_xy(i)[1])
else: # Leave a blank row for extra cards at the bottom.
self.grid[i] = None
- # Add cards to the bottom row when there is no match.
def deal_extra_cards(self, deck):
- # But only if there are still cards in the deck
- # and only 12 cards in the grid
+ """ 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 deck.empty() is False and self.cards_in_grid() == DEAL:
- for c in range(0,COL):
+ for c in range(0, COL):
i = self.grid.index(None)
self.grid[i] = deck.deal_next_card()
self.place_a_card(self.grid[i], self.grid_to_xy(i)[0],
self.grid_to_xy(i)[1])
- # How many cards are on the grid?
def cards_in_grid(self):
- return ROW*COL-self.grid.count(None)
+ """ How many cards are on the grid? """
+ return ROW * COL - self.grid.count(None)
- # Restore cards to grid upon resume or share.
def restore(self, deck, saved_card_index):
+ """ Restore cards to grid upon resume or share. """
self.hide()
j = 0
for i in saved_card_index:
@@ -74,8 +70,8 @@ class Grid:
j += 1
self.show()
- # Remove a match from the grid and replace it with new cards from the deck.
def remove_and_replace(self, clicked_set, deck):
+ """ 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 +91,16 @@ class Grid:
# Mark as empty the grid positions we are not refilling
self.grid[i] = None
- # Move card to the match area.
def display_match(self, spr, i):
- spr.move((MATCH_POSITION, self.top + i*self.yinc))
+ """ Move card to the match area. """
+ spr.move((MATCH_POSITION, self.top + i * self.yinc))
spr.set_layer(2000)
- # If we have removed cards from an expanded grid, we have to consolidate.
def consolidate(self):
- for j in range((ROW-1)*COL,ROW*COL):
+ """ 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):
if self.grid[i] is None:
@@ -111,42 +109,43 @@ class Grid:
self.grid[i].spr.set_layer(2000)
self.grid[j] = None
else:
- i+=1
+ i += 1
- # Place a card at position x,y and display it.
def place_a_card(self, c, x, y):
+ """ Place a card at position x,y and display it. """
if c is not None:
- c.spr.move((x,y))
+ c.spr.move((x, 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 sprite x,y to grid index. """
+ 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))
+ """ Convert from grid index to sprite x,y. """
+ return ((self.left + i % COL * self.xinc),
+ (self.top + (i / COL) * self.yinc))
- # Return the sprite in grid-position i.
def grid_to_spr(self, i):
+ """ Return the sprite in grid-position i. """
return self.grid[i].spr
- # Return the index of a sprite in grid.
def spr_to_grid(self, spr):
- for i in range(ROW*COL):
+ """ 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
- # Hide all of the cards on the grid.
def hide(self):
- for i in range(ROW*COL):
+ """ 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()
- # Restore all card on the grid to their x,y positions.
def show(self):
- for i in range(ROW*COL):
- self.place_a_card(self.grid[i],self.grid_to_xy(i)[0],
+ """ 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])
-