Sections Demo 1#

Screen shot of using sections
sections_demo_1.py#
  1"""
  2Section Example 1:
  3
  4In this Section example we divide the screen in two sections and let the user
  5pick a box depending on the selected Section
  6
  7Note:
  8    - How View know nothing of what's happening inside the sections.
  9      Each section knows what to do.
 10    - Each event mouse input is handled by each Section even if the class
 11      it's the same (ScreenPart).
 12    - How on_mouse_enter/leave triggers in each Section when the mouse
 13      enter or leaves the section boundaries
 14
 15If Python and Arcade are installed, this example can be run from the command line with:
 16python -m arcade.examples.sections_demo_1
 17"""
 18from typing import Optional
 19
 20import arcade
 21
 22
 23class Box(arcade.SpriteSolidColor):
 24    """ This is a Solid Sprite that represents a GREEN Box on the screen """
 25
 26    def __init__(self, section):
 27        super().__init__(100, 100, color=arcade.color.APPLE_GREEN)
 28        self.section = section
 29
 30    def on_update(self, delta_time: float = 1 / 60):
 31        # update the box (this actually moves the Box by changing its position)
 32        self.update()
 33
 34        # if we hit the ground then lay on the ground and stop movement
 35        if self.bottom <= 0:
 36            self.bottom = 0
 37            self.stop()
 38
 39    def release(self):
 40        self.section.hold_box = None
 41        self.change_y = -10
 42
 43
 44class ScreenPart(arcade.Section):
 45    """
 46    This represents a part of the View defined by its
 47    boundaries (left, bottom, etc.)
 48    """
 49
 50    def __init__(self, left: int, bottom: int, width: int, height: int,
 51                 **kwargs):
 52        super().__init__(left, bottom, width, height, **kwargs)
 53
 54        self.selected: bool = False  # if this section is selected
 55
 56        self.box: Box = Box(self)  # the section Box Sprite
 57
 58        # position the Box inside this section using self.left + self.width
 59        self.box.position = self.left + (self.width / 2), 50
 60
 61        # variable that will hold the Box when it's being dragged
 62        self.hold_box: Optional[Box] = None
 63
 64    def on_update(self, delta_time: float):
 65        # call on_update on the owned Box
 66        self.box.on_update(delta_time)
 67
 68    def on_draw(self):
 69        """ Draw this section """
 70        if self.selected:
 71            # Section is selected when mouse is within its boundaries
 72            arcade.draw_lrbt_rectangle_filled(self.left, self.right, self.bottom,
 73                                              self.top, arcade.color.GRAY)
 74            arcade.draw_text(f'You\'re are on the {self.name}', self.left + 30,
 75                             self.top - 50, arcade.color.BLACK, 16)
 76
 77        # draw the box
 78        self.box.draw()
 79
 80    def on_mouse_drag(self, x: float, y: float, dx: float, dy: float,
 81                      _buttons: int, _modifiers: int):
 82        # if we hold a box, then whe move it at the same rate the mouse moves
 83        if self.hold_box:
 84            self.hold_box.position = x, y
 85
 86    def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
 87        # if we pick a Box with the mouse, "hold" it and stop its movement
 88        if self.box.collides_with_point((x, y)):
 89            self.hold_box = self.box
 90            self.hold_box.stop()
 91
 92    def on_mouse_release(self, x: float, y: float, button: int, modifiers: int):
 93        # if hold_box is True because we pick it with on_mouse_press
 94        # then release the Box
 95        if self.hold_box:
 96            self.hold_box.release()
 97
 98    def on_mouse_enter(self, x: float, y: float):
 99        # select this section
100        self.selected = True
101
102    def on_mouse_leave(self, x: float, y: float):
103        # unselect this section
104        self.selected = False
105
106        # if we are holding this section box and we leave the section
107        # we release the box as if we release the mouse button
108        if self.hold_box:
109            self.hold_box.release()
110
111
112class GameView(arcade.View):
113
114    def __init__(self):
115        super().__init__()
116
117        # add sections to the view
118
119        # 1) First section holds half of the screen
120        self.add_section(ScreenPart(0, 0, self.window.width / 2,
121                                    self.window.height, name='Left'))
122
123        # 2) Second section holds the other half of the screen
124        self.add_section(ScreenPart(self.window.width / 2, 0,
125                                    self.window.width / 2, self.window.height,
126                                    name='Right'))
127
128    def on_draw(self):
129        # clear the screen
130        self.clear(color=arcade.color.BEAU_BLUE)
131
132        # draw a line separating each Section
133        arcade.draw_line(self.window.width / 2, 0, self.window.width / 2,
134                         self.window.height, arcade.color.BLACK, 1)
135
136
137def main():
138    # create the window
139    window = arcade.Window()
140
141    # create the custom View. Sections are initialized inside the GameView init
142    view = GameView()
143
144    # show the view
145    window.show_view(view)
146
147    # run arcade loop
148    window.run()
149
150
151if __name__ == '__main__':
152    main()