Display Performance Statistics

Screen shot of using sprites to collect coins
sprite_collect_coins_with_stats.py
  1"""
  2Sprite Collect Coins with Stats
  3
  4Simple program to show basic sprite usage.
  5
  6Artwork from http://kenney.nl
  7
  8If Python and Arcade are installed, this example can be run from the command line with:
  9python -m arcade.examples.sprite_collect_coins_with_stats
 10"""
 11
 12import random
 13import arcade
 14import timeit
 15
 16# --- Constants ---
 17SPRITE_SCALING_PLAYER = 0.5
 18SPRITE_SCALING_COIN = 0.3
 19COIN_COUNT = 150
 20
 21SCREEN_WIDTH = 800
 22SCREEN_HEIGHT = 600
 23SCREEN_TITLE = "Sprite Collect Coins with Stats Example"
 24
 25
 26class MyGame(arcade.Window):
 27    """ Our custom Window Class"""
 28
 29    def __init__(self):
 30        """ Initializer """
 31        # Call the parent class initializer
 32        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 33
 34        # Variables that will hold sprite lists
 35        self.player_list = None
 36        self.coin_list = None
 37
 38        # Set up the player info
 39        self.player_sprite = None
 40        self.score = 0
 41
 42        # Don't show the mouse cursor
 43        self.set_mouse_visible(False)
 44
 45        # --- Variables for our statistics
 46
 47        # Time for on_update
 48        self.processing_time = 0
 49
 50        # Time for on_draw
 51        self.draw_time = 0
 52
 53        # Variables used to calculate frames per second
 54        self.frame_count = 0
 55        self.fps_start_timer = None
 56        self.fps = None
 57
 58        arcade.set_background_color(arcade.color.AMAZON)
 59
 60    def setup(self):
 61        """ Set up the game and initialize the variables. """
 62
 63        # Sprite lists
 64        self.player_list = arcade.SpriteList()
 65        self.coin_list = arcade.SpriteList()
 66
 67        # Score
 68        self.score = 0
 69
 70        # Set up the player
 71        # Character image from kenney.nl
 72        self.player_sprite = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", SPRITE_SCALING_PLAYER)
 73        self.player_sprite.center_x = 50
 74        self.player_sprite.center_y = 50
 75        self.player_list.append(self.player_sprite)
 76
 77        # Create the coins
 78        for i in range(COIN_COUNT):
 79
 80            # Create the coin instance
 81            # Coin image from kenney.nl
 82            coin = arcade.Sprite(":resources:images/items/coinGold.png",
 83                                 SPRITE_SCALING_COIN)
 84
 85            # Position the coin
 86            coin.center_x = random.randrange(SCREEN_WIDTH)
 87            coin.center_y = random.randrange(SCREEN_HEIGHT)
 88
 89            # Add the coin to the lists
 90            self.coin_list.append(coin)
 91
 92    def on_draw(self):
 93        """ Draw everything """
 94
 95        # Start timing how long this takes
 96        start_time = timeit.default_timer()
 97
 98        # --- Calculate FPS
 99
100        fps_calculation_freq = 60
101        # Once every 60 frames, calculate our FPS
102        if self.frame_count % fps_calculation_freq == 0:
103            # Do we have a start time?
104            if self.fps_start_timer is not None:
105                # Calculate FPS
106                total_time = timeit.default_timer() - self.fps_start_timer
107                self.fps = fps_calculation_freq / total_time
108            # Reset the timer
109            self.fps_start_timer = timeit.default_timer()
110        # Add one to our frame count
111        self.frame_count += 1
112
113        arcade.start_render()
114        self.coin_list.draw()
115        self.player_list.draw()
116
117        # Put the text on the screen.
118        output = f"Score: {self.score}"
119        arcade.draw_text(output, 10, 20, arcade.color.BLACK, 18)
120
121        # Display timings
122        output = f"Processing time: {self.processing_time:.3f}"
123        arcade.draw_text(output, 20, SCREEN_HEIGHT - 25, arcade.color.BLACK, 18)
124
125        output = f"Drawing time: {self.draw_time:.3f}"
126        arcade.draw_text(output, 20, SCREEN_HEIGHT - 50, arcade.color.BLACK, 18)
127
128        if self.fps is not None:
129            output = f"FPS: {self.fps:.0f}"
130            arcade.draw_text(output, 20, SCREEN_HEIGHT - 75, arcade.color.BLACK, 18)
131
132        # Stop the draw timer, and calculate total on_draw time.
133        self.draw_time = timeit.default_timer() - start_time
134
135    def on_mouse_motion(self, x, y, dx, dy):
136        """ Handle Mouse Motion """
137
138        # Move the center of the player sprite to match the mouse x, y
139        self.player_sprite.center_x = x
140        self.player_sprite.center_y = y
141        # print(x, y)
142
143    def on_update(self, delta_time):
144        """ Movement and game logic """
145
146        # Start timing how long this takes
147        start_time = timeit.default_timer()
148
149        # Call update on all sprites (The sprites don't do much in this
150        # example though.)
151        self.coin_list.update()
152
153        # Generate a list of all sprites that collided with the player.
154        coins_hit_list = arcade.check_for_collision_with_list(self.player_sprite, self.coin_list)
155
156        # Loop through each colliding sprite, remove it, and add to the score.
157        for coin in coins_hit_list:
158            coin.remove_from_sprite_lists()
159            self.score += 1
160
161        # Stop the draw timer, and calculate total on_draw time.
162        self.processing_time = timeit.default_timer() - start_time
163
164
165def main():
166    """ Main method """
167    window = MyGame()
168    window.setup()
169    arcade.run()
170
171
172if __name__ == "__main__":
173    main()