martes, 6 de diciembre de 2011

wxPython example. A jigsaw puzzle (2)

Continuing with the previous post I created another puzzle, but this time the pieces has the shape of the mainland Spain provinces. I think is a fun way to learn geography and can be used for any map.



The code is basically what described in the previous post. The only difference is the way the pieces masks are created. I did it manually with gimp. For example:



The main file of the application, which I call prov.py, has the following code:



#!/usr/bin/python

import board
import stateDB
import rectangle
import piece
import wx
import random

# Id, image, mask, size, top-left positions, relations
provinceList = [
[1, 'coruna.png', 'corunaMask.png', (106, 96), (0,0), [2, 3] ],
[2, 'lugo.png', 'lugoMask.png', (72, 119), (82,0), [1, 3, 4, 5, 37 ]],
[3, 'pontevedra.png', 'pontevedraMask.png', (70, 79), (17, 73), [1,2,4]],
[4, 'orense.png', 'orenseMask.png', (97, 70), (60,94), [2, 3, 25, 37]],
[5, 'asturias.png', 'asturiasMask.png', (159, 66), (134,11), [2,6, 37]],
[6, 'cantabria.png', 'cantabriaMask.png', (104,64), (274, 25), [5,7, 37,
40, 41]],
[7, 'vizcaya.png', 'vizcayaMask.png', (68, 35), (356,32), [6, 8, 9, 40]],
[8, 'guipuzcoa.png', 'guipuzcoaMask.png', (50, 42), (412,34), [7, 9, 10]],
[9, 'alava.png','alavaMask.png', (62, 60), (372, 52), [8, 7, 10, 39, 40 ]],
[10, 'navarra.png', 'navarraMask.png', (102, 116), (423, 38), [8,9, 11,
12, 39]],
[11, 'huesca.png', 'huescaMask.png', (112, 126), (511,69), [10, 12, 14 ]],
[12, 'zaragoza.png', 'zaragozaMask.png', (160, 147), (441, 85), [11, 10,
13, 14, 17, 18, 38]],
[13, 'teruel.png', 'teruelMask.png', (132, 122), (464, 197), [12, 17, 18,
33, 34, 35]],
[14, 'lleida.png', 'lleidaMask.png', (93,133), (596, 69), [ 11, 12, 15,
16, 17]],
[15, 'girona.png', 'gironaMask.png', (94, 72), (683,89), [ 14, 16 ]],
[16, 'barcelona.png', 'barcelonaMask.png', (85, 96), (662, 108), [14, 15,
17]],
[17, 'tarra.png', 'tarraMask.png', (93,91), (589,169), [16, 14, 13, 12,
34]],
[18, 'guada.png', 'guadaMask.png', (127, 95), (354, 202), [ 13, 12, 19,
35, 38, 43]],
[19, 'madrid.png', 'madridMask.png', (101,100), (287, 219), [18, 20, 35,
43, 44]],
[20, 'avila.png', 'avilaMask.png', (103,91), (212,216), [19, 21, 24, 42,
43, 44]],
[21, 'caceres.png', 'caceresMask.png', (165, 119), (96, 270), [20, 22,
24, 44]],
[22, 'badajoz.png', 'badajozMask.png', (177,125), (106, 352), [21, 23, 26,
44, 45, 47]],
[23, 'huelva.png', 'huelvaMask.png', (91,112), (87, 456), [22, 26 ]],
[24, 'salamanca.png', 'salamancaMask.png',(113, 94), (141,198), [20, 21,
25, 42 ]],
[25, 'zamora.png', 'zamoraMask.png',(111, 84), (136,127), [24, 4, 37, 42 ]],
[26, 'sevilla.png', 'sevillaMask.png',(128, 116), (153, 456), [22, 23,
27, 28, 47 ]],
[27, 'cadiz.png', 'cadizMask.png', (96,84), (154, 555), [26, 28]],
[28, 'malaga.png', 'malagaMask.png', (122,84), (221, 532), [27, 26, 29,
47]],
[29, 'granada.png', 'granadaMask.png', (138,117), (303, 466), [28,30, 31,
36, 46, 47]],
[30, 'almeria.png', 'almeriaMask.png', (105,103), (381, 480), [29, 31]],
[31, 'murcia.png', 'murciaMask.png', (108, 114), (434, 410), [29, 32, 36]],
[32, 'alicante.png', 'alicanteMask.png', (81,92), (520,391), [31, 33, 36]],
[33, 'valencia.png', 'valenciaMask.png', (95,121), (485,292), [32, 13, 34,
35, 36]],
[34, 'castellon.png', 'castellonMask.png', (86,89), (526,242), [33, 13,
17]],
[35, 'cuenca.png', 'cuencaMask.png', (132, 118), (376, 256), [18, 19, 13,
33, 36, 44, 45]],
[36, 'albacete.png', 'albaceteMask.png', (129,119), (397, 354), [35, 29,
36, 32, 33, 31, 45, 46]],
[37, 'leon.png', 'leonMask.png', (143, 102), (138, 50), [2, 4, 25, 5, 6,
41, 42]],
[38, 'soria.png', 'soriaMask.png', (107, 89), (358,136), [12, 18, 39, 40,
43 ]],
[39, 'rioja.png', 'riojaMask.png', (83,55), (383, 96), [38, 9, 10, 40]],
[40, 'burgos.png', 'burgosMask.png', (110,141), (306,52), [39, 38, 6, 7,
9, 41, 42, 43]],
[41, 'palencia.png', 'palenciaMask.png', (70,100), (262,65), [37, 6, 40,
42 ]],
[42, 'valla.png', 'vallaMask.png', (96,98), (235, 125), [37, 40, 41, 20,
25, 24, 43]],
[43, 'segovia.png', 'segoviaMask.png', (92,78), (280, 185), [42, 40, 38,
18, 19, 20]],
[44, 'toledo.png', 'toledoMask.png', (160, 83), (233, 287), [35,21, 22,
19, 20, 45]],
[45, 'creal.png', 'crealMask.png', (158, 103), (255, 344), [44, 36, 35,
22, 46, 47 ]],
[46, 'Jaen.png', 'JaenMask.png', (122, 98), (306, 430), [45, 36, 29, 47]],
[47, 'cordoba.png', 'cordobaMask.png', (109,129), (218,412), [46, 45, 26,
28, 29, 22]]
]


def createPieceList (provinceList, boardSize):
pieces = []

for entry in provinceList:
im = wx.Image (entry[1])
mask = wx.Image (entry[2])
im.SetMaskFromImage (mask, 255, 255, 255)

rels = []
# Calculate the relations
for related in entry[5]:
index = related -1
difX = provinceList [index][4][0] - entry[4][0]
difY = provinceList [index][4][1] - entry[4][1]
rel = [related, (difX, difY), 0, 0]
rels.append (rel)

rec = rectangle.Rectangle (entry[0], (im, mask), entry[3],
rels, 0, 0)
p = piece.Piece (entry[0])
p.addRectangle (rec, (0, 0))
pieces.append (p)

for i in range (len(pieces)):
pieces[i].setPosition ( (random.randint (10, boardSize[0] - 300),\
random.randint(10, boardSize[1] - 300)))
#pieces[i].setOrientation (rectangle.getRandomOrientation())

# Re-order the list so we don't always get the same provinces on top
random.shuffle (pieces)

return pieces

boardSize = (1000, 800)
app = wx.App ()
db = stateDB.StateDB()
count = -1
seconds = 0

if db.isThereADBFile ():
count, seconds, pieces = db.readBoard (board.Board.boardId)

if count <=0:
pieces= createPieceList (provinceList, boardSize)
count = 0

board.Puzzle(None, -1, 'Spain mainland provinces', pieces, boardSize, count,
seconds, False)
app.MainLoop()


No hay comentarios:

Publicar un comentario