Arcade Draw Moving Sprites Source Code

draw_moving_sprites.py
  1"""
  2Moving Sprite Stress Test
  3
  4Simple program to test how fast we can draw sprites that are moving
  5
  6Artwork from https://kenney.nl
  7"""
  8
  9import random
 10import arcade
 11import os
 12from performance_timing import PerformanceTiming
 13
 14# --- Constants ---
 15SPRITE_SCALING_COIN = 0.25
 16SPRITE_NATIVE_SIZE = 128
 17SPRITE_SIZE = int(SPRITE_NATIVE_SIZE * SPRITE_SCALING_COIN)
 18
 19RESULTS_FILE = "../../result_data/arcade/draw_moving_sprites.csv"
 20RESULTS_IMAGE = "../../result_data/arcade/draw_moving_sprites.png"
 21SCREEN_WIDTH = 1800
 22SCREEN_HEIGHT = 1000
 23SCREEN_TITLE = "Arcade - Moving Sprite Stress Test"
 24
 25
 26class Coin(arcade.Sprite):
 27
 28    def on_update(self, dt):
 29        """
 30        Update the sprite.
 31        """
 32        self.position = (self.position[0] + self.change_x, self.position[1] + self.change_y)
 33
 34
 35class MyGame(arcade.Window):
 36    """ Our custom Window Class"""
 37
 38    def __init__(self):
 39        """ Initializer """
 40        # Call the parent class initializer
 41        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
 42
 43        arcade.cleanup_texture_cache()
 44
 45        # Set the working directory (where we expect to find files) to the same
 46        # directory this .py file is in. You can leave this out of your own
 47        # code, but it is needed to easily run the examples using "python -m"
 48        # as mentioned at the top of this program.
 49        file_path = os.path.dirname(os.path.abspath(__file__))
 50        os.chdir(file_path)
 51
 52        # Variables that will hold sprite lists
 53        self.coin_list = None
 54        self.sprite_count_list = []
 55
 56        self.performance_timing = PerformanceTiming(results_file=RESULTS_FILE,
 57                                                    start_n=0,
 58                                                    increment_n=250,
 59                                                    end_time=60)
 60
 61        arcade.set_background_color(arcade.color.AMAZON)
 62
 63    def add_coins(self, amount):
 64
 65        # Create the coins
 66        for i in range(amount):
 67            # Create the coin instance
 68            # Coin image from kenney.nl
 69            coin = Coin("../resources/coinGold.png", SPRITE_SCALING_COIN)
 70
 71            # Position the coin
 72            coin.center_x = random.randrange(SPRITE_SIZE, SCREEN_WIDTH - SPRITE_SIZE)
 73            coin.center_y = random.randrange(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE)
 74
 75            coin.change_x = random.randrange(-3, 4)
 76            coin.change_y = random.randrange(-3, 4)
 77
 78            # Add the coin to the lists
 79            self.coin_list.append(coin)
 80
 81    def setup(self):
 82        """ Set up the game and initialize the variables. """
 83
 84        # Sprite lists
 85        self.coin_list = arcade.SpriteList(use_spatial_hash=False)
 86
 87    def on_draw(self):
 88        """ Draw everything """
 89
 90        # Start timing how long this takes
 91        self.performance_timing.start_timer('draw')
 92
 93        # Clear the screen
 94        arcade.start_render()
 95
 96        # Draw all the sprites
 97        self.coin_list.draw()
 98
 99        # Stop timing how long this takes
100        self.performance_timing.stop_timer('draw')
101
102    def on_update(self, delta_time):
103        # Start update timer
104        self.performance_timing.start_timer('update')
105
106        self.coin_list.update()
107
108        for sprite in self.coin_list:
109
110            if sprite.position[0] < 0:
111                sprite.change_x *= -1
112            elif sprite.position[0] > SCREEN_WIDTH:
113                sprite.change_x *= -1
114            if sprite.position[1] < 0:
115                sprite.change_y *= -1
116            elif sprite.position[1] > SCREEN_HEIGHT:
117                sprite.change_y *= -1
118
119        # Stop timing the update
120        self.performance_timing.stop_timer('update')
121
122        # Figure out if we need more coins
123        if self.performance_timing.target_n > len(self.coin_list):
124            new_coin_amount = self.performance_timing.target_n - len(self.coin_list)
125            self.add_coins(new_coin_amount)
126
127        # End the program run
128        if self.performance_timing.end_run():
129            # Save screenshot
130            image = arcade.get_image()
131            image.save(RESULTS_IMAGE, 'PNG')
132            self.close()
133
134
135def main():
136    """ Main method """
137    window = MyGame()
138    window.setup()
139    arcade.run()
140    # arcade.set_window(None)
141
142
143if __name__ == "__main__":
144    main()