《付録》TetrisWorld.py

# -*- coding: utf-8 -*-
#===============================================================================
#    Copyright (C) 2000-2008, 小泉ひよ子とタマゴ倶楽部
#
# Change History: Games
#    1988/05, Smalltalk
#    2004/09, Java
#    2005/02, C#
#    2005/03, Jython
# Change History: WPF examples
#    2008/01/25, IronPython 1.1.1 (download)
#    2008/08/22, IronPython 1.1.2 (download)
#    2008/03/16, ver.2.0, WPF
#    2008/00/00, ver.2.1, IronPython 1.1.2 
#===============================================================================

from hexagon import HexStone
from TetrisContext import *

## --------------------               
class Tray:
    M = 2; M2 = M*2;
    
    def __init__(self):
        self.tiles     = self._tiles()
        self.leftEdge  = self._leftEdge()
        self.rightEdge = self._rightEdge()
        self.bottomEnd = self._bottomEnd()

    def _tiles(self):
        m1, m2 = self.M, self.M2;
        
        s = {}
        for x, y in [(e*m1+2, 0) for e in range(5)]:
            for _ in range(5):
                s[x, y] = Tile(x, y); y += m2
        for x, y in [(e*m1+3, 2) for e in range(4)]:
            for _ in range(4):
                s[x, y] = Tile(x, y); y += m2
        return s

    def _leftEdge(self):
        return self._edge( 0)

    def _rightEdge(self):
        return self._edge(12)

    def _edge(self, x):
        m = self.M;
        
        s = {}
        for x, y in [(x-1, e*m) for e in range(9)]:
            for _ in range(3):
                s[x, y] = Edge(x, y); x += 1
        return s

    def _bottomEnd(self):
        s = {}
        for x, y in [(e+2, 18) for e in range(9)]:
            s[x, y] = Bottom(x, y)
        return s

## --------------------               
class Ostone(object):
    def __init__(self, x, y, strokeColor=None, fillColor=None):
        self.x = x
        self.y = y
        self.strokeColor = strokeColor
        self.fillColor   = fillColor
        self.shape = self._shape()

    def _shape(self):
        M = 2;
        Ox = HexStone._dx; W = HexStone._width ;
        Oy = HexStone._dy; H = HexStone._height;
        X = Ox + W*self.x;
        Y = Oy + H*self.y;

        points = XPointCollection([XPoint(X + dx*M, Y + dy*M)
            for dx, dy in HexStone(X, Y, False).vertices])
        return XPolygon(
            Stroke=self.strokeColor,
            Fill=self.fillColor,
            Points=points,
            )

class Tile(Ostone):
    def __init__(self, x, y):
        super(self.__class__, self).__init__(x, y,
            strokeColor=Gray,
            )

class Edge(Ostone):
    def __init__(self, x, y):
        super(self.__class__, self).__init__(x, y,
            strokeColor=LightGray,
            )

class Bottom(Ostone):
    def __init__(self, x, y):
        super(self.__class__, self).__init__(x, y,
            fillColor=LightGray,
            )

## --------------------