03_views.py Full Listing#

03_views.py#
  1import random
  2import arcade
  3
  4# --- Constants ---
  5SPRITE_SCALING_PLAYER = 0.5
  6SPRITE_SCALING_COIN = .25
  7COIN_COUNT = 50
  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        arcade.set_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        arcade.set_viewport(0, self.window.width, 0, self.window.height)
 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 GameView(arcade.View):
 41    """ Our custom Window Class"""
 42
 43    def __init__(self):
 44        """ Initializer """
 45        # Call the parent class initializer
 46        super().__init__()
 47
 48        # Variables that will hold sprite lists
 49        self.player_list = None
 50        self.coin_list = None
 51
 52        # Set up the player info
 53        self.player_sprite = None
 54        self.score = 0
 55
 56        # Don't show the mouse cursor
 57        self.window.set_mouse_visible(False)
 58
 59        arcade.set_background_color(arcade.color.AMAZON)
 60
 61    def setup(self):
 62        """ Set up the game and initialize the variables. """
 63
 64        # Sprite lists
 65        self.player_list = arcade.SpriteList()
 66        self.coin_list = arcade.SpriteList()
 67
 68        # Score
 69        self.score = 0
 70
 71        # Set up the player
 72        # Character image from kenney.nl
 73        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png",
 74                                           SPRITE_SCALING_PLAYER)
 75        self.player_sprite.center_x = 50
 76        self.player_sprite.center_y = 50
 77        self.player_list.append(self.player_sprite)
 78
 79        # Create the coins
 80        for i in range(COIN_COUNT):
 81
 82            # Create the coin instance
 83            # Coin image from kenney.nl
 84            coin = arcade.Sprite(":resources:images/items/coinGold.png",
 85                                 SPRITE_SCALING_COIN)
 86
 87            # Position the coin
 88            coin.center_x = random.randrange(SCREEN_WIDTH)
 89            coin.center_y = random.randrange(SCREEN_HEIGHT)
 90
 91            # Add the coin to the lists
 92            self.coin_list.append(coin)
 93
 94    def on_draw(self):
 95        """ Draw everything """
 96        self.clear()
 97        self.coin_list.draw()
 98        self.player_list.draw()
 99
100        # Put the text on the screen.
101        output = f"Score: {self.score}"
102        arcade.draw_text(output, 10, 20, arcade.color.WHITE, 14)
103
104    def on_mouse_motion(self, x, y, dx, dy):
105        """ Handle Mouse Motion """
106
107        # Move the center of the player sprite to match the mouse x, y
108        self.player_sprite.center_x = x
109        self.player_sprite.center_y = y
110
111    def on_update(self, delta_time):
112        """ Movement and game logic """
113
114        # Call update on all sprites (The sprites don't do much in this
115        # example though.)
116        self.coin_list.update()
117
118        # Generate a list of all sprites that collided with the player.
119        coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
120
121        # Loop through each colliding sprite, remove it, and add to the score.
122        for coin in coins_hit_list:
123            coin.remove_from_sprite_lists()
124            self.score += 1
125
126
127def main():
128    """ Main function """
129
130    window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
131    start_view = InstructionView()
132    window.show_view(start_view)
133    arcade.run()
134
135
136if __name__ == "__main__":
137    main()