Sep-16-2023, 01:09 PM
But where do I store the data? In the shape module?
What if I use the simple factory pattern instead?
What if I use the simple factory pattern instead?
from pyglet.math import Vec2 from mino import Mino standard_wall_kick_data = { 0: { 3: ((-1, 0), (-1, -1), (0, 2), (-1, 2)), 1: ((1, 0), (1, -1), (0, 2), (1, 2))}, 1: { 0: ((-1, 0), (-1, 1), (0, -2), (-1, -2)), 2: ((-1, 0), (-1, 1), (0, -2), (-1, -2))}, 2: { 1: ((1, 0), (1, -1), (0, 2), (1, 2)), 3: ((-1, 0), (-1, -1), (0, 2), (-1, 2))}, 3: { 2: ((1, 0), (1, 1), (0, -2), (1, -2)), 0: ((1, 0), (1, 1), (0, -2), (1, -2))} } i_wall_kick_data = { 0: { 3: ((1, 0), (-2, 0), (1, -2), (-2, 1)), 1: ((2, 0), (-1, 0), (2, 1), (-1, -2))}, 1: { 0: ((-2, 0), (1, 0), (-2, -1), (1, 2)), 2: ((1, 0), (-2, 0), (1, -2), (-2, 1))}, 2: { 1: ((-1, 0), (2, 0), (-1, 2), (2, -1)), 3: ((-2, 0), (1, 0), (-2, -1), (1, 2))}, 3: { 2: ((2, 0), (-1, 0), (2, 1), (-1, -2)), 0: ((-1, 0), (2, 0), (-1, 2), (2, -1))} } class Tetromino: def __init__(self, shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch): self.minos = [Mino(x + dx, y + dy, cell_size, color, batch) for (x, y) in shape_data] self.pivot = pivot + Vec2(dx, dy) self.wall_kick_data = wall_kick_data self.current_rotation = 0 self.previous_rotation = 0 def move_by(self, vector): self.pivot += vector for mino in self.minos: mino.move_by(vector) def rotate(self, angle): self.previous_rotation = self.current_rotation if angle > 0: self.current_rotation = (self.current_rotation - 1) % 4 else: self.current_rotation = (self.current_rotation + 1) % 4 for mino in self.minos: mino.rotate(self.pivot, angle) def update(self): for mino in self.minos: mino.update() class I(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-2, -2), (-1, -2), (0, -2), (1, -2)) pivot = Vec2(-0.5, -2.5) wall_kick_data = i_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class J(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-2, -2), (-1, -2), (0, -2), (-2, -1)) pivot = Vec2(-1, -2) wall_kick_data = standard_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class L(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-2, -2), (-1, -2), (0, -2), (0, -1)) pivot = Vec2(-1, -2) wall_kick_data = standard_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class O(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-1, -2), (0, -2), (-1, -1), (0, -1)) pivot = Vec2(-0.5, -1.5) wall_kick_data = standard_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class S(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-2, -2), (-1, -2), (-1, -1), (0, -1)) pivot = Vec2(-1, -2) wall_kick_data = standard_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class T(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-2, -2), (-1, -2), (0, -2), (-1, -1)) pivot = Vec2(-1, -2) wall_kick_data = standard_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class Z(Tetromino): def __init__(self, dx, dy, cell_size, color, batch): shape_data = ((-1, -2), (0, -2), (-2, -1), (-1, -1)) pivot = Vec2(-1, -2) wall_kick_data = standard_wall_kick_data super().__init__(shape_data, pivot, wall_kick_data, dx, dy, cell_size, color, batch) class TetrominoFactory: def make_tetromino(self, shape, dx, dy, cell_size, color, batch): args = (dx, dy, cell_size, color, batch) match shape: case 'I': return I(*args) case 'J': return J(*args) case 'L': return L(*args) case 'O': return O(*args) case 'S': return S(*args) case 'T': return T(*args) case 'Z': return Z(*args) case _: raise ValueError(shape)This way I can store the raw data in the Tetromino subclasses. What I don't like is all those arguments I need to build the minos collection that get passed along the calls. But I need them since each Mino is technically a pyglet rectangle.