level.py

This module provides simple and powerfull tools for creating levels.

You can use basic implementation Level or make your custom tool by inheriting from BaseLevel.

Example:

>>> level = Level.from_string(4, 3, 'X..X .XX. X.X.', '.', 'X')
>>> level.width
4
>>> level.height
3
>>> print(*level)
True False False True False True True False True False True False
class level.BaseLevel

Base class for all Level objects.

You can use it to make custom level object like in the example below:

 1class MyCustom_AlwaysZeroLevel(BaseLevel):
 2
 3        def __init__(self, width: int, height: int):
 4                self._width = width
 5                self._height = height
 6
 7        @property
 8        def width(self) -> int:
 9                return self._width
10
11        @property
12        def height(self) -> int:
13                return self._height
14
15        def __iter__(self) -> Iterator[bool]:
16                for row in range(self.height):
17                        for col in range(self.width):
18                                yield False
19
20level = MyCustom_AlwaysZeroLevel(5, 6)
21assert level.width == 5
22assert level.height == 6
23assert not any(level), 'AlwaysZeroLevel is not always zero! Aaaaaaaaaa!'

Note

I use property for width and height

abstract __iter__() Iterator[bool]

Line by line representation of the level.

Order of cells: (4 x 4 example)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Example: (True and False in replaced to 1 and 0)

0

1

1

1

1

0

1

0

0

1

1

0

1

0

0

1

Iterator:

0111

1010

0110

1001

Note

The length of the iterator should be greater or equal than width * height

abstract property height: int

Level height

abstract property width: int

Level width

class level.Level(width, height, *, content: Sequence[bool] = ())

This is a basic implementation for level.

It has built-in functions to load level from different sources.

  1. Prepared Sequence[bool]
    1level_content = (
    2        True, False, False, True,
    3        False, True, True, False,
    4        True, False, True, False,
    5)
    6level = Level(4, 3, content=level_content)
    
  2. String
    1level_content = '''\
    2X..X
    3.XX.
    4X.X.
    5'''
    6level = Level.from_string(4, 3, level_content, '.', 'X')
    
  3. File (TextIO)

    Note

    Text io data must be written in special format

    1import io
    2level_content = '''\
    34 3 0
    4.*
    5X#
    6#..X    .#X*    X*X.
    7'''
    8level = Level.from_io(StringIO(level_content))
    

After that you get a simple level object.

>>> level.width
4
>>> level.height
3
>>> print(*level)
True False False True False True True False True False True False
__init__(width, height, *, content: Sequence[bool] = ())
Width:

level width

Height:

level height

Content:

Prepared Sequence[bool]

__iter__() Iterator[bool]

Line by line representation of the level.

Note

If the content length isn’t enough it yields False

classmethod from_file(filename: str)
Filename:

file name

The same as:

with open(filename) as file:
        level = Level.from_io(file)
classmethod from_io(io: TextIO)

Read level from io (use a special format).

Io:

TextIO to read level

classmethod from_string(width: int, height: int, string: str, off_codes: Container[str], on_codes: Container[str])

Read level from string.

Width:

level width

Height:

level height

String:

string of level (rows aren’t separated)

Off_codes:

Container of the light-off characters

On_codes:

Container of the light-on characters

Note

If character in string isn’t in on/off_codes it’s ignored

Examples:

  • 1string = '1001__0110__1010'
    2level = Level.from_string(4, 3, string, '0', '1')
    
  •  1line_separator = '_'
     2lights_off = '-~'
     3lights_on = '#X'
     4
     5line1 = '#--#'
     6line2 = '~XX~'
     7# Mixed case:
     8line3 = 'X-#~'
     9lines = line1, line2, line3
    10
    11string_a = line_separator.join(lines)
    12string_b = ''.join(lines) # Line separators are optional
    13
    14level_a = Level.from_string(4, 3, string_a, off_codes, on_codes)
    15level_b = Level.from_string(4, 3, string_b, off_codes, on_codes)
    16for cell_a, cell_b is zip(level_a, level_b):
    17        assert cell_a is cell_b
    
property height: int

Level height

property width: int

Level width