Code to draw stationary sprites in Arcade

draw_stationary_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_stationary_sprites.csv"
 20RESULTS_IMAGE = "../../result_data/arcade/draw_stationary_sprites.png"
 21SCREEN_WIDTH = 1800
 22SCREEN_HEIGHT = 1000
 23SCREEN_TITLE = "Arcade - Stationary Sprite Stress Test"
 24
 25
 26class Coin(arcade.Sprite):
 27
 28    def on_update(self, delta_time):
 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            # Add the coin to the lists
 76            self.coin_list.append(coin)
 77
 78    def setup(self):
 79        """ Set up the game and initialize the variables. """
 80
 81        # Sprite lists
 82        self.coin_list = arcade.SpriteList()
 83
 84    def on_draw(self):
 85        """ Draw everything """
 86
 87        # Start timing how long this takes
 88        self.performance_timing.start_timer('draw')
 89
 90        # Clear the screen
 91        self.clear()
 92
 93        # Draw all the sprites
 94        self.coin_list.draw()
 95
 96        # Stop timing how long this takes
 97        self.performance_timing.stop_timer('draw')
 98
 99    def on_update(self, delta_time):
100
101        # Start update timer
102        self.performance_timing.start_timer('update')
103
104        # Stop timing the update
105        self.performance_timing.stop_timer('update')
106
107        # Figure out if we need more coins
108        if self.performance_timing.target_n > len(self.coin_list):
109            new_coin_amount = self.performance_timing.target_n - len(self.coin_list)
110            self.add_coins(new_coin_amount)
111
112        # End the program run
113        if self.performance_timing.end_run():
114            # Save screenshot
115            image = arcade.get_image()
116            image.save(RESULTS_IMAGE, 'PNG')
117            self.close()
118
119
120def main():
121    """ Main method """
122    window = MyGame()
123    window.setup()
124    arcade.run()
125    # arcade.set_window(None)
126
127
128if __name__ == "__main__":
129    main()