章节演示1#

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