Ejemplo n.º 1
0
    def draw(self):
        self.destroy()

        for x in range(self._game_map.size.width):
            for y in range(self._game_map.size.height):
                top_of_cell = self._game_map.block(Position(x, y)).top()
                bv = block_view_factory.from_block( top_of_cell, self._canvas)
                self._block_views[Position(x, y)] = bv
                bv.draw(Position(x, y))
Ejemplo n.º 2
0
def test_two_ducks_against_melting_ice():
    # Given
    map, manager = loadMap("test_multi_ducks_with_ice.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    p1: 'Player'= map.block(Position(4, 0)).top()
    assert p1.is_alive
    p2: 'Player' = map.block(Position(5, 0)).top()
    assert p2.is_alive
Ejemplo n.º 3
0
def test_boulder():
    # Given
    map, manager = loadMap("test_boulder.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    player: Player = map.block(Position(3, 0)).top()
    assert player.is_alive
    boulder: Boulder = map.block(Position(4, 0)).top()
    assert type(boulder) == Boulder
Ejemplo n.º 4
0
def test_spike_movedown():
    # Given
    map, manager = loadMap("test_spike.json")
    p = _find_one_player(map)
    map.move(pos_from=Position(0, 5), pos_to=Position(0, 1), what=p)

    # When
    manager.execute_turn(Direction.DOWN)

    # Then
    p = _find_one_player(map)
    assert p.position == Position(0, 1)
    assert p.is_alive
Ejemplo n.º 5
0
def test_stone_movearound():
    # Given
    map, manager = loadMap("test_stone_4x4.json")

    # When-Then
    manager.execute_turn(Direction.RIGHT)
    assert _find_one_player(map).position == Position(2, 1)
    manager.execute_turn(Direction.DOWN)
    assert _find_one_player(map).position == Position(2, 2)
    manager.execute_turn(Direction.LEFT)
    assert _find_one_player(map).position == Position(1, 2)
    manager.execute_turn(Direction.UP)
    assert _find_one_player(map).position == Position(1, 1)
Ejemplo n.º 6
0
def test_duck_boulder_duck():
    # Given
    map, manager = loadMap("test_duck_boulder_duck.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    p1: 'Player'= map.block(Position(3, 0)).top()
    assert p1.is_alive
    p2: 'Player' = map.block(Position(5, 0)).top()
    assert p2.is_alive
    assert type(map.block(Position(4, 0)).top()) == Boulder
Ejemplo n.º 7
0
def test_spike_moveup_directhit():
    # Given
    map, manager = loadMap("test_spike.json")
    p = _find_one_player(map)
    map.move(pos_from=Position(0, 5), pos_to=Position(0, 3), what=p)


    # When
    manager.execute_turn(Direction.UP)
    p = _find_one_player(map)

    # Then
    assert p.position == Position(0, 3)
    assert not p.is_alive
Ejemplo n.º 8
0
def test_move_one_duck():
    # Given
    map, manager = loadMap("test_move_one_duck.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    player: Player = map.block(Position(2, 0)).top()
    assert player.is_alive

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    player: Player = map.block(Position(3, 0)).top()
    assert player.is_alive
Ejemplo n.º 9
0
def test_stone_moveleft():
    # Given
    map, manager = loadMap("test_stone.json")

    # When
    manager.execute_turn(Direction.LEFT)

    # Then
    p = _find_one_player(map)
    assert p.position == Position(3, 0)

    # When
    manager.execute_turn(Direction.LEFT)

    # Then
    p = _find_one_player(map)
    assert p.position == Position(3, 0)
    assert p.is_alive
Ejemplo n.º 10
0
def test_empty_moveright():
    # Given
    map, manager = loadMap("test_empty.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    p = _find_one_player(map)
    assert not p.is_alive
    assert p.position == Position(4, 0)
Ejemplo n.º 11
0
def test_spike_moveup():
    # Given
    map, manager = loadMap("test_spike.json")

    # When
    manager.execute_turn(Direction.UP)

    # Then
    p = _find_one_player(map)
    assert p.position == Position(0, 3)
    assert not p.is_alive
Ejemplo n.º 12
0
def test_two_ducks_move_in_row():
    # Given
    map, manager = loadMap("test_multi_ducks.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    p1: 'Player' = map.block(Position(4, 0)).top()
    assert p1.is_alive
    p2: 'Player' = map.block(Position(5, 0)).top()
    assert p2.is_alive

    # When
    manager.execute_turn(Direction.LEFT)

    # Then
    p1: 'Player' = map.block(Position(1, 0)).top()
    assert p1.is_alive
    p2: 'Player' = map.block(Position(2, 0)).top()
    assert p2.is_alive
Ejemplo n.º 13
0
def test_duckpool():
    # Given
    map, manager = loadMap("test_duckpool.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    assert _find_one_player(map) is None
    pool: DuckPoolBlock = map.block(Position(3, 0)).top()
    assert pool.capacity == -1
    assert pool.free_space == -1
    assert len(pool._blocks_in_pool) == 1
Ejemplo n.º 14
0
def test_melting_ice():
    # Given
    map, manager = loadMap("test_melting_ice.json")

    # When-Then
    manager.execute_turn(Direction.DOWN)
    p = _find_one_player(map)
    assert p.position == Position(0, 4)
    assert p.is_alive

    # When-Then
    manager.execute_turn(Direction.UP)
    manager.execute_turn(Direction.DOWN)
    p = _find_one_player(map)
    assert p.position == Position(0, 4)
    assert p.is_alive

    # When-Then
    manager.execute_turn(Direction.UP)
    manager.execute_turn(Direction.DOWN)
    p = _find_one_player(map)
    # Ice is now broken
    assert p.position == Position(0, 5)
    assert p.is_alive
Ejemplo n.º 15
0
def test_multiple_frogs():
    # Given
    map, manager = loadMap("test_multiple_frogs.json")

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    p: 'Player' = map.block(Position(2, 0)).top()
    assert p.is_alive
    p = map.block(Position(3, 0)).top()
    assert p.is_alive
    p = map.block(Position(4, 0)).top()
    assert p.is_alive

    # When
    manager.execute_turn(Direction.RIGHT)

    # Then
    p: 'Player' = map.block(Position(3, 0)).top()
    assert p.is_alive
    p = map.block(Position(4, 0)).top()
    assert p.is_alive
    p = map.block(Position(5, 0)).top()
    assert p.is_alive

    # When
    manager.execute_turn(Direction.LEFT)

    # Then
    p: 'Player' = map.block(Position(1, 0)).top()
    assert p.is_alive
    p = map.block(Position(3, 0)).top()
    assert p.is_alive
    p = map.block(Position(4, 0)).top()
    assert p.is_alive
Ejemplo n.º 16
0
    def mouse_moved(self, mouse_e):
        new_pos = Position(int(mouse_e.x / BLOCK_SIZE + 0.5),
                           int(mouse_e.y / BLOCK_SIZE + 0.5))
        if new_pos == self._current_pos:
            return
        self._current_pos = new_pos

        # Move border
        self._canvas.delete(self._border_id)
        self._create_border()

        # Move resizing rect
        if self._moving_resizing_rect_id != 0:
            self._canvas.delete(self._moving_resizing_rect_id)
        self._moving_resizing_rect_id = self._create_resizing_rect(new_pos)

        # Move resizing label
        if self._size_label_id != 0:
            self._canvas.delete(self._size_label_id)
        self._create_moving_label()
Ejemplo n.º 17
0
    def _shift_clicked(self, mouse_event):
        if self._in_exception(mouse_event.x, mouse_event.y):
            return

        # If no selections were made yet, shift click works pretty much like simple click
        if self._head is None:
            self._clicked(mouse_event)
            return

        pos = BlockSelectionController._mouse_pos_to_game_pos(
            mouse_event.x, mouse_event.y)
        for x in range(min(pos.x, self._head.x), max(pos.x, self._head.x) + 1):
            for y in range(min(pos.y, self._head.y),
                           max(pos.y, self._head.y) + 1):
                p = Position(x, y)
                if self._game_pos_in_exception(p, shift_candidate=True):
                    continue
                self._select_game_pos(p)
        self._head = pos
        self.on_changed()
Ejemplo n.º 18
0
    def __init__(self, canvas: Canvas, map_view: MapView, selection_controller,
                 callback: Callable[[Size], bool]):
        self._canvas = canvas
        self._map_view = map_view
        self._selection_controller = selection_controller
        self._callback = callback

        self._resizing_rect_id: int = 0
        """The Id of the Resizing Rectangle which is at the bottom right corner of the currently available map.
           Resizing can be triggered via clicking on the Resizing Rectangle"""

        self._moving_resizing_rect_id: int = 0
        """The Id of the proposed new Resizing Rectangle during resizing event."""

        self._current_pos = Position(self._map_view.size.width,
                                     self._map_view.size.height)
        """Holds the current position (in logical unit) during resizing event"""

        self._border_id: int = 0
        """Holds the Id of the currently displayed resizing border. The border visualise the proposed map size."""

        self._size_label_id: int = 0
        """The Id of a label which represents the proposed map size with numbers."""
Ejemplo n.º 19
0
 def refresh(self):
     if self._resizing_rect_id:
         self._canvas.delete(self._resizing_rect_id)
     self._current_pos = Position(self._map_view.size.width,
                                  self._map_view.size.height)
     self.draw_resizing_rect()
Ejemplo n.º 20
0
 def blocks(self) -> Dict[Position, GameStack]:
     res = {}
     for x, r in enumerate(self._blocks):
         for y, c in enumerate(r):
             res[Position(x, y)] = c
     return res
Ejemplo n.º 21
0
import logging

from game.blocks.block import AbstractBlock
from game.move_info import MoveInfo
from game.utils.MoveReports import MovableMoveReport
from game.utils.direction import Direction
from game.utils.move_verdict import MoveVerdict, MoveVerdictEnum
from game.utils.position import Position

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    from game.gamemap import GameMap

dir_func_map = {
    Direction.DOWN: lambda pos: pos + Position(0, 1),
    Direction.UP: lambda pos: pos + Position(0, -1),
    Direction.RIGHT: lambda pos: pos + Position(1, 0),
    Direction.LEFT: lambda pos: pos + Position(-1, 0),
}


class Movable(AbstractBlock):
    def __init__(self):
        super().__init__()
        self._position = None
        self._game_map = None

    def initialize(self, pos: Position, game_map: 'GameMap'):
        self._position = pos
        self._game_map = game_map
Ejemplo n.º 22
0
 def _mouse_pos_to_game_pos(x, y) -> Position:
     return Position(x // BLOCK_SIZE, y // BLOCK_SIZE)