04_views.py Full Listing#

04_views.py#
  1import random
  2import arcade
  3
  4# --- Constants ---
  5SPRITE_SCALING_PLAYER = 0.5
  6SPRITE_SCALING_COIN = .25
  7COIN_COUNT = 25
  8
  9SCREEN_WIDTH = 800
 10SCREEN_HEIGHT = 600
 11SCREEN_TITLE = "Implement Views Example"
 12
 13
 14class InstructionView(arcade.View):
 15    """ View to show instructions """
 16
 17    def on_show_view(self):
 18        """ This is run once when we switch to this view """
 19        self.window.background_color = arcade.csscolor.DARK_SLATE_BLUE
 20
 21        # Reset the viewport, necessary if we have a scrolling game and we need
 22        # to reset the viewport back to the start so we can see what we draw.
 23        self.window.default_camera.use()
 24
 25    def on_draw(self):
 26        """ Draw this view """
 27        self.clear()
 28        arcade.draw_text("Instructions Screen", self.window.width / 2, self.window.height / 2,
 29                         arcade.color.WHITE, font_size=50, anchor_x="center")
 30        arcade.draw_text("Click to advance", self.window.width / 2, self.window.height / 2-75,
 31                         arcade.color.WHITE, font_size=20, anchor_x="center")
 32
 33    def on_mouse_press(self, _x, _y, _button, _modifiers):
 34        """ If the user presses the mouse button, start the game. """
 35        game_view = GameView()
 36        game_view.setup()
 37        self.window.show_view(game_view)
 38
 39
 40class GameOverView(arcade.View):
 41    """ View to show when game is over """
 42
 43    def __init__(self):
 44        """ This is run once when we switch to this view """
 45        super().__init__()
 46        self.texture = arcade.load_texture("game_over.png")
 47
 48        # Reset the viewport, necessary if we have a scrolling game and we need
 49        # to reset the viewport back to the start so we can see what we draw.
 50        self.window.default_camera.use()
 51
 52    def on_draw(self):
 53        """ Draw this view """
 54        self.clear()
 55        self.texture.draw_sized(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2,
 56                                SCREEN_WIDTH, SCREEN_HEIGHT)
 57
 58    def on_mouse_press(self, _x, _y, _button, _modifiers):
 59        """ If the user presses the mouse button, re-start the game. """
 60        game_view = GameView()
 61        game_view.setup()
 62        self.window.show_view(game_view)
 63
 64
 65class GameView(arcade.View):
 66    """ Our custom Window Class"""
 67
 68    def __init__(self):
 69        """ Initializer """
 70        # Call the parent class initializer
 71        super().__init__()
 72
 73        # Variables that will hold sprite lists
 74        self.player_list = None
 75        self.coin_list = None
 76
 77        # Set up the player info
 78        self.player_sprite = None
 79        self.score = 0
 80
 81        # Don't show the mouse cursor
 82        self.window.set_mouse_visible(False)
 83
 84        self.background_color = arcade.color.AMAZON
 85
 86    def setup(self):
 87        """ Set up the game and initialize the variables. """
 88
 89        # Sprite lists
 90        self.player_list = arcade.SpriteList()
 91        self.coin_list = arcade.SpriteList()
 92
 93        # Score
 94        self.score = 0
 95
 96        # Set up the player
 97        # Character image from kenney.nl
 98        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 99                                           SPRITE_SCALING_PLAYER)
100        self.player_sprite.center_x = 50
101        self.player_sprite.center_y = 50
102        self.player_list.append(self.player_sprite)
103
104        # Create the coins
105        for i in range(COIN_COUNT):
106
107            # Create the coin instance
108            # Coin image from kenney.nl
109            coin = arcade.Sprite(":resources:images/items/coinGold.png",
110                                 SPRITE_SCALING_COIN)
111
112            # Position the coin
113            coin.center_x = random.randrange(SCREEN_WIDTH)
114            coin.center_y = random.randrange(SCREEN_HEIGHT)
115
116            # Add the coin to the lists
117            self.coin_list.append(coin)
118
119    def on_draw(self):
120        """ Draw everything """
121        self.clear()
122        self.coin_list.draw()
123        self.player_list.draw()
124
125        # Put the text on the screen.
126        output = f"Score: {self.score}"
127        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
128
129    def on_mouse_motion(self, x, y, dx, dy):
130        """ Handle Mouse Motion """
131
132        # Move the center of the player sprite to match the mouse x, y
133        self.player_sprite.center_x = x
134        self.player_sprite.center_y = y
135
136    def on_update(self, delta_time):
137        """ Movement and game logic """
138
139        # Call update on all sprites (The sprites don't do much in this
140        # example though.)
141        self.coin_list.update()
142
143        # Generate a list of all sprites that collided with the player.
144        coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
145
146        # Loop through each colliding sprite, remove it, and add to the score.
147        for coin in coins_hit_list:
148            coin.remove_from_sprite_lists()
149            self.score += 1
150
151        # Check length of coin list. If it is zero, flip to the
152        # game over view.
153        if len(self.coin_list) == 0:
154            view = GameOverView()
155            self.window.show_view(view)
156
157
158def main():
159    """ Main function """
160
161    window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
162    start_view = InstructionView()
163    window.show_view(start_view)
164    arcade.run()
165
166
167if __name__ == "__main__":
168    main()