Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter.bender@gmail.com>2011-11-20 20:41:12 (GMT)
committer Walter Bender <walter.bender@gmail.com>2011-11-20 20:41:12 (GMT)
commit8d11abfe6f8f4ac7d96adf340301fc33afc5ca02 (patch)
tree755a6ed09161d0c4bbbc3c9b100e2de6e5ff5340
parentdc49b844c7f4ef7a0035544ddd7beddd1a5c6cb9 (diff)
code cleanupv6
-rw-r--r--CardSortActivity.py35
-rw-r--r--card.py27
-rwxr-xr-xcardsort.py67
-rw-r--r--grid.py258
-rw-r--r--orientation.py10
-rw-r--r--window.py269
6 files changed, 328 insertions, 338 deletions
diff --git a/CardSortActivity.py b/CardSortActivity.py
index 48fd06e..91abbd1 100644
--- a/CardSortActivity.py
+++ b/CardSortActivity.py
@@ -1,14 +1,13 @@
-#Copyright (c) 2009,10 Walter Bender
+#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.
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import pygtk
pygtk.require('2.0')
@@ -35,7 +34,7 @@ import locale
import os.path
from sprites import *
-import window
+from window import Game
SERVICE = 'org.sugarlabs.CardSortActivity'
IFACE = SERVICE
@@ -128,17 +127,15 @@ class CardSortActivity(activity.Activity):
# Create a canvas
canvas = gtk.DrawingArea()
- canvas.set_size_request(gtk.gdk.screen_width(), \
+ canvas.set_size_request(gtk.gdk.screen_width(),
gtk.gdk.screen_height())
self.set_canvas(canvas)
canvas.show()
self.show_all()
# Initialize the canvas
- self.tw = window.new_window(canvas, \
- os.path.join(activity.get_bundle_path(), \
- 'images'), \
- self)
+ self.game = Game(canvas, os.path.join(
+ activity.get_bundle_path(), 'images'), self)
# Read the mode from the Journal
try:
@@ -167,8 +164,8 @@ class CardSortActivity(activity.Activity):
self.grid3x2.set_icon("3x2off")
self.grid2x3.set_icon("2x3off")
self.grid3x3.set_icon("3x3off")
- self.tw.test = self.tw.grid.test2x2
- self.tw.grid.reset2x2(self.tw)
+ self.game.test = self.game.grid.test2x2
+ self.game.grid.reset2x2(self.game)
self.metadata['grid'] = "2x2"
def _grid3x2_cb(self, button):
@@ -180,8 +177,8 @@ class CardSortActivity(activity.Activity):
self.grid3x2.set_icon("3x2on")
self.grid2x3.set_icon("2x3off")
self.grid3x3.set_icon("3x3off")
- self.tw.test = self.tw.grid.test3x2
- self.tw.grid.reset3x2(self.tw)
+ self.game.test = self.game.grid.test3x2
+ self.game.grid.reset3x2(self.game)
self.metadata['grid'] = "3x2"
def _grid2x3_cb(self, button):
@@ -193,8 +190,8 @@ class CardSortActivity(activity.Activity):
self.grid3x2.set_icon("3x2off")
self.grid2x3.set_icon("2x3on")
self.grid3x3.set_icon("3x3off")
- self.tw.test = self.tw.grid.test2x3
- self.tw.grid.reset2x3(self.tw)
+ self.game.test = self.game.grid.test2x3
+ self.game.grid.reset2x3(self.game)
self.metadata['grid'] = "2x3"
def _grid3x3_cb(self, button):
@@ -206,8 +203,8 @@ class CardSortActivity(activity.Activity):
self.grid3x2.set_icon("3x2off")
self.grid2x3.set_icon("2x3off")
self.grid3x3.set_icon("3x3on")
- self.tw.test = self.tw.grid.test3x3
- self.tw.grid.reset3x3(self.tw)
+ self.game.test = self.game.grid.test3x3
+ self.game.grid.reset3x3(self.game)
self.metadata['grid'] = "3x3"
"""
diff --git a/card.py b/card.py
index c5e9a40..0f08a20 100644
--- a/card.py
+++ b/card.py
@@ -1,14 +1,13 @@
-#Copyright (c) 2009,10 Walter Bender
+#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.
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import pygtk
pygtk.require('2.0')
@@ -29,28 +28,27 @@ class Card:
# Heart = 2,-2
# Club = 3,-3
# Diamond = 4,-4
- def __init__(self,tw,c,i,x,y):
+ def __init__(self, game, c, i, x, y):
self.north = c[0]
self.east = c[1]
self.south = c[2]
self.west = c[3]
self.orientation = 0
self.images = []
- file = "%s/card%d.svg" % (tw.path,i)
- self.images.append(load_image(file, tw.card_dim*tw.scale,
- tw.card_dim*tw.scale))
+ self.images.append(load_image(
+ os.path.join(game.path, 'card%d.svg' % (i)),
+ game.card_dim * game.scale, game.card_dim * game.scale))
for j in range(3):
self.images.append(self.images[j].rotate_simple(90))
# create sprite from svg file
- self.spr = Sprite(tw.sprites, x, y, self.images[0])
+ self.spr = Sprite(game.sprites, x, y, self.images[0])
self.spr.set_label(i)
- self.spr.draw()
- def reset_image(self, tw, i):
+ def reset_image(self, game, i):
while self.orientation != 0:
self.rotate_ccw()
- def set_orientation(self,r,rotate_spr=True):
+ def set_orientation(self, r, rotate_spr=True):
while r != self.orientation:
self.rotate_ccw(rotate_spr)
@@ -65,8 +63,7 @@ class Card:
if self.orientation == 360:
self.orientation = 0
if rotate_spr is True:
- self.spr.set_shape(self.images[int(self.orientation/90)])
- self.spr.draw()
+ self.spr.set_shape(self.images[int(self.orientation / 90)])
def print_card(self):
print "(" + str(self.north) + "," + str(self.east) + \
diff --git a/cardsort.py b/cardsort.py
index bd3ddb0..fa4f68f 100755
--- a/cardsort.py
+++ b/cardsort.py
@@ -1,16 +1,15 @@
#!/usr/bin/env python
-#Copyright (c) 2009,10 Walter Bender
+#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.
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import pygtk
pygtk.require('2.0')
@@ -19,12 +18,10 @@ import gtk
from gettext import gettext as _
import os.path
-import window
-import grid
-import card
-import sprites
+from window import Game
from orientation import get_rotation_sets
+
class CardSortMain:
def __init__(self):
self.r = 0
@@ -83,12 +80,10 @@ class CardSortMain:
self.win.show_all()
# Start the activity
- self.tw = window.new_window(canvas, \
- os.path.join(os.path.abspath('.'), \
- 'images'))
- self.tw.win = self.win
- self.tw.test = self.tw.grid.test2x2
- self.tw.grid.reset2x2(self.tw)
+ self.game = Game(canvas, os.path.join(os.path.abspath('.'), 'images'))
+ self.game.win = self.win
+ self.game.test = self.game.grid.test2x2
+ self.game.grid.reset2x2(self.game)
def set_title(self, title):
self.win.set_title(title)
@@ -97,20 +92,20 @@ class CardSortMain:
# Grid resize callbacks
#
def _grid2x2_cb(self, button):
- self.tw.test = self.tw.grid.test2x2
- self.tw.grid.reset2x2(self.tw)
+ self.game.test = self.game.grid.test2x2
+ self.game.grid.reset2x2(self.game)
def _grid3x2_cb(self, button):
- self.tw.test = self.tw.grid.test3x2
- self.tw.grid.reset3x2(self.tw)
+ self.game.test = self.game.grid.test3x2
+ self.game.grid.reset3x2(self.game)
def _grid2x3_cb(self, button):
- self.tw.test = self.tw.grid.test2x3
- self.tw.grid.reset2x3(self.tw)
+ self.game.test = self.game.grid.test2x3
+ self.game.grid.reset2x3(self.game)
def _grid3x3_cb(self, button):
- self.tw.test = self.tw.grid.test3x3
- self.tw.grid.reset3x3(self.tw)
+ self.game.test = self.game.grid.test3x3
+ self.game.grid.reset3x3(self.game)
def _solve_cb(self, widget):
self.show_all()
@@ -124,30 +119,30 @@ class CardSortMain:
return True
counter += 1
if (counter/1000)*1000 == counter:
- print str(counter) + ": " + str(self.tw.grid.grid)
+ print str(counter) + ": " + str(self.game.grid.grid)
print "no solution found :("
return True
def test(self,g):
- self.tw.grid.grid = g
+ self.game.grid.grid = g
for o in range(64):
for r in range(9):
- self.tw.grid.card_table[self.tw.grid.grid.index(r)]\
+ self.game.grid.card_table[self.game.grid.grid.index(r)]\
.set_orientation(self.rotation_sets[o][r],False)
- if self.tw.test() is True:
+ if self.game.test() is True:
print _("You solved the puzzle.")
- self.tw.grid.print_grid()
- self.tw.grid.print_orientations()
- self.tw.win.set_title(_("CardSort") + ": " + \
+ self.game.grid.print_grid()
+ self.game.grid.print_orientations()
+ self.game.win.set_title(_("CardSort") + ": " + \
_("You solved the puzzle."))
- self.tw.grid.reset3x3(self.tw)
- self.tw.grid.set_grid(g)
+ self.game.grid.reset3x3(self.game)
+ self.game.grid.set_grid(g)
for r in range(9):
- self.tw.grid.card_table[self.tw.grid.grid.index(r)]\
+ self.game.grid.card_table[self.game.grid.grid.index(r)]\
.set_orientation(self.rotation_sets[o][r],True)
- self.tw.grid.print_grid()
- self.tw.grid.print_orientations()
- self.tw.sprites.redraw_sprites()
+ self.game.grid.print_grid()
+ self.game.grid.print_orientations()
+ self.game.sprites.redraw_sprites()
return True
return False
diff --git a/grid.py b/grid.py
index 3d70450..dfbec1d 100644
--- a/grid.py
+++ b/grid.py
@@ -1,27 +1,27 @@
-#Copyright (c) 2009,10 Walter Bender
+#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.
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import pygtk
pygtk.require('2.0')
import gtk
import gobject
+import os.path
from random import uniform
from sprites import Sprite
from card import Card, load_image
-CARD_DEFS = ((1,3,-2,-3),(2,3,-3,-2),(2,3,-4,-4),
- (2,1,-1,-4),(3,4,-4,-3),(4,2,-1,-2),
- (1,1,-2,-4),(4,2,-3,-4),(1,3,-1,-2))
+CARD_DEFS = ((1, 3, -2, -3), (2, 3, -3, -2), (2, 3, -4, -4),
+ (2, 1, -1, -4), (3, 4, -4, -3), (4, 2, -1, -2),
+ (1, 1, -2, -4), (4, 2, -3, -4), (1, 3, -1, -2))
#
@@ -34,66 +34,72 @@ class Grid:
345 34x 345 34x
678 xxx xxx 67x
"""
- def __init__(self, tw):
- self.grid = [0,1,2,3,4,5,6,7,8]
+ def __init__(self, game):
+ self.grid = [0, 1, 2, 3, 4, 5, 6, 7, 8]
self.card_table = []
self.mask_table = []
# Stuff to keep around for the graphics
- self.w = int(tw.width)
- self.h = int(tw.height)
- self.d = int(tw.card_dim*tw.scale)
- self.s = tw.scale
+ self.w = int(game.width)
+ self.h = int(game.height)
+ self.d = int(game.card_dim * game.scale)
+ self.s = game.scale
# Initialize the cards
i = 0 # i is used as a label on the sprite
for c in CARD_DEFS:
x, y = self.i_to_xy(i)
- self.card_table.append(Card(tw,c,i,x,y))
+ self.card_table.append(Card(game, c, i, x, y))
i += 1
# Initialize the masks (We need up to 6 of each one.)
for i in range(4):
- name = "%s/mask%dh.svg" % (tw.path, i)
- bitmap = load_image(name, 120*tw.scale, 48*tw.scale)
+ bitmap = load_image(
+ os.path.join(game.path, 'mask%dh.svg' % (i)),
+ 120 * game.scale, 48 * game.scale)
for j in range(6):
x, y = self.mh_to_xy(j)
- self.mask_table.append(Sprite(tw.sprites, x, y, bitmap))
- name = "%s/mask%dv.svg" % (tw.path, i)
- bitmap = load_image(name, 48*tw.scale, 120*tw.scale)
+ self.mask_table.append(Sprite(game.sprites, x, y, bitmap))
+ bitmap = load_image(
+ os.path.join(game.path, 'mask%dv.svg' % (i)),
+ 48 * game.scale, 120 * game.scale)
for j in range(6):
x, y = self.mv_to_xy(j)
- self.mask_table.append(Sprite(tw.sprites, x, y, bitmap))
+ self.mask_table.append(Sprite(game.sprites, x, y, bitmap))
self.hide_masks()
# Utility functions
def i_to_xy(self, i):
- return int((self.w-(self.d*3))/2) + (i%3)*self.d,\
- int((self.h-(self.d*3))/2) + int(i/3)*self.d
+ return int((self.w - (self.d * 3)) / 2) + (i % 3) * self.d, \
+ int((self.h - (self.d * 3)) / 2) + int(i / 3) * self.d
def mh_to_xy(self, i):
- return int((self.w-(self.d*3))/2 + ((i%2)+1)*self.d - 60*self.s),\
- int((self.h-(self.d*3))/2 + (int(i/2)+.5)*self.d - 24*self.s)
+ return int((self.w - (self.d * 3)) / 2 + \
+ ((i % 2) + 1) * self.d - 60 * self.s), \
+ int((self.h - (self.d * 3)) / 2 + \
+ (int(i / 2) + .5) * self.d - 24 * self.s)
def mv_to_xy(self, i):
- return int((self.w-(self.d*3))/2 + ((i%3)+.5)*self.d - 24*self.s),\
- int((self.h-(self.d*3))/2 + (int(i/3)+1)*self.d - 60*self.s)
+ return int((self.w - (self.d * 3)) / 2 + \
+ ((i % 3) + .5) * self.d - 24 * self.s), \
+ int((self.h - (self.d * 3)) / 2 + \
+ (int(i / 3) + 1) * self.d - 60 * self.s)
def xy_to_i(self, x, y):
- return (x-int((self.w-(self.d*3))/2))/self.d +\
- ((y-int((self.h-(self.d*3))/2))/self.d)*3
+ return (x - int((self.w - (self.d * 3)) / 2)) / self.d + \
+ ((y - int((self.h - (self.d * 3)) / 2)) / self.d) * 3
def set_orientation(self, neworientation, draw_card=True):
for c in range(9):
self.card_table[c].set_orientation(neworientation[c], draw_card)
def randomize_orientation(self, draw_card=True):
- olist = [0,90,180,270]
+ olist = [0, 90, 180, 270]
for c in range(9):
- o = int(uniform(0,4))
+ o = int(uniform(0, 4))
self.card_table[c].set_orientation(olist[o], draw_card)
def set_grid(self, newgrid):
for i, c in enumerate(newgrid):
x, y = self.i_to_xy(i)
- self.card_table[c].spr.move((x,y))
+ self.card_table[c].spr.move((x, y))
self.grid[i] = c
def show_all(self):
@@ -109,79 +115,79 @@ class Grid:
i.hide()
# Reset everything to initial layout
- def reset3x3(self, tw):
+ def reset3x3(self, game):
self.show_all()
self.hide_masks()
- self.set_grid([0,1,2,3,4,5,6,7,8])
+ self.set_grid([0, 1, 2, 3, 4, 5, 6, 7, 8])
self.randomize_orientation()
self.hide_masks()
self.test3x3()
- # Two by two = ((7,5,0,3),(7,4,5,2),(1,3,5,8),(4,5,6,1))
- def reset2x2(self, tw):
+ # Two by two = ((7, 5, 0, 3), (7, 4, 5, 2), (1, 3, 5, 8), (4, 5, 6, 1))
+ def reset2x2(self, game):
self.show_all()
self.hide_masks()
self.randomize_orientation()
- r = int(uniform(0,4))
+ r = int(uniform(0, 4))
if r == 0:
- self.set_grid([7,5,1,0,3,2,4,6,8])
- self.hide_list([1,2,4,6,8])
+ self.set_grid([7, 5, 1, 0, 3, 2, 4, 6, 8])
+ self.hide_list([1, 2, 4, 6, 8])
elif r == 1:
- self.set_grid([7,4,1,5,2,3,0,6,8])
- self.hide_list([0,1,3,6,8])
+ self.set_grid([7, 4, 1, 5, 2, 3, 0, 6, 8])
+ self.hide_list([0, 1, 3, 6, 8])
elif r == 2:
- self.set_grid([1,3,2,5,8,4,6,7,0])
- self.hide_list([2,4,6,7,0])
+ self.set_grid([1, 3, 2, 5, 8, 4, 6, 7, 0])
+ self.hide_list([2, 4, 6, 7, 0])
else:
- self.set_grid([4,5,0,6,1,2,3,7,8])
- self.hide_list([0,2,3,7,8])
+ self.set_grid([4, 5, 0, 6, 1, 2, 3, 7, 8])
+ self.hide_list([0, 2, 3, 7, 8])
self.hide_masks()
self.test2x2()
- # Three by two = ((7,5,0,2,4,3),(5,6,1,4,3,8))
- def reset3x2(self, tw):
+ # Three by two = ((7, 5, 0, 2, 4, 3), (5, 6, 1, 4, 3, 8))
+ def reset3x2(self, game):
self.show_all()
self.hide_masks()
self.randomize_orientation()
- r = int(uniform(0,2))
+ r = int(uniform(0, 2))
if r == 0:
- self.set_grid([7,5,0,2,4,3,1,6,8])
- self.hide_list([1,6,8])
+ self.set_grid([7, 5, 0, 2, 4, 3, 1, 6, 8])
+ self.hide_list([1, 6, 8])
else:
- self.set_grid([5,6,1,4,3,8,0,2,7])
- self.hide_list([0,2,7])
+ self.set_grid([5, 6, 1, 4, 3, 8, 0, 2, 7])
+ self.hide_list([0, 2, 7])
self.hide_masks()
self.test3x2()
- # Two by three = ((5,2,4,6,1,7),(7,1,2,5,8,0))
+ # Two by three = ((5, 2, 4, 6, 1, 7), (7, 1, 2, 5, 8, 0))
# reset everything to initial layout
- def reset2x3(self, tw):
+ def reset2x3(self, game):
self.show_all()
self.hide_masks()
self.randomize_orientation()
- r = int(uniform(0,2))
+ r = int(uniform(0, 2))
if r == 0:
- self.set_grid([5,2,0,4,6,3,1,7,8])
- self.hide_list([0,3,8])
+ self.set_grid([5, 2, 0, 4, 6, 3, 1, 7, 8])
+ self.hide_list([0, 3, 8])
else:
- self.set_grid([7,1,3,2,5,4,8,0,6])
- self.hide_list([3,4,6])
+ self.set_grid([7, 1, 3, 2, 5, 4, 8, 0, 6])
+ self.hide_list([3, 4, 6])
self.hide_masks()
self.test2x3()
# swap card a and card b
# swap their entries in the grid and the position of their sprites
def swap(self, a, b):
- # swap grid elements and x,y positions of sprites
+ # swap grid elements and x, y positions of sprites
# print "swapping cards " + str(a) + " and " + str(b)
ai = self.grid.index(a)
bi = self.grid.index(b)
self.grid[bi] = a
self.grid[ai] = b
- ax,ay = self.card_table[a].spr.get_xy()
- bx,by = self.card_table[b].spr.get_xy()
- self.card_table[a].spr.move((bx,by))
- self.card_table[b].spr.move((ax,ay))
+ ax, ay = self.card_table[a].spr.get_xy()
+ bx, by = self.card_table[b].spr.get_xy()
+ self.card_table[a].spr.move((bx, by))
+ self.card_table[b].spr.move((ax, ay))
# print the grid
def print_grid(self):
@@ -192,124 +198,124 @@ class Grid:
# print the grid orientations
def print_orientations(self):
- print self.card_table[self.grid[0]].orientation,\
- self.card_table[self.grid[1]].orientation,\
+ print self.card_table[self.grid[0]].orientation, \
+ self.card_table[self.grid[1]].orientation, \
self.card_table[self.grid[2]].orientation
- print self.card_table[self.grid[3]].orientation,\
- self.card_table[self.grid[4]].orientation,\
+ print self.card_table[self.grid[3]].orientation, \
+ self.card_table[self.grid[4]].orientation, \
self.card_table[self.grid[5]].orientation
- print self.card_table[self.grid[6]].orientation,\
- self.card_table[self.grid[7]].orientation,\
+ print self.card_table[self.grid[6]].orientation, \
+ self.card_table[self.grid[7]].orientation, \
self.card_table[self.grid[8]].orientation
return
# Test all relevant borders, ignoring edges
# Highlight matches.
def test3x3(self):
- for m, i in enumerate([0,1,3,4,6,7]):
+ for m, i in enumerate([0, 1, 3, 4, 6, 7]):
offset = abs(self.card_table[self.grid[i]].east)
- offset = (offset-1)*12
+ offset = (offset - 1) * 12
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west == 0:
- self.mask_table[m+offset].set_layer(2000)
+ self.card_table[self.grid[i + 1]].west == 0:
+ self.mask_table[m + offset].set_layer(2000)
else:
- self.mask_table[m+offset].hide()
- for m, i in enumerate([0,1,2,3,4,5]):
+ self.mask_table[m + offset].hide()
+ for m, i in enumerate([0, 1, 2, 3, 4, 5]):
offset = abs(self.card_table[self.grid[i]].south)
- offset = (offset-1)*12+6
+ offset = (offset - 1) * 12 + 6
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north == 0:
- self.mask_table[m+offset].set_layer(2000)
+ self.card_table[self.grid[i + 3]].north == 0:
+ self.mask_table[m + offset].set_layer(2000)
else:
- self.mask_table[m+offset].hide()
- for i in (0,1,3,4,6,7):
+ self.mask_table[m + offset].hide()
+ for i in (0, 1, 3, 4, 6, 7):
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west != 0:
+ self.card_table[self.grid[i + 1]].west != 0:
return False
- for i in (0,1,2,3,4,5):
+ for i in (0, 1, 2, 3, 4, 5):
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north != 0:
+ self.card_table[self.grid[i + 3]].north != 0:
return False
return True
def test2x3(self):
- for m, i in enumerate([0,3,6]):
+ for m, i in enumerate([0, 3, 6]):
offset = abs(self.card_table[self.grid[i]].east)
- offset = (offset-1)*12
+ offset = (offset - 1) * 12
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west == 0:
- self.mask_table[m*2+offset].set_layer(2000)
+ self.card_table[self.grid[i + 1]].west == 0:
+ self.mask_table[m * 2 + offset].set_layer(2000)
else:
- self.mask_table[m*2+offset].hide()
- for m, i in enumerate([0,1,3,4]):
+ self.mask_table[m * 2 + offset].hide()
+ for m, i in enumerate([0, 1, 3, 4]):
offset = abs(self.card_table[self.grid[i]].south)
- offset = (offset-1)*12+6
+ offset = (offset - 1) * 12 + 6
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north == 0:
- self.mask_table[i+offset].set_layer(2000)
+ self.card_table[self.grid[i + 3]].north == 0:
+ self.mask_table[i + offset].set_layer(2000)
else:
- self.mask_table[i+offset].hide()
- for i in (0,3,6):
+ self.mask_table[i + offset].hide()
+ for i in (0, 3, 6):
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west != 0:
+ self.card_table[self.grid[i + 1]].west != 0:
return False
- for i in (0,1,3,4):
+ for i in (0, 1, 3, 4):
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north != 0:
+ self.card_table[self.grid[i + 3]].north != 0:
return False
return True
def test3x2(self):
- for m, i in enumerate([0,1,3,4]):
+ for m, i in enumerate([0, 1, 3, 4]):
offset = abs(self.card_table[self.grid[i]].east)
- offset = (offset-1)*12
+ offset = (offset - 1) * 12
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west == 0:
- self.mask_table[m+offset].set_layer(2000)
+ self.card_table[self.grid[i + 1]].west == 0:
+ self.mask_table[m + offset].set_layer(2000)
else:
- self.mask_table[m+offset].hide()
- for m, i in enumerate([0,1,2]):
+ self.mask_table[m + offset].hide()
+ for m, i in enumerate([0, 1, 2]):
offset = abs(self.card_table[self.grid[i]].south)
- offset = (offset-1)*12+6
+ offset = (offset - 1) * 12 + 6
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north == 0:
- self.mask_table[m+offset].set_layer(2000)
+ self.card_table[self.grid[i + 3]].north == 0:
+ self.mask_table[m + offset].set_layer(2000)
else:
- self.mask_table[m+offset].hide()
- for i in (0,1,3,4):
+ self.mask_table[m + offset].hide()
+ for i in (0, 1, 3, 4):
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west != 0:
+ self.card_table[self.grid[i + 1]].west != 0:
return False
- for i in (0,1,2):
+ for i in (0, 1, 2):
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north != 0:
+ self.card_table[self.grid[i + 3]].north != 0:
return False
return True
def test2x2(self):
- for m, i in enumerate([0,3]):
+ for m, i in enumerate([0, 3]):
offset = abs(self.card_table[self.grid[i]].east)
- offset = (offset-1)*12
+ offset = (offset - 1) * 12
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west == 0:
- self.mask_table[m*2+offset].set_layer(2000)
+ self.card_table[self.grid[i + 1]].west == 0:
+ self.mask_table[m * 2 + offset].set_layer(2000)
else:
- self.mask_table[m*2+offset].hide()
- for m, i in enumerate([0,1]):
+ self.mask_table[m * 2 + offset].hide()
+ for m, i in enumerate([0, 1]):
offset = abs(self.card_table[self.grid[i]].south)
- offset = (offset-1)*12+6
+ offset = (offset - 1) * 12 + 6
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north == 0:
- self.mask_table[m+offset].set_layer(2000)
+ self.card_table[self.grid[i + 3]].north == 0:
+ self.mask_table[m + offset].set_layer(2000)
else:
- self.mask_table[m+offset].hide()
- for i in (0,3):
+ self.mask_table[m + offset].hide()
+ for i in (0, 3):
if self.card_table[self.grid[i]].east + \
- self.card_table[self.grid[i+1]].west != 0:
+ self.card_table[self.grid[i + 1]].west != 0:
return False
- for i in (0,1):
+ for i in (0, 1):
if self.card_table[self.grid[i]].south + \
- self.card_table[self.grid[i+3]].north != 0:
+ self.card_table[self.grid[i + 3]].north != 0:
return False
return True
diff --git a/orientation.py b/orientation.py
index 8863003..f50e86f 100644
--- a/orientation.py
+++ b/orientation.py
@@ -1,14 +1,14 @@
-#Copyright (c) 2009,10 Walter Bender
+#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.
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
+
# There are a limited number of valid orientation combinations available,
# assuming each card has two neighboring patterns of the same gender, e.g.,
diff --git a/window.py b/window.py
index e72fe5a..45b7ec9 100644
--- a/window.py
+++ b/window.py
@@ -1,14 +1,13 @@
-#Copyright (c) 2009,10 Walter Bender
+#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.
+# You should have received a copy of the GNU General Public License
+# along with this library; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
import pygtk
pygtk.require('2.0')
@@ -21,146 +20,142 @@ try:
except:
GRID_CELL_SIZE = 0
-from grid import *
-from card import *
+from grid import Grid
from sprites import Sprites
from math import sqrt
CARD_DIM = 135
-class taWindow: pass
-
#
# handle launch from both within and without of Sugar environment
#
-def new_window(canvas, path, parent=None):
- tw = taWindow()
- tw.path = path
- tw.activity = parent
-
- # starting from command line
- # we have to do all the work that was done in CardSortActivity.py
- if parent is None:
- tw.sugar = False
- tw.canvas = canvas
-
- # starting from Sugar
- else:
- tw.sugar = True
- tw.canvas = canvas
- parent.show_all()
-
- tw.canvas.set_flags(gtk.CAN_FOCUS)
- tw.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK)
- tw.canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
- tw.canvas.connect("expose-event", _expose_cb, tw)
- tw.canvas.connect("button-press-event", _button_press_cb, tw)
- tw.canvas.connect("button-release-event", _button_release_cb, tw)
- tw.width = gtk.gdk.screen_width()
- tw.height = gtk.gdk.screen_height()-GRID_CELL_SIZE
- tw.card_dim = CARD_DIM
- tw.scale = 0.8 * tw.height/(tw.card_dim*3)
-
- # Initialize the sprite repository
- tw.sprites = Sprites(tw.canvas)
-
- # Initialize the grid
- tw.grid = Grid(tw)
-
- # Start solving the puzzle
- tw.press = -1
- tw.release = -1
- tw.start_drag = [0,0]
-
- return tw
-
-#
-# Button press
-#
-def _button_press_cb(win, event, tw):
- win.grab_focus()
- x, y = map(int, event.get_coords())
- tw.start_drag = [x,y]
- tw.grid.hide_masks()
- spr = tw.sprites.find_sprite((x,y))
- if spr is None:
- tw.press = -1
- tw.release = -1
- return True
- # take note of card under button press
- tw.press = int(spr.labels[0])
- return True
-
-#
-# Button release
-#
-def _button_release_cb(win, event, tw):
- win.grab_focus()
- tw.grid.hide_masks()
- x, y = map(int, event.get_coords())
- spr = tw.sprites.find_sprite((x,y))
- if spr is None:
- tw.press = -1
- tw.release = -1
+class Game():
+
+ def __init__(self, canvas, path, parent=None):
+ self.activity = parent
+ self.path = path
+
+ # starting from command line
+ # we have to do all the work that was done in CardSortActivity.py
+ if parent is None:
+ self.sugar = False
+ self.canvas = canvas
+
+ # starting from Sugar
+ else:
+ self.sugar = True
+ self.canvas = canvas
+ parent.show_all()
+
+ self.canvas.set_flags(gtk.CAN_FOCUS)
+ self.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK)
+ self.canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
+ self.canvas.connect("expose-event", self._expose_cb)
+ self.canvas.connect("button-press-event", self._button_press_cb)
+ self.canvas.connect("button-release-event", self._button_release_cb)
+ self.width = gtk.gdk.screen_width()
+ self.height = gtk.gdk.screen_height()-GRID_CELL_SIZE
+ self.card_dim = CARD_DIM
+ self.scale = 0.8 * self.height / (self.card_dim * 3)
+
+ # Initialize the sprite repository
+ self.sprites = Sprites(self.canvas)
+
+ # Initialize the grid
+ self.grid = Grid(self)
+
+ # Start solving the puzzle
+ self.press = -1
+ self.release = -1
+ self.start_drag = [0, 0]
+
+ #
+ # Button press
+ #
+ def _button_press_cb(self, win, event):
+ win.grab_focus()
+ x, y = map(int, event.get_coords())
+ self.start_drag = [x, y]
+ self.grid.hide_masks()
+ spr = self.sprites.find_sprite((x,y))
+ if spr is None:
+ self.press = -1
+ self.release = -1
+ return True
+ # take note of card under button press
+ self.press = int(spr.labels[0])
return True
- # take note of card under button release
- tw.release = int(spr.labels[0])
- # if the same card (click) then rotate
- if tw.press == tw.release:
- # check to see if it was an aborted move
- if distance(tw.start_drag,[x,y]) < 20:
- tw.grid.card_table[tw.press].rotate_ccw()
- # tw.grid.card_table[tw.press].print_card()
- # if different card (drag) then swap
- else:
- tw.grid.swap(tw.press,tw.release)
- # tw.grid.print_grid()
- tw.press = -1
- tw.release = -1
- if tw.test() == True:
- if tw.sugar is True:
- tw.activity.results_label.set_text(_("You solved the puzzle."))
- tw.activity.results_label.show()
+
+ #
+ # Button release
+ #
+ def _button_release_cb(self, win, event):
+ win.grab_focus()
+ self.grid.hide_masks()
+ x, y = map(int, event.get_coords())
+ spr = self.sprites.find_sprite((x, y))
+ if spr is None:
+ self.press = -1
+ self.release = -1
+ return True
+ # take note of card under button release
+ self.release = int(spr.labels[0])
+ # if the same card (click) then rotate
+ if self.press == self.release:
+ # check to see if it was an aborted move
+ if self.distance(self.start_drag, [x, y]) < 20:
+ self.grid.card_table[self.press].rotate_ccw()
+ # self.grid.card_table[self.press].print_card()
+ # if different card (drag) then swap
else:
- tw.win.set_title(_("CardSort") + ": " + \
- _("You solved the puzzle."))
- else:
- if tw.sugar is True:
- tw.activity.results_label.set_text(_("Keep trying."))
- tw.activity.results_label.show()
+ self.grid.swap(self.press,self.release)
+ # self.grid.print_grid()
+ self.press = -1
+ self.release = -1
+ if self.test() == True:
+ if self.sugar is True:
+ self.activity.results_label.set_text(
+ _("You solved the puzzle."))
+ self.activity.results_label.show()
+ else:
+ self.win.set_title(_("CardSort") + ": " + \
+ _("You solved the puzzle."))
else:
- tw.win.set_title(_("CardSort") + ": " + _("Keep trying."))
- return True
-
-#
-# Measure length of drag between button press and button release
-#
-def distance(start,stop):
- dx = start[0]-stop[0]
- dy = start[1]-stop[1]
- return sqrt(dx*dx+dy*dy)
-
-
-#
-# Repaint
-#
-def _expose_cb(tw, win, event):
- ''' Callback to handle window expose events '''
- tw.do_expose_event(event)
- return True
-
-def do_expose_event(tw, event):
- ''' Handle the expose-event by drawing '''
- # Restrict Cairo to the exposed area
- cr = tw.canvas.window.cairo_create()
- cr.rectangle(event.area.x, event.area.y,
- event.area.width, event.area.height)
- cr.clip()
- # Refresh sprite list
- tw.sprites.redraw_sprites(cr=cr)
-
-#
-# callbacks
-#
-def _destroy_cb(win, event, tw):
- gtk.main_quit()
+ if self.sugar is True:
+ self.activity.results_label.set_text(_("Keep trying."))
+ self.activity.results_label.show()
+ else:
+ self.win.set_title(_("CardSort") + ": " + _("Keep trying."))
+ return True
+
+ #
+ # Measure length of drag between button press and button release
+ #
+ def distance(self, start, stop):
+ dx = start[0] - stop[0]
+ dy = start[1] - stop[1]
+ return sqrt(dx * dx + dy * dy)
+
+ #
+ # Repaint
+ #
+ def _expose_cb(self, win, event):
+ ''' Callback to handle window expose events '''
+ self.do_expose_event(event)
+ return True
+
+ def do_expose_event(self, event):
+ ''' Handle the expose-event by drawing '''
+ # Restrict Cairo to the exposed area
+ cr = self.canvas.window.cairo_create()
+ cr.rectangle(event.area.x, event.area.y,
+ event.area.width, event.area.height)
+ cr.clip()
+ # Refresh sprite list
+ self.sprites.redraw_sprites(cr=cr)
+
+ #
+ # callbacks
+ #
+ def _destroy_cb(self, win, event):
+ gtk.main_quit()