将视图用于暂停屏幕#
您可能还想退房 使用开始/结束屏幕的视图 。
view_pause_screen.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 | """ This program shows how to have a pause screen without resetting the game. Make a separate class for each view (screen) in your game. The class will inherit from arcade.View. The structure will look like an arcade.Window as each View will need to have its own draw, update and window event methods. To switch a View, simply create a view with `view = MyView()` and then use the "self.window.set_view(view)" method. If Python and Arcade are installed, this example can be run from the command line with: python -m arcade.examples.view_pause_screen """ import arcade WIDTH = 800 HEIGHT = 600 SPRITE_SCALING = 0.5 class MenuView(arcade.View): def on_show_view(self): arcade.set_background_color(arcade.color.WHITE) def on_draw(self): self.clear() arcade.draw_text("Menu Screen", WIDTH / 2, HEIGHT / 2, arcade.color.BLACK, font_size=50, anchor_x="center") arcade.draw_text("Click to advance.", WIDTH / 2, HEIGHT / 2 - 75, arcade.color.GRAY, font_size=20, anchor_x="center") def on_mouse_press(self, _x, _y, _button, _modifiers): game = GameView() self.window.show_view(game) class GameView(arcade.View): def __init__(self): super().__init__() self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING) self.player_sprite.center_x = 50 self.player_sprite.center_y = 50 self.player_sprite.velocity = [3, 3] def on_show_view(self): arcade.set_background_color(arcade.color.AMAZON) def on_draw(self): self.clear() # Draw all the sprites. self.player_sprite.draw() # Show tip to pause screen arcade.draw_text("Press Esc. to pause", WIDTH / 2, HEIGHT - 100, arcade.color.BLACK, font_size=20, anchor_x="center") def on_update(self, delta_time): # Call update on all sprites self.player_sprite.update() # Bounce off the edges if self.player_sprite.left < 0 or self.player_sprite.right > WIDTH: self.player_sprite.change_x *= -1 if self.player_sprite.bottom < 0 or self.player_sprite.top > HEIGHT: self.player_sprite.change_y *= -1 def on_key_press(self, key, _modifiers): if key == arcade.key.ESCAPE: # pass self, the current view, to preserve this view's state pause = PauseView(self) self.window.show_view(pause) class PauseView(arcade.View): def __init__(self, game_view): super().__init__() self.game_view = game_view def on_show_view(self): arcade.set_background_color(arcade.color.ORANGE) def on_draw(self): self.clear() # Draw player, for effect, on pause screen. # The previous View (GameView) was passed in # and saved in self.game_view. player_sprite = self.game_view.player_sprite player_sprite.draw() # draw an orange filter over him arcade.draw_lrtb_rectangle_filled(left=player_sprite.left, right=player_sprite.right, top=player_sprite.top, bottom=player_sprite.bottom, color=arcade.color.ORANGE + (200,)) arcade.draw_text("PAUSED", WIDTH / 2, HEIGHT / 2 + 50, arcade.color.BLACK, font_size=50, anchor_x="center") # Show tip to return or reset arcade.draw_text("Press Esc. to return", WIDTH / 2, HEIGHT / 2, arcade.color.BLACK, font_size=20, anchor_x="center") arcade.draw_text("Press Enter to reset", WIDTH / 2, HEIGHT / 2 - 30, arcade.color.BLACK, font_size=20, anchor_x="center") def on_key_press(self, key, _modifiers): if key == arcade.key.ESCAPE: # resume game self.window.show_view(self.game_view) elif key == arcade.key.ENTER: # reset game game = GameView() self.window.show_view(game) def main(): window = arcade.Window(WIDTH, HEIGHT, "Instruction and Game Over Views Example") menu = MenuView() window.show_view(menu) arcade.run() if __name__ == "__main__": main() |