1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#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 deck import Deck
from tile import blank_tile
from utils import json_dump, json_load
from constants import ROW, COL, GRID, TILES
class Grid:
''' Class for managing ROWxCOL matrix of tiles '''
def __init__(self, sprites, width, height, tile_width, tile_height, scale,
color):
# the playing surface
self.grid = []
self.blanks = []
for i in range(ROW * COL):
self.grid.append(None)
# 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(tile_height)
for i in range(ROW * COL):
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)
def clear(self):
for i in range(ROW * COL):
self.grid[i] = None
def tiles_in_grid(self):
''' How many tiles are on the grid? '''
return ROW * COL - self.grid.count(None)
def serialize(self):
''' Serialize the grid for passing to share and saving '''
grid = []
for i in range(ROW * COL):
if self.grid[i] is not None:
grid.append([self.grid[i].number, self.grid[i].orientation])
else:
grid.append([None, None])
return json_dump(grid)
def restore(self, grid_as_text, deck):
''' Restore tiles to grid upon resume or share. '''
self.hide()
grid = json_load(grid_as_text)
for i in range(ROW * COL):
if grid[i][0] is None:
self.grid[i] = None
else:
for k in range(ROW * COL):
if deck.tiles[k].number == grid[i][0]:
self.add_tile_to_grid(k, grid[i][1], i, deck)
break
self.show()
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(TILES)
while orientation > 0:
self.grid[grid_number].rotate_clockwise()
orientation -= 90
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(TILES)
def xy_to_grid(self, x, y):
''' Convert from sprite x,y to grid index. '''
if x > self.left:
return COL * int((y - self.top) / self.yinc) + \
int((x - self.left) / self.xinc)
else:
return None
def grid_to_xy(self, i):
''' Convert from grid index to sprite x,y. '''
return (int((self.left + i % COL * self.xinc)),
int((self.top + (i / COL) * self.yinc)))
def grid_to_spr(self, 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. '''
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 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 tile on the grid to their x,y positions. '''
for i in range(ROW * COL):
self.place_a_tile(self.grid[i], self.grid_to_xy(i)[0],
self.grid_to_xy(i)[1])
|