Exemple #1
0
    def test_pos_equal(self):
        pos_a = Pos(100, 200)
        pos_b = Pos(100, 200)

        self.assertTrue(pos_a == pos_b)

        with self.assertRaises(TypeError):
            _ = pos_a == {}
Exemple #2
0
    def test_pos_init(self):
        pos = Pos(1, 2)
        self.assertEqual(pos.x, 1)
        self.assertEqual(pos.y, 2)

        pos = Pos(x=42, y=100)
        self.assertEqual(pos.x, 42)
        self.assertEqual(pos.y, 100)
Exemple #3
0
    def test_rotate_brick_to_vertical_obstacle(self):
        self.board.spawn_brick()

        origin = self.board.brick.blocks
        colors = [o.color for o in origin]

        self.board.rotate_brick()
        brick = self.board.brick

        self.assertFalse(brick.is_horizontal())
        self.assertPosEqual(brick.blocks[0].pos, origin[0].pos + Pos(0, 1))
        self.assertPosEqual(brick.blocks[1].pos, origin[1].pos + Pos(-1, 0))

        for i in range(0, 2):
            self.assertEqual(brick.blocks[i].color, colors[i])
Exemple #4
0
    def test_pos_equal_tuple(self):
        pos = Pos(100, 200)
        pos_tuple = (100, 200)
        pos_list = [100, 200]

        self.assertTrue(pos == pos_tuple)
        self.assertTrue(pos == pos_list)
Exemple #5
0
    def test_pos_not_equal_tuple(self):
        pos = Pos(100, 200)
        pos_tuple = (1, 2)
        pos_list = [3, 4]

        self.assertFalse(pos == pos_tuple)
        self.assertFalse(pos == pos_list)
Exemple #6
0
    def test_pos_add(self):
        pos_a = Pos(100, 200)
        pos_b = Pos(5, 10)

        pos = pos_a + pos_b
        self.assertEqual(pos.x, 105)
        self.assertEqual(pos.y, 210)

        pos = pos_b + pos_a
        self.assertEqual(pos.x, 105)
        self.assertEqual(pos.y, 210)

        pos = pos_a
        pos += pos_b
        self.assertEqual(pos.x, 105)
        self.assertEqual(pos.y, 210)
Exemple #7
0
    def test_spawn_brick_position(self):
        self.board.spawn_brick()

        self.assertEqual(len(self.board.brick.blocks), 2)

        block_a, block_b = self.board.brick.blocks

        self.assertPosEqual(block_a.pos, board.SPAWN_POS)
        self.assertPosEqual(block_b.pos, board.SPAWN_POS + Pos(1, 0))
Exemple #8
0
    def test_move_brick_down(self):
        self.board.spawn_brick()

        brick = self.board.brick
        blocks = copy.deepcopy(brick.blocks)

        self.board.move_brick('down')

        for i in range(0, 2):
            self.assertPosEqual(brick.blocks[i].pos, blocks[i].pos + Pos(0, 1))
            self.assertEqual(brick.blocks[i].color, blocks[i].color)
Exemple #9
0
    def test_rotate_brick_to_horizontal(self):
        self.board.spawn_brick()
        # Ensure there is enough space
        self.board.move_brick('down')
        # Set up origin vertical position
        self.board.rotate_brick()

        origin = self.board.brick.blocks
        colors = [o.color for o in origin]
        colors.reverse()

        self.board.rotate_brick()
        brick = self.board.brick

        self.assertTrue(brick.is_horizontal())
        self.assertPosEqual(brick.blocks[0].pos, origin[1].pos + Pos(0, 1))
        self.assertPosEqual(brick.blocks[1].pos, origin[0].pos + Pos(1, 0))

        for i in range(0, 2):
            self.assertEqual(brick.blocks[i].color, colors[i])
Exemple #10
0
    def test_pos_tuple(self):
        pos_a = Pos(100, 200)
        pos_tuple = (5, 10)

        pos = pos_a + pos_tuple
        self.assertEqual(pos.x, 105)
        self.assertEqual(pos.y, 210)

        pos = pos_a
        pos += pos_tuple
        self.assertEqual(pos.x, 105)
        self.assertEqual(pos.y, 210)
Exemple #11
0
    def rotate_brick(self):
        transform = (
            # vertical to horizontal
            ((Pos(1, 0), Pos(0, 1)),
             (Pos(0, 0), Pos(-1, 1))),
            # horizontal to vertical
            ((Pos(0, 0), Pos(-1, -1)),
             (Pos(0, 1), Pos(-1, 0)))
        )

        section = int(self.brick.is_horizontal())

        origin = self.brick.blocks

        for offset in transform[section]:
            try:
                i = not section
                j = not i
                new_blocks = (
                    self.block(origin[i].x + offset[i].x, origin[i].y + offset[i].y),
                    self.block(origin[j].x + offset[j].x, origin[j].y + offset[j].y),
                )

                # Don't overwrite blocks
                for b in new_blocks:
                    if not b.is_clear() and b.pos != origin[0].pos and b.pos != origin[1].pos:
                        raise InvalidOperation("New block is occupied")

                if self.brick.is_horizontal():
                    # standard colors
                    colors = (origin[0].color, origin[1].color)
                else:
                    # swap colors
                    colors = (origin[1].color, origin[0].color)

                for k in range(0, 2):
                    origin[k].clear()
                    new_blocks[k].set_color(colors[k])

                self.brick.set_blocks(*new_blocks)
            except (OutOfBoard, InvalidOperation):
                continue
            else:
                break
Exemple #12
0
    def test_rotate_brick_to_vertical(self):
        self.board.spawn_brick()
        # Ensure there is enough space
        self.board.move_brick('down')

        origin = self.board.brick.blocks
        colors = [o.color for o in origin]

        self.board.rotate_brick()
        brick = self.board.brick

        self.assertFalse(brick.is_horizontal())
        self.assertPosEqual(brick.blocks[0].pos, origin[0].pos)
        self.assertPosEqual(brick.blocks[1].pos, origin[1].pos + Pos(-1, -1))

        for i in range(0, 2):
            self.assertEqual(brick.blocks[i].color, colors[i])
Exemple #13
0
    def test_block(self):
        block = self.board.block(0, 0)

        self.assertIsNotNone(block)
        self.assertPosEqual(block.pos, Pos(0, 0))

        # Should return the same instance
        block_2 = self.board.block(0, 0)
        self.assertIs(block, block_2)

        with self.assertRaises(BottomReached):
            _ = self.board.block(0, board.HEIGHT + 1)

        with self.assertRaises(OutOfBoard):
            _ = self.board.block(999, 0)

        with self.assertRaises(OutOfBoard):
            _ = self.board.block(-1, 0)
Exemple #14
0
    def test_rotate_brick_to_horizontal_obstacle(self):
        self.board.spawn_brick()
        # Ensure there is enough space
        self.board.move_brick('down')
        # Set up origin vertical position
        self.board.rotate_brick()

        # Place obstacle
        b = self.board.brick.blocks[0]
        self.board.block(b.x + 1, b.y).set_color(Color.RED)

        origin = self.board.brick.blocks
        colors = [o.color for o in origin]
        colors.reverse()

        self.board.rotate_brick()
        brick = self.board.brick

        self.assertTrue(brick.is_horizontal())
        self.assertPosEqual(brick.blocks[0].pos, origin[1].pos + Pos(-1, 1))
        self.assertPosEqual(brick.blocks[1].pos, origin[0].pos)

        for i in range(0, 2):
            self.assertEqual(brick.blocks[i].color, colors[i])
Exemple #15
0
import drpython.block
import drpython.brick
from drpython.exceptions import *
from drpython.block import Block
from drpython.block import Color
from drpython.brick import Brick
from drpython.colors import *
from drpython.utils import Pos

WIDTH = 8
HEIGHT = 16

WIDTH_PIXELS = WIDTH * drpython.block.WIDTH
HEIGHT_PIXELS = HEIGHT * drpython.block.HEIGHT

SPAWN_POS = Pos(x=3, y=0)


class Board(object):

    def __init__(self):
        self._display = pygame.Surface((WIDTH_PIXELS, HEIGHT_PIXELS))
        # Init board with clear blocks
        self._board = []
        for h in range(0, HEIGHT):
            self._board.append([])
            for w in range(0, WIDTH):
                self._board[h].append(Block(w, h))

        self._brick = None
Exemple #16
0
 def pos(self):
     return Pos(self.x, self.y)
Exemple #17
0
    def test_pos_not_equal(self):
        pos_a = Pos(100, 200)
        pos_b = Pos(1, 2)

        self.assertFalse(pos_a == pos_b)