Arcade Collision Source Code

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