Tetris wrapper

class pyboy.plugins.game_wrapper_tetris.GameWrapperTetris(*args, **kwargs)[source]

Bases: PyBoyGameWrapper

This class wraps Tetris, and provides easy access to score, lines and level for AIs.

If you call print on an instance of this object, it will show an overview of everything this object provides.

mapping_compressed

Compressed mapping for pyboy.PyBoy.game_area_mapping

mapping_minimal

Minimal mapping for pyboy.PyBoy.game_area_mapping

score

The score provided by the game

level

The current level

lines

The number of cleared lines

start_game(timer_div=None)[source]

Call this function right after initializing PyBoy. This will navigate through menus to start the game at the first playable state.

The state of the emulator is saved, and using reset_game, you can get back to this point of the game instantly.

Kwargs:
  • timer_div (int): Replace timer’s DIV register with this value. Use None to randomize.

reset_game(timer_div=None)[source]

After calling start_game, you can call this method at any time to reset the game.

Kwargs:
  • timer_div (int): Replace timer’s DIV register with this value. Use None to randomize.

game_area()[source]

Use this method to get a matrix of the “game area” of the screen. This view is simplified to be perfect for machine learning applications.

In Tetris, this is only the part of the screen where the “tetrominoes” are placed. The score, lines cleared, and level can be found in the variables of this class.

     0   1   2   3   4   5   6   7   8   9
____________________________________________
0  | 47  47  47  47  47  47  47  47  47  47
1  | 47  47  47  47  47  47  47  47  47  47
2  | 47  47  47  47  47  47  47  132 132 132
3  | 47  47  47  47  47  47  47  132 47  47
4  | 47  47  47  47  47  47  47  47  47  47
5  | 47  47  47  47  47  47  47  47  47  47
6  | 47  47  47  47  47  47  47  47  47  47
7  | 47  47  47  47  47  47  47  47  47  47
8  | 47  47  47  47  47  47  47  47  47  47
9  | 47  47  47  47  47  47  47  47  47  47
10 | 47  47  47  47  47  47  47  47  47  47
11 | 47  47  47  47  47  47  47  47  47  47
12 | 47  47  47  47  47  47  47  47  47  47
13 | 47  47  47  47  47  47  47  47  47  47
14 | 47  47  47  47  47  47  47  47  47  47
15 | 47  47  47  47  47  47  47  47  47  47
16 | 47  47  47  47  47  47  47  47  47  47
17 | 47  47  47  47  47  47  138 139 139 143
Returns:

Simplified two-dimensional array of mapped tile identifiers with dtype numpy.uint32.

Return type:

numpy.ndarray

next_tetromino()[source]

Returns the next Tetromino to drop.

NOTE: Don’t use this function together with pyboy.plugins.game_wrapper_tetris.GameWrapperTetris.set_tetromino.

Returns:

str of which Tetromino will drop:

  • "L": L-shape

  • "J": reverse L-shape

  • "I": I-shape

  • "O": square-shape

  • "Z": zig-zag left to right

  • "S": zig-zag right to left

  • "T": T-shape

Return type:

shape

set_tetromino(shape)[source]

This function patches the random Tetromino routine in the ROM to output any given Tetromino instead.

NOTE: Any changes here are not saved or loaded to game states! Use this function with caution and reapply any overrides when reloading the ROM. This also applies to pyboy.plugins.game_wrapper_tetris.GameWrapperTetris.start_game and pyboy.plugins.game_wrapper_tetris.GameWrapperTetris.reset_game.

Parameters:
  • shape (str) – Define which Tetromino to use:

  • "L" (*) – L-shape

  • "J" (*) – reverse L-shape

  • "I" (*) – I-shape

  • "O" (*) – square-shape

  • "Z" (*) – zig-zag left to right

  • "S" (*) – zig-zag right to left

  • "T" (*) – T-shape

game_over()[source]

After calling start_game, you can call this method at any time to know if the game is over.

Game over happens, when the game area is filled with Tetrominos without clearing any rows.