An input object represents a game controller having left, right, up, down, and two buttons.

button1: None

Confirm, FIRE, or any other action may be associated with button1.

button2: None

Cancel, or any other action may be associated with button2.

down: None

The down button is a directional command to the game from the player.

left: None

The left button is a directional command to the game from the player.

right: None

The right button is a directional command to the game from the player.

up: None

The up button is a directional command to the game from the player.

__init__(self, left=False, right=False, up=False, down=False, button1=False, button2=False)

Show source code in ctoybox/input.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    def __init__(self, left: bool=False, right: bool=False, up: bool=False, down: bool=False, button1: bool=False, button2: bool=False):
        """
        The default constructor creates an input object with no buttons pressed.

        Arguments:
            left: Move left in most games.
            right: Move right in most games.
            up: Move up in most games.
            down: Move down in most games.
            button1: FIRE or ACTION1 or CONFIRM in most games.
            button2: ACTION2 or CANCEL in most games.
        """
        self.left = left
        """The left button is a directional command to the game from the player."""
        self.right = right
        """The right button is a directional command to the game from the player."""
        self.up = up
        """The up button is a directional command to the game from the player."""
        self.down = down
        """The down button is a directional command to the game from the player."""
        self.button1 = button1
        """Confirm, FIRE, or any other action may be associated with button1."""
        self.button2 = button2
        """Cancel, or any other action may be associated with button2."""

The default constructor creates an input object with no buttons pressed.

Parameters

Name Type Description Default
left bool Move left in most games. False
right bool Move right in most games. False
up bool Move up in most games. False
down bool Move down in most games. False
button1 bool FIRE or ACTION1 or CONFIRM in most games. False
button2 bool ACTION2 or CANCEL in most games. False

__repr__(self)

Show source code in ctoybox/input.py
60
61
62
63
64
    def __repr__(self):
        """
        Get a string representation of this Input object.
        """
        return self.__dict__.__str__()

Get a string representation of this Input object.

__str__(self)

Show source code in ctoybox/input.py
54
55
56
57
58
    def __str__(self) -> str:
        """
        Get a string representation of this Input object.
        """
        return self.__dict__.__str__()

Get a string representation of this Input object.

reset(self)

Show source code in ctoybox/input.py
43
44
45
46
47
48
49
50
51
52
    def reset(self):
        """
        This method turns all buttons pressed to false.
        """
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.button1 = False
        self.button2 = False

This method turns all buttons pressed to false.

set_input(self, input_dir, button='noop')

Show source code in ctoybox/input.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    def set_input(self, input_dir: str, button=_NOOP):
        """
        Set the direction and button separately based on strings.
        """
        input_dir = input_dir.lower()
        button = button.lower()

        # reset all directions
        if   input_dir == Input._NOOP:
            pass
        elif input_dir == Input._LEFT:
            self.left = True
        elif input_dir == Input._RIGHT:
            self.right = True
        elif input_dir == Input._UP:
            self.up = True
        elif input_dir == Input._DOWN:
            self.down = True
        else:
            print('input_dir:', input_dir)
            assert False

        # reset buttons
        if button == Input._NOOP:
            pass
        elif button == Input._BUTTON1:
            self.button1 = True
        elif button == Input._BUTTON2:
            self.button2 = True
        else:
            assert False

Set the direction and button separately based on strings.