diff options
author | Walter Bender <walter@sugarlabs.org> | 2010-09-10 00:34:49 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2010-09-10 00:34:49 (GMT) |
commit | 6a817aa19ffe517a760a771e734a0ddea894c64a (patch) | |
tree | bdba92e2f3f654e0bf5717c0600916ccce158b6f | |
parent | 6577efe934883d82f67b121b5b273ba51904f2f5 (diff) |
added hexagon game
-rw-r--r-- | PukllanapacActivity.py | 2 | ||||
-rw-r--r-- | card.py | 12 | ||||
-rwxr-xr-x | gentiles.py | 35 | ||||
-rw-r--r-- | grid.py | 28 | ||||
-rw-r--r-- | window.py | 1 |
5 files changed, 54 insertions, 24 deletions
diff --git a/PukllanapacActivity.py b/PukllanapacActivity.py index 382f319..2eda863 100644 --- a/PukllanapacActivity.py +++ b/PukllanapacActivity.py @@ -45,7 +45,7 @@ SERVICE = 'org.sugarlabs.PukllanapacActivity' IFACE = SERVICE PATH = '/org/augarlabs/PukllanapacActivity' LEVEL_ICONS = ['level1', 'level2', 'level3'] -GAME_ICONS = ['rectangle', 'hexagon'] +GAME_ICONS = ['rectangle', 'hexagon', 'hexagon2'] def _button_factory(icon_name, tooltip, callback, toolbar, cb_arg=None, @@ -29,13 +29,16 @@ class Card: """ Load a card from a precomputed SVG. """ self.images = [] self.orientation = 0 - self.increment = 90 if shape == 'triangle': file = "%s/triangle-r0-%d.svg" % (path, c) self.increment = 60 + elif shape == 'hexagon': + file = "%s/hexagon-r0-%d.svg" % (path, c) + self.increment = 120 else: file = "%s/card-%d.svg" % (path, c) + self.increment = 90 self.images.append(load_image(file, card_dim * scale, card_dim * scale)) @@ -56,6 +59,13 @@ class Card: file = "%s/triangle-r300-%d.svg" % (path, c) self.images.append(load_image(file, card_dim * scale, card_dim * scale)) + elif shape == 'hexagon': + file = "%s/hexagon-r120-%d.svg" % (path, c) + self.images.append(load_image(file, card_dim * scale, + card_dim * scale)) + file = "%s/hexagon-r240-%d.svg" % (path, c) + self.images.append(load_image(file, card_dim * scale, + card_dim * scale)) else: for r in range(3): self.images.append(self.images[r].rotate_simple(90)) diff --git a/gentiles.py b/gentiles.py index 43864ef..4cbdcd7 100755 --- a/gentiles.py +++ b/gentiles.py @@ -30,7 +30,7 @@ def _svg_header(): g = ' width="200"\n' h = ' height="200"\n' i = ' id="svg2">\n' - j = ' <g transform="translate(0,-852.36218)">\n' + j = ' <g transform="translate(0,-852.4)">\n' k = ' <rect\n' l = ' width="200"\n' m = ' height="200"\n' @@ -43,8 +43,8 @@ def _svg_header(): def _svg_circle(x, y): """ draw an arc at x, y """ a = ' <path\n' - b = ' d="m 342.85714,826.64789 a 97.14286,88.571426 0 1 1 -194.28572,0 97.14286,88.571426 0 1 1 194.28572,0 z"\n' - c = ' transform="matrix(0.76176468,0,0,0.83548389,' + x + ',' + y +')"\n' + b = ' d="m 342.9,826.6 a 97.1,88.6 0 1 1 -194.3,0 97.1,88.6 0 1 1 194.3,0 z"\n' + c = ' transform="matrix(0.76,0,0,0.84,' + x + ',' + y +')"\n' return a + b + c @@ -82,7 +82,7 @@ def _svg_footer(): b = '</svg>\n' return a + b -def _svg_hex(top, left, right): +def _svg_hex(top, left, right, rotate=0): """ draw a hexagon """ a = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n' b = '<!-- Created with Python in Emacs -->\n' @@ -90,18 +90,23 @@ def _svg_hex(top, left, right): d = ' xmlns:svg="http://www.w3.org/2000/svg"\n' e = ' xmlns="http://www.w3.org/2000/svg"\n' f = ' version="1.1"\n' - g = ' width="202.28767"\n' - h = ' height="231.94138"\n' + g = ' width="202."\n' + h = ' height="231.9"\n' i = ' id="hexagon">\n' - j = ' <g transform="translate(-270.9034,-365.15558)">\n' + if rotate == 120: + j = ' <g transform="matrix(-0.5,-0.87,0.87,-0.5,-129.5,678.7)">\n' + elif rotate == 240: + j = ' <g transform="matrix(-0.5,0.87,-0.87,-0.5,703.5,34.5)">\n' + else: + j = ' <g transform="translate(-270.9,-365.2)">\n' k = ' <path\n' - l = ' d="m 272.04724,423.15558 100.55096,-57.5 99.44904,57.5 -100.55096,57.5 -99.44904,-57.5 z"\n' + l = ' d="m 272.0,423.2 100.6,-57.5 99.4,57.5 -100.6,57.5 -99.4,-57.5 z"\n' m = ' style="fill:' + top + ';fill-opacity:1;stroke:#000000;stroke-width:4;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />\n' n = ' <path\n' - o = ' d="m 371.47534,596.59696 -100.07194,-58.32969 0.0719,-114.87539 100.07194,58.32969 -0.0719,114.87539 z"\n' + o = ' d="m 371.5,596.6 -100.1,-58.3 0.1,-114.9 100.1,58.3 -0.1,114.9 z"\n' p = ' style="fill:' + left + '#0000ff;fill-opacity:1;stroke:#000000;stroke-width:4;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />\n' q = ' <path\n' - r = ' d="m 372.61914,596.3201 100.07194,-58.32969 -0.0719,-114.87539 -100.07194,58.32969 0.0719,114.87539 z"\n' + r = ' d="m 372.6,596.3 100.1,-58.3 -0.1,-114.9 -100.1,58.3 0.1,114.9 z"\n' s = ' style="fill:' + right + ';fill-opacity:1;stroke:#000000;stroke-width:4;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" />\n' t = ' </g>\n' u = '</svg>\n' @@ -157,7 +162,7 @@ def open_file(datapath, filename): def generator(datapath): - """ Make all of the cards """ + """ Make the cards: squares with circles, hexagons, and triangles """ for i, c in enumerate(C): f = open_file(datapath, "card-%d.svg" % (i)) svg = _svg_header() @@ -170,9 +175,15 @@ def generator(datapath): f.close() for i, c in enumerate(H): - f = open_file(datapath, "hex-%d.svg" % (i)) + f = open_file(datapath, "hexagon-r0-%d.svg" % (i)) f.write(_svg_hex(c[0], c[1], c[2])) f.close() + f = open_file(datapath, "hexagon-r120-%d.svg" % (i)) + f.write(_svg_hex(c[0], c[1], c[2], 120)) + f.close() + f = open_file(datapath, "hexagon-r240-%d.svg" % (i)) + f.write(_svg_hex(c[0], c[1], c[2], 240)) + f.close() for i, c in enumerate(RT): f = open_file(datapath, "triangle-r0-%d.svg" % (i)) @@ -24,6 +24,10 @@ GRID_TO_HEX = [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26] HEX_ORIENTATION = [-1, 180, 0, -1, 180, 0, 180, 0, 0, 180, 0, 180, 180, 0, 180, 0, 0, 180, 0, 180, 180, 0, 180, 0, -1, 180, 0, -1] +HEX2_TO_GRID = [-1, 0, 1, 2, 3, -1, 4, 5, 6, 7, 8, -1, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, -1, -1, 20, 21, 22, 23, -1] +GRID_TO_HEX2 = [1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 25, 26, 27, 28] class Grid: """ Class for defining matrix of cards """ @@ -38,6 +42,7 @@ class Grid: self.d = int(tw.card_dim * tw.scale) self.dx = self.d * 0.85 self.dy = self.d * 0.5 + self.dy2 = self.d * 0.75 self.s = tw.scale self.initialize_cards(tw.sprites, tw.path, tw.card_dim, tw.scale, shape) @@ -53,6 +58,9 @@ class Grid: x, y, 'triangle')) self.card_table[i].set_orientation( HEX_ORIENTATION[GRID_TO_HEX[i]]) + elif shape == 'hexagon2': + self.card_table.append(Card(sprites, path, card_dim, scale, i, + x, y, 'hexagon')) else: self.card_table.append(Card(sprites, path, card_dim, scale, i, x, y)) @@ -64,22 +72,22 @@ class Grid: (GRID_TO_HEX[i] % 4) * self.dx, \ int((self.h - (self.dy * 7)) / 2) + \ int(GRID_TO_HEX[i] / 4) * self.dy + elif shape == 'hexagon2': # 6 x 5 with empty corners + if int(GRID_TO_HEX2[i]/6) == 1 or \ + int(GRID_TO_HEX2[i]/6) == 3: + hoffset = self.dx / 2 + else: + hoffset = 0 + return int((self.w - (self.dx * 6)) / 2) + \ + (GRID_TO_HEX2[i] % 6) * self.dx + hoffset, \ + int((self.h - (self.dy2 * 5)) / 2) + \ + int(GRID_TO_HEX2[i] / 6) * self.dy2 else: # 6 x 4 return int((self.w - (self.d * 6)) / 2) + \ (i % 6) * self.d - 10 + i % 6 * 4, \ int((self.h - (self.d * 4)) / 2) + \ int( i / 6) * self.d - 6 + int(i / 6) * 4 - def xy_to_i(self, x, y, shape='rectangle'): - """ Convert an x, y position to a grid index """ - if shape == 'hexagon': - return HEX_TO_GRID[(x - int((self.w - (self.d * 6)) / 2)) \ - / self.d + \ - ((y - int((self.h - (self.d * 4)) / 2)) / self.d) * 6] - else: - return (x - int((self.w - (self.d * 6)) / 2)) / self.d + \ - ((y - int((self.h - (self.d * 4)) / 2)) / self.d) * 6 - def restore_grid(self, grid, shape='rectangle'): """ Move cards to x, y positions specified in grid """ for i, c in enumerate(grid): @@ -35,6 +35,7 @@ LEVEL_BOUNDS = [[[1, 2], [0, 1], [2, 3], [1, 2]], [[1, 2], [0, 1], [1, 4], [0, 3]], [[0, 3], [-1, 2], [1, 5], [-1, 4]]] + class Game(): """ The game play -- called from within Sugar or GNOME """ |