diff options
author | Walter Bender <walter.bender@gmail.com> | 2011-04-01 01:41:01 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2011-04-01 01:41:01 (GMT) |
commit | b99e9930e4f36e99d912c429fad7dbb80f851789 (patch) | |
tree | ffa73615e4a7e77400caec5c5f824278b4f2235d | |
parent | 5c28743aa1fd34e4989770248e8daf45f11c28d6 (diff) |
renamed card to tile
-rw-r--r-- | PathsActivity.py | 24 | ||||
-rw-r--r-- | constants.py | 6 | ||||
-rw-r--r-- | deck.py | 112 | ||||
-rw-r--r-- | game.py | 112 | ||||
-rw-r--r-- | grid.py | 50 | ||||
-rw-r--r-- | hand.py | 38 | ||||
-rw-r--r-- | tile.py | 107 |
7 files changed, 278 insertions, 171 deletions
diff --git a/PathsActivity.py b/PathsActivity.py index ccc6b8a..a8db230 100644 --- a/PathsActivity.py +++ b/PathsActivity.py @@ -43,7 +43,7 @@ from gettext import gettext as _ import locale import os.path -from game import Game, CARDS +from game import Game, TILES from hand import Hand from genpieces import generate_xo from utils import json_load, json_dump, svg_str_to_pixbuf @@ -181,7 +181,7 @@ class PathsActivity(activity.Activity): self.toolbar) self.robot_button = _button_factory( - 'robot-off', _('Play with the computer.'), self._robot_cb, + 'robot-off', _('Play with the robot.'), self._robot_cb, self.toolbar) self.player = _image_factory( @@ -276,21 +276,21 @@ class PathsActivity(activity.Activity): if 'hand-' + str(i) in self.metadata: if len(self._game.hands) < i + 1: # Add robot or shared hand? self._game.hands.append( - Hand(self._game.card_width, self._game.card_height, + Hand(self._game.tile_width, self._game.tile_height, remote=True)) self._game.hands[i].restore(self.metadata['hand-' + str(i)], self._game.deck) - self._game.deck.index = ROW * COL - self._game.grid.cards_in_grid() + self._game.deck.index = ROW * COL - self._game.grid.tiles_in_grid() for h in self._game.hands: - self._game.deck.index += (COL - h.cards_in_hand()) + self._game.deck.index += (COL - h.tiles_in_hand()) self._game.last_spr_moved = None if 'last' in self.metadata: j = int(self.metadata['last']) for k in range(ROW * COL): - if self._game.deck.cards[k].number == j: - self._game.last_spr_moved = self._game.deck.cards[k].spr + if self._game.deck.tiles[k].number == j: + self._game.last_spr_moved = self._game.deck.tiles[k].spr return # Collaboration-related methods @@ -443,9 +443,9 @@ state=%d' % (id, initiator, type, service, params, state)) def _sending_deck(self, payload): ''' Sharer sends the deck. ''' self._game.deck.restore(payload) - for card in self._game.deck.cards: - card.reset() - card.hide() + for tile in self._game.deck.tiles: + tile.reset() + tile.hide() def _sending_hand(self, payload): ''' Sharer sends a hand. ''' @@ -459,10 +459,10 @@ state=%d' % (id, initiator, type, service, params, state)) ''' When a piece is played, everyone should move it into position. ''' tile_number, orientation, grid_position = json_load(payload) for i in range(ROW * COL): # find the tile with this number - if self._game.deck.cards[i].number == tile_number: + if self._game.deck.tiles[i].number == tile_number: tile_to_move = i break - self._game.grid.add_card_to_grid(tile_to_move, orientation, + self._game.grid.add_tile_to_grid(tile_to_move, orientation, grid_position, self._game.deck) self._game.show_connected_tiles() diff --git a/constants.py b/constants.py index 42ddf50..be1b74b 100644 --- a/constants.py +++ b/constants.py @@ -21,9 +21,9 @@ COL = 8 HIDE = 0 BOARD = 1 GRID = 2 -CARDS = 3 +TILES = 3 TOP = 4 OVER_THE_TOP = 5 -CARD_WIDTH = 55 -CARD_HEIGHT = 55 +TILE_WIDTH = 55 +TILE_HEIGHT = 55 @@ -13,54 +13,54 @@ import gtk from random import randrange -from card import Card, board_card +from tile import Tile, board_card from genpieces import generate_tile_1_line, generate_tile_2_lines from utils import json_dump, json_load from constants import HIDE, BOARD, ROW, COL class Deck: - ''' Class for defining deck of cards. ''' + ''' Class for defining deck of tiles. ''' def __init__(self, sprites, scale=1.0, color='#000000'): - ''' Create the deck of cards. ''' - self.cards = [] + ''' Create the deck of tiles. ''' + self.tiles = [] i = 0 for a in range(16): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_1_line(-1, 0, 0, 0, scale), [generate_tile_1_line(-1, 0, 0, 0, scale, color)], number=i)) - self.cards[-1].set_paths([[0, 0, 0, 1]]) + self.tiles[-1].set_paths([[0, 0, 0, 1]]) i += 1 for a in range(4): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_1_line(-1, 0, 1, 0, scale), [generate_tile_1_line(-1, 0, 1, 0, scale, color)], number=i)) - self.cards[-1].set_paths([[0, 1, 0, 1]]) + self.tiles[-1].set_paths([[0, 1, 0, 1]]) i += 1 for a in range(12): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_2_lines(-1, 0, 1, 0, 0, 0, 0, 1, scale), [generate_tile_2_lines(-1, 0, 1, 0, 0, 0, 0, 1, scale, [color, color])], number=i)) - self.cards[-1].set_paths([[0, 1, 1, 1]]) + self.tiles[-1].set_paths([[0, 1, 1, 1]]) i += 1 for a in range(16): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_2_lines(-1, 0, 0, 0, 0, -1, 0, 0, scale), [generate_tile_2_lines(-1, 0, 0, 0, 0, -1, 0, 0, scale, [color, color])], number=i)) - self.cards[-1].set_paths([[1, 0, 0, 1]]) + self.tiles[-1].set_paths([[1, 0, 0, 1]]) i += 1 for a in range(4): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_2_lines(-1, 0, 1, 0, 0, -1, 0, 1, scale), [generate_tile_2_lines(-1, 0, 1, 0, 0, -1, 0, 1, scale, [color, color])], number=i)) - self.cards[-1].set_paths([[1, 1, 1, 1]]) + self.tiles[-1].set_paths([[1, 1, 1, 1]]) i += 1 for a in range(8): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_2_lines(0, -1, 1, 0, -1, 0, 0, 1, scale), [generate_tile_2_lines(0, -1, 1, 0, -1, 0, 0, 1, scale, [color, '#000000']), @@ -68,10 +68,10 @@ class Deck: ['#000000', color]), generate_tile_2_lines(0, -1, 1, 0, -1, 0, 0, 1, scale, [color, color])], number=i)) - self.cards[-1].set_paths([[1, 1, 0, 0], [0, 0, 1, 1]]) + self.tiles[-1].set_paths([[1, 1, 0, 0], [0, 0, 1, 1]]) i += 1 for a in range(4): - self.cards.append(Card(sprites, + self.tiles.append(Tile(sprites, generate_tile_2_lines(0, -1, 1, 0, -1, 0, 0, 0, scale), [generate_tile_2_lines(0, -1, 1, 0, -1, 0, 0, 0, scale, [color, '#000000']), @@ -79,7 +79,7 @@ class Deck: ['#000000', color]), generate_tile_2_lines(0, -1, 1, 0, -1, 0, 0, 0, scale, [color, color])], number=i)) - self.cards[-1].set_paths([[1, 1, 0, 0], [0, 0, 0, 1]]) + self.tiles[-1].set_paths([[1, 1, 0, 0], [0, 0, 0, 1]]) i += 1 # Remember the current position in the deck. @@ -92,13 +92,13 @@ class Deck: def shuffle(self): ''' Shuffle the deck (Knuth algorithm). ''' decksize = self.count() - # Hide all the cards and make sure they are back to orientation 0 - for card in self.cards: - card.reset() - # Randomize the card order. + # Hide all the tiles and make sure they are back to orientation 0 + for tile in self.tiles: + tile.reset() + # Randomize the tile order. for n in range(decksize): i = randrange(decksize - n) - self.swap_cards(n, decksize - 1 - i) + self.swap_tiles(n, decksize - 1 - i) # Reset the index to the beginning of the deck after a shuffle, self.index = 0 self.hide() @@ -120,7 +120,7 @@ class Deck: ''' Serialize the deck for passing to share and saving ''' order = [] for i in range(ROW * COL): - order.append(self.cards[i].number) + order.append(self.tiles[i].number) return json_dump(order) def restore(self, deck_as_text): @@ -128,60 +128,60 @@ class Deck: deck = [] order = json_load(deck_as_text) for i in order: - deck.append(self.cards[order[i]]) - self.cards = deck[:] + deck.append(self.tiles[order[i]]) + self.tiles = deck[:] def clear(self): - ''' Remove any highlight from the cards. ''' - for card in self.cards: - card.reset() - - def swap_cards(self, i, j): - ''' Swap the position of two cards in the deck. ''' - tmp = self.cards[j] - self.cards[j] = self.cards[i] - self.cards[i] = tmp + ''' Remove any highlight from the tiles. ''' + for tile in self.tiles: + tile.reset() + + def swap_tiles(self, i, j): + ''' Swap the position of two tiles in the deck. ''' + tmp = self.tiles[j] + self.tiles[j] = self.tiles[i] + self.tiles[i] = tmp return - def spr_to_card(self, spr): - ''' Given a sprite, find the corresponding card in the deck. ''' - for card in self.cards: - if card.spr == spr: - return card + def spr_to_tile(self, spr): + ''' Given a sprite, find the corresponding tile in the deck. ''' + for tile in self.tiles: + if tile.spr == spr: + return tile return None - def index_to_card(self, i): - ''' Given a card index, find the corresponding card in the deck. ''' - for card in self.cards: - if card.index == i: - return card + def index_to_tile(self, i): + ''' Given a tile index, find the corresponding tile in the deck. ''' + for tile in self.tiles: + if tile.index == i: + return tile return None - def deal_next_card(self): - ''' Return the next card from the deck. ''' + def deal_next_tile(self): + ''' Return the next tile from the deck. ''' if self.empty(): return None - next_card = self.cards[self.index] + next_tile = self.tiles[self.index] self.index += 1 - return next_card + return next_tile def empty(self): ''' Is the deck empty? ''' - if self.cards_remaining() > 0: + if self.tiles_remaining() > 0: return False else: return True - def cards_remaining(self): - ''' Return how many cards are remaining in the deck. ''' + def tiles_remaining(self): + ''' Return how many tiles are remaining in the deck. ''' return(self.count() - self.index) def hide(self): ''' Hide the deck. ''' - for card in self.cards: - if card is not None: - card.hide() + for tile in self.tiles: + if tile is not None: + tile.hide() def count(self): ''' Return the length of the deck. ''' - return len(self.cards) + return len(self.tiles) @@ -27,10 +27,10 @@ except ImportError: from grid import Grid from hand import Hand from deck import Deck -from card import error_card, highlight_cards +from tile import error_graphic, highlight_graphic from utils import json_dump -from constants import ROW, COL, NORTH, EAST, SOUTH, WEST, CARD_WIDTH, \ - CARD_HEIGHT, HIDE, BOARD, GRID, CARDS, TOP, OVER_THE_TOP +from constants import ROW, COL, NORTH, EAST, SOUTH, WEST, TILE_WIDTH, \ + TILE_HEIGHT, HIDE, BOARD, GRID, TILES, TOP, OVER_THE_TOP from sprites import Sprites OFFSETS = [-COL, 1, COL, -1] @@ -65,23 +65,23 @@ class Game(): self._width = gtk.gdk.screen_width() self._height = gtk.gdk.screen_height() - (GRID_CELL_SIZE * 1.5) - self._scale = self._height / (8.0 * CARD_HEIGHT) - self.card_width = CARD_WIDTH * self._scale - self.card_height = CARD_HEIGHT * self._scale + self._scale = self._height / (8.0 * TILE_HEIGHT) + self.tile_width = TILE_WIDTH * self._scale + self.tile_height = TILE_HEIGHT * self._scale # Generate the sprites we'll need... self._sprites = Sprites(self._canvas) self.grid = Grid(self._sprites, self._width, self._height, - self.card_width, self.card_height, self._scale, + self.tile_width, self.tile_height, self._scale, colors[0]) self.deck = Deck(self._sprites, self._scale, colors[1]) self.deck.board.move((self.grid.left, self.grid.top)) self.hands = [] - self.hands.append(Hand(self.card_width, self.card_height)) + self.hands.append(Hand(self.tile_width, self.tile_height)) self._errormsg = [] for i in range(4): - self._errormsg.append(error_card(self._sprites)) - self._highlight = highlight_cards(self._sprites, self._scale) + self._errormsg.append(error_graphic(self._sprites)) + self._highlight = highlight_graphic(self._sprites, self._scale) # and initialize a few variables we'll need. self.buddies = [] @@ -144,7 +144,7 @@ class Game(): # ...deal a hand to the robot... if self.playing_with_robot: if len(self.hands) < ROBOT_HAND + 1: - self.hands.append(Hand(self.card_width, self.card_height, + self.hands.append(Hand(self.tile_width, self.tile_height, remote=True)) self.hands[ROBOT_HAND].deal(self.deck) # ...or deal hands to the joiners. @@ -152,7 +152,7 @@ class Game(): for i, buddy in enumerate(self.buddies): if buddy != self._activity.nick: self.hands.append(Hand( - self.card_width, self.card_height, remote=True)) + self.tile_width, self.tile_height, remote=True)) self.hands[i].deal(self.deck) self._activity.send_event('h|%s' % \ (self.hands[i].serialize(buddy=buddy))) @@ -185,7 +185,7 @@ class Game(): # and I am no longer waiting for my turn. self._waiting_for_my_turn = False # If I don't have any tiles left, time to redeal. - if self.hands[self._my_hand].cards_in_hand() == 0: + if self.hands[self._my_hand].tiles_in_hand() == 0: self._redeal() if self._running_sugar: self._activity.set_player_on_toolbar(self._activity.nick) @@ -195,12 +195,12 @@ class Game(): self._set_label(_('It is your turn.')) def _redeal(self): - # Only the sharer deals cards. + # Only the sharer deals tiles. if not self.we_are_sharing(): self.hands[self._my_hand].deal(self.deck) if self.playing_with_robot: self.hands[ROBOT_HAND].deal(self.deck) - if self.hands[self._my_hand].cards_in_hand() == 0: + if self.hands[self._my_hand].tiles_in_hand() == 0: if self._running_sugar: self._activity.dialog_button.set_icon( 'media-playback-stop-insensitive') @@ -211,15 +211,15 @@ class Game(): if self.deck.empty(): self._set_label(_('Game over')) return - if self.deck.cards_remaining() < COL * len(self.buddies): - number_of_cards_to_deal = \ - int(self.deck.cards_remaining() / len(self.buddies)) - if number_of_cards_to_deal == 0: - number_of_cards_to_deal = 1 # Deal last card in deck. + if self.deck.tiles_remaining() < COL * len(self.buddies): + number_of_tiles_to_deal = \ + int(self.deck.tiles_remaining() / len(self.buddies)) + if number_of_tiles_to_deal == 0: + number_of_tiles_to_deal = 1 # Deal last tile in deck. else: - number_of_cards_to_deal = COL + number_of_tiles_to_deal = COL for i, nick in enumerate(self.buddies): - self.hands[i].deal(self.deck, number_of_cards_to_deal) + self.hands[i].deal(self.deck, number_of_tiles_to_deal) # Send the joiners their new hands. if nick != self._activity.nick: self._activity.send_event('h|%s' % \ @@ -247,7 +247,7 @@ class Game(): # I took my turn, so I am waiting again. self._waiting_for_my_turn = True if self.last_spr_moved is not None: - self.last_spr_moved.set_layer(CARDS) + self.last_spr_moved.set_layer(TILES) self.last_spr_moved = None self._hide_highlight() self._set_label(_('You took your turn.')) @@ -359,29 +359,29 @@ class Game(): if grid_pos is not None: # Placing tile in grid if self.grid.grid[grid_pos] is None: - card = self.deck.spr_to_card(self._press) - card.spr.move(self.grid.grid_to_xy(grid_pos)) + tile = self.deck.spr_to_tile(self._press) + tile.spr.move(self.grid.grid_to_xy(grid_pos)) i = self.grid.spr_to_grid(self._press) if i is not None: self.grid.grid[i] = None - self.grid.grid[grid_pos] = card + self.grid.grid[grid_pos] = tile self.placed_a_tile = True - self._last_tile_played = card.number + self._last_tile_played = tile.number self._last_grid_played = grid_pos i = self.hands[self._my_hand].spr_to_hand(self._press) if i is not None: self.hands[self._my_hand].hand[i] = None - if self.last_spr_moved != card.spr: - self.last_spr_moved = card.spr + if self.last_spr_moved != tile.spr: + self.last_spr_moved = tile.spr self._show_highlight() elif hand_pos is not None: # Returning tile to hand i = self.hands[self._my_hand].find_empty_slot() if i is not None: - card = self.deck.spr_to_card(self._press) - card.spr.move(self.hands[self._my_hand].hand_to_xy(i)) + tile = self.deck.spr_to_tile(self._press) + tile.spr.move(self.hands[self._my_hand].hand_to_xy(i)) if self.hands[self._my_hand].spr_to_hand( self._press) is not None: self.hands[self._my_hand].hand[ @@ -389,7 +389,7 @@ class Game(): self._press)] = None elif self.grid.spr_to_grid(self._press) is not None: self.grid.grid[self.grid.spr_to_grid(self._press)] = None - self.hands[self._my_hand].hand[i] = card + self.hands[self._my_hand].hand[i] = tile if spr == self.last_spr_moved: self.last_spr_moved = None self._hide_errormsgs() @@ -397,8 +397,8 @@ class Game(): else: # Or return tile to the grid grid_pos = self.grid.spr_to_grid(self._press) if grid_pos is not None: - card = self.deck.spr_to_card(self._press) - card.spr.move(self.grid.grid_to_xy(grid_pos)) + tile = self.deck.spr_to_tile(self._press) + tile.spr.move(self.grid.grid_to_xy(grid_pos)) self._hide_highlight() self._press = None self._release = None @@ -407,18 +407,18 @@ class Game(): self._release = spr if self._press == self._release and not self._it_is_a_drag(): # Rotate - card = self.deck.spr_to_card(spr) - card.rotate_clockwise() - self._last_tile_orientation = card.orientation - if self.last_spr_moved != card.spr: - self.last_spr_moved = card.spr + tile = self.deck.spr_to_tile(spr) + tile.rotate_clockwise() + self._last_tile_orientation = tile.orientation + if self.last_spr_moved != tile.spr: + self.last_spr_moved = tile.spr self._show_highlight() if hand_pos is None and x < self.grid.left: # In limbo: return to grid grid_pos = self.grid.spr_to_grid(self._press) if grid_pos is not None: - card = self.deck.spr_to_card(self._press) - card.spr.move(self.grid.grid_to_xy(grid_pos)) + tile = self.deck.spr_to_tile(self._press) + tile.spr.move(self.grid.grid_to_xy(grid_pos)) self._hide_highlight() self._test_for_bad_paths(self.grid.spr_to_grid(self._press)) @@ -429,7 +429,7 @@ class Game(): def _it_is_a_drag(self): if self._total_drag[0] * self._total_drag[0] + \ self._total_drag[1] * self._total_drag[1] > \ - self.card_width * self.card_height: + self.tile_width * self.tile_height: return True return False @@ -480,7 +480,7 @@ class Game(): self._set_label(_('Nowhere to play.')) def _robot_play(self): - ''' The robot tries random cards in random locations. ''' + ''' The robot tries random tiles in random locations. ''' # TODO: strategy try to complete paths order = self.deck.random_order(ROW * COL) for i in range(ROW * COL): @@ -491,7 +491,7 @@ class Game(): self.hands[ROBOT_HAND].hand[ self.hands[ROBOT_HAND].hand.index(tile)] = None tile.spr.move(self.grid.grid_to_xy(order[i])) - tile.spr.set_layer(CARDS) + tile.spr.set_layer(TILES) self._waiting_for_robot = False return @@ -617,16 +617,16 @@ class Game(): self._hide_errormsgs() self._there_are_errors = False if tile is not None: - self._check_card(tile, [int(tile / COL), 0], NORTH, + self._check_tile(tile, [int(tile / COL), 0], NORTH, tile + OFFSETS[0]) - self._check_card(tile, [tile % ROW, ROW - 1], EAST, + self._check_tile(tile, [tile % ROW, ROW - 1], EAST, tile + OFFSETS[1]) - self._check_card(tile, [int(tile / COL), COL - 1], SOUTH, + self._check_tile(tile, [int(tile / COL), COL - 1], SOUTH, tile + OFFSETS[2]) - self._check_card(tile, [tile % ROW, 0], WEST, tile + OFFSETS[3]) + self._check_tile(tile, [tile % ROW, 0], WEST, tile + OFFSETS[3]) - def _check_card(self, i, edge_check, direction, neighbor): - ''' Can a card be placed at position i? ''' + def _check_tile(self, i, edge_check, direction, neighbor): + ''' Can a tile be placed at position i? ''' if edge_check[0] == edge_check[1]: for path in self.grid.grid[i].paths: if path[direction] == 1: @@ -651,8 +651,8 @@ class Game(): [-0.125, 0.375]] x, y = self._press.get_xy() self._errormsg[direction].move( - (x + dxdy[direction][0] * self.card_width, - y + dxdy[direction][1] * self.card_height)) + (x + dxdy[direction][0] * self.tile_width, + y + dxdy[direction][1] * self.tile_height)) self._errormsg[direction].set_layer(OVER_THE_TOP) self._there_are_errors = True @@ -682,10 +682,10 @@ class Game(): else: # Giving a hint. x, y = pos self._highlight[0].move((x, y)) - self._highlight[1].move((x + 7 * self.card_width / 8, y)) - self._highlight[2].move((x + 7 * self.card_width / 8, - y + 7 * self.card_height / 8)) - self._highlight[3].move((x, y + 7 * self.card_height / 8)) + self._highlight[1].move((x + 7 * self.tile_width / 8, y)) + self._highlight[2].move((x + 7 * self.tile_width / 8, + y + 7 * self.tile_height / 8)) + self._highlight[3].move((x, y + 7 * self.tile_height / 8)) for i in range(4): self._highlight[i].set_layer(OVER_THE_TOP) @@ -13,15 +13,15 @@ import gtk from deck import Deck -from card import blank_card +from tile import blank_tile from utils import json_dump, json_load -from constants import ROW, COL, GRID, CARDS +from constants import ROW, COL, GRID, TILES class Grid: - ''' Class for managing ROWxCOL matrix of cards ''' + ''' Class for managing ROWxCOL matrix of tiles ''' - def __init__(self, sprites, width, height, card_width, card_height, scale, + def __init__(self, sprites, width, height, tile_width, tile_height, scale, color): # the playing surface self.grid = [] @@ -30,15 +30,15 @@ class Grid: for i in range(ROW * COL): self.grid.append(None) - # card spacing - self.left_hand = int(card_width / 2) - self.left = int((width - (card_width * COL)) / 2 + card_width) - self.xinc = int(card_width) + # tile spacing + self.left_hand = int(tile_width / 2) + self.left = int((width - (tile_width * COL)) / 2 + tile_width) + self.xinc = int(tile_width) self.top = 0 - self.yinc = int(card_height) + self.yinc = int(tile_height) for i in range(ROW * COL): - self.blanks.append(blank_card(sprites, scale=scale, color=color)) + self.blanks.append(blank_tile(sprites, scale=scale, color=color)) self.blanks[i].move(self.grid_to_xy(i)) self.blanks[i].set_layer(GRID) @@ -46,8 +46,8 @@ class Grid: for i in range(ROW * COL): self.grid[i] = None - def cards_in_grid(self): - ''' How many cards are on the grid? ''' + def tiles_in_grid(self): + ''' How many tiles are on the grid? ''' return ROW * COL - self.grid.count(None) def serialize(self): @@ -61,7 +61,7 @@ class Grid: return json_dump(grid) def restore(self, grid_as_text, deck): - ''' Restore cards to grid upon resume or share. ''' + ''' Restore tiles to grid upon resume or share. ''' self.hide() grid = json_load(grid_as_text) for i in range(ROW * COL): @@ -69,25 +69,25 @@ class Grid: self.grid[i] = None else: for k in range(ROW * COL): - if deck.cards[k].number == grid[i][0]: - self.add_card_to_grid(k, grid[i][1], i, deck) + if deck.tiles[k].number == grid[i][0]: + self.add_tile_to_grid(k, grid[i][1], i, deck) break self.show() - def add_card_to_grid(self, card_number, orientation, grid_number, deck): - ''' Add cards[card_number] to grid[grid_number] at orientation ''' - self.grid[grid_number] = deck.cards[card_number] + def add_tile_to_grid(self, tile_number, orientation, grid_number, deck): + ''' Add tiles[tile_number] to grid[grid_number] at orientation ''' + self.grid[grid_number] = deck.tiles[tile_number] self.grid[grid_number].spr.move(self.grid_to_xy(grid_number)) - self.grid[grid_number].spr.set_layer(CARDS) + self.grid[grid_number].spr.set_layer(TILES) while orientation > 0: self.grid[grid_number].rotate_clockwise() orientation -= 90 - def place_a_card(self, c, x, y): - ''' Place a card at position x,y and display it. ''' + def place_a_tile(self, c, x, y): + ''' Place a tile at position x,y and display it. ''' if c is not None: c.spr.move((x, y)) - c.spr.set_layer(CARDS) + c.spr.set_layer(TILES) def xy_to_grid(self, x, y): ''' Convert from sprite x,y to grid index. ''' @@ -114,13 +114,13 @@ class Grid: return None def hide(self): - ''' Hide all of the cards on the grid. ''' + ''' Hide all of the tiles on the grid. ''' for i in range(ROW * COL): if self.grid[i] is not None: self.grid[i].hide() def show(self): - ''' Restore all card on the grid to their x,y positions. ''' + ''' Restore all tile 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.place_a_tile(self.grid[i], self.grid_to_xy(i)[0], self.grid_to_xy(i)[1]) @@ -16,13 +16,13 @@ from utils import json_dump, json_load ROW = 8 COL = 8 -CARDS = 3 +TILES = 3 class Hand: - ''' Class for managing COL matrix of cards ''' + ''' Class for managing COL matrix of tiles ''' - def __init__(self, card_width, card_height, remote=False): + def __init__(self, tile_width, tile_height, remote=False): # The tiles in your hand self.hand = [] self.remote = remote # Does this hand belong to someone remote? @@ -30,26 +30,26 @@ class Hand: for i in range(COL): self.hand.append(None) - # Card spacing - self.xinc = int(card_width) + # Tile spacing + self.xinc = int(tile_width) if self.remote: self.left = -self.xinc else: - self.left = int(card_width / 2) + self.left = int(tile_width / 2) self.top = 0 - self.yinc = int(card_height) + self.yinc = int(tile_height) def clear(self): for i in range(COL): self.hand[i] = None def deal(self, deck, number=COL): - ''' Deal an initial set of cards to the hand ''' + ''' Deal an initial set of tiles to the hand ''' for i in range(number): - self.hand[i] = deck.deal_next_card() + self.hand[i] = deck.deal_next_tile() if self.hand[i] is not None: self.hand[i].spr.move(self.hand_to_xy(i)) - self.hand[i].spr.set_layer(CARDS) + self.hand[i].spr.set_layer(TILES) return True def find_empty_slot(self): @@ -59,8 +59,8 @@ class Hand: return i return None - def cards_in_hand(self): - ''' How many cards are in the hand? ''' + def tiles_in_hand(self): + ''' How many tiles are in the hand? ''' return COL - self.hand.count(None) def serialize(self, buddy=None): @@ -77,22 +77,22 @@ class Hand: return json_dump(hand) def restore(self, hand_as_text, deck, buddy=False): - ''' Restore cards to hand upon resume or share. ''' + ''' Restore tiles to hand upon resume or share. ''' hand = json_load(hand_as_text) if buddy: offset = 1 # skip the buddy else: offset = 0 - for card in range(COL): - i = card + offset + for tile in range(COL): + i = tile + offset if hand[i] is None: self.hand[i] = None else: for k in range(ROW * COL): - if deck.cards[k].number == hand[i]: - self.hand[card] = deck.cards[k] - self.hand[card].spr.move(self.hand_to_xy(card)) - self.hand[card].spr.set_layer(CARDS) + if deck.tiles[k].number == hand[i]: + self.hand[tile] = deck.tiles[k] + self.hand[tile].spr.move(self.hand_to_xy(tile)) + self.hand[tile].spr.set_layer(TILES) break def xy_to_hand(self, x, y): @@ -0,0 +1,107 @@ +#Copyright (c) 2009-11 Walter Bender + +# This program is free software; you can redistribute it and/or modify +# 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. + +import gtk +from constants import NORTH, EAST, SOUTH, WEST, HIDE, TILES +from sprites import Sprite +from utils import svg_str_to_pixbuf + + +class Tile: + + def __init__(self, sprites, svg, svgs, tile_type='tile', number=0): + self.spr = Sprite(sprites, 0, 0, svg_str_to_pixbuf(svg)) + self.highlight = [self.spr.images[0]] + for s in svgs: + self.highlight.append(svg_str_to_pixbuf(s)) + self.paths = [] # [[N, E, S, W], [N, E, S, W]] + self.shape = None + self.orientation = 0 + self.type = tile_type + self.number = number + self.spr.set_label_color('#FF0000') + # self.spr.set_label_attributes(32) + # self.spr.set_label(str(number)) + + def set_paths(self, paths): + for c in paths: + self.paths.append(c) + + def get_paths(self): + return self.paths + + def reset(self): + self.spr.set_layer(HIDE) + self.shape = None + self.spr.set_shape(self.highlight[0]) + while self.orientation != 0: + self.rotate_clockwise() + + def set_shape(self, path): + if self.shape is None: + self.spr.set_shape(self.highlight[path + 1]) + self.shape = path + elif self.shape != path: + self.spr.set_shape(self.highlight[-1]) + + def rotate_clockwise(self): + """ rotate the tile and its paths """ + for i in range(len(self.paths)): + west = self.paths[i][WEST] + self.paths[i][WEST] = self.paths[i][SOUTH] + self.paths[i][SOUTH] = self.paths[i][EAST] + self.paths[i][EAST] = self.paths[i][NORTH] + self.paths[i][NORTH] = west + self.spr.images[0] = self.spr.images[0].rotate_simple(270) + for h in range(len(self.highlight)): + self.highlight[h] = self.highlight[h].rotate_simple(270) + self.spr.draw() + self.orientation += 90 + self.orientation %= 360 + + def show_tile(self): + self.spr.set_layer(CARDS) + self.spr.draw() + + def hide(self): + self.spr.move((-self.spr.images[0].get_width(), 0)) + + +# +# Utilities used to create graphics used for interactions +# +from genpieces import generate_board, generate_x, generate_blank, \ + generate_corners + + +def board_card(sprites, scale=1.0): + return Sprite(sprites, 0, 0, svg_str_to_pixbuf(generate_board(scale))) + + +def error_graphic(sprites, scale=1.0): + return Sprite(sprites, -100, 0, svg_str_to_pixbuf(generate_x(0.5 * scale))) + + +def blank_tile(sprites, scale=1.0, color='#80FF80'): + return Sprite(sprites, 0, 0, svg_str_to_pixbuf( + generate_blank(scale, color))) + + +def highlight_graphic(sprites, scale=1.0): + return [Sprite(sprites, -100, 0, svg_str_to_pixbuf( + generate_corners(0, 0.125 * scale))), + Sprite(sprites, -100, 0, svg_str_to_pixbuf( + generate_corners(1, 0.125 * scale))), + Sprite(sprites, -100, 0, svg_str_to_pixbuf( + generate_corners(2, 0.125 * scale))), + Sprite(sprites, -100, 0, svg_str_to_pixbuf( + generate_corners(3, 0.125 * scale)))] |