Python Forum
[Pyglet] Making tetrominos
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pyglet] Making tetrominos
#5
But where do I store the data? In the shape module?

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.
Reply


Messages In This Thread
Making tetrominos - by ragoo - Sep-15-2023, 11:00 AM
RE: Making tetrominos - by deanhystad - Sep-15-2023, 03:34 PM
RE: Making tetrominos - by ragoo - Sep-15-2023, 06:28 PM
RE: Making tetrominos - by deanhystad - Sep-15-2023, 11:11 PM
RE: Making tetrominos - by ragoo - Sep-16-2023, 01:09 PM
RE: Making tetrominos - by deanhystad - Sep-17-2023, 02:38 AM
RE: Making tetrominos - by ragoo - Sep-18-2023, 10:09 AM
RE: Making tetrominos - by deanhystad - Sep-18-2023, 05:10 PM
RE: Making tetrominos - by ragoo - Sep-19-2023, 11:49 AM
RE: Making tetrominos - by deanhystad - Sep-19-2023, 06:20 PM
RE: Making tetrominos - by ragoo - Sep-26-2023, 07:13 PM
RE: Making tetrominos - by Benixon - Dec-07-2023, 02:44 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020