|
| 1 | +import arcade |
| 2 | +import random |
| 3 | +from arcade_perf.tests.base import ArcadePerfTest |
| 4 | + |
| 5 | +SPRITE_SCALING_COIN = 0.09 |
| 6 | +SPRITE_SCALING_PLAYER = 0.5 |
| 7 | +SPRITE_NATIVE_SIZE = 128 |
| 8 | +SPRITE_SIZE = int(SPRITE_NATIVE_SIZE * SPRITE_SCALING_COIN) |
| 9 | +SCREEN_WIDTH = 1800 |
| 10 | +SCREEN_HEIGHT = 1000 |
| 11 | +SCREEN_TITLE = "Moving Sprite Stress Test - Arcade" |
| 12 | +USE_SPATIAL_HASHING = True |
| 13 | +DEFAULT_METHOD = 3 |
| 14 | +NAMES = { |
| 15 | + 0: "Arcade Auto", |
| 16 | + 1: "Arcade Spatial", |
| 17 | + 2: "Arcade GPU", |
| 18 | + 3: "Arcade Simple (No Spatial Hashing, No Sprite Lists)", |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +class Test(ArcadePerfTest): |
| 23 | + name = "collision" |
| 24 | + |
| 25 | + def __init__(self, method: int = DEFAULT_METHOD): |
| 26 | + super().__init__( |
| 27 | + size=(SCREEN_WIDTH, SCREEN_HEIGHT), |
| 28 | + title=SCREEN_TITLE, |
| 29 | + start_count=0, |
| 30 | + increment_count=100, |
| 31 | + duration=60.0, |
| 32 | + ) |
| 33 | + # Collision method |
| 34 | + self.method = method |
| 35 | + self.name = f"collision-{self.method}" |
| 36 | + self.series_name = NAMES[self.method] |
| 37 | + |
| 38 | + # Variables that will hold sprite lists |
| 39 | + self.coin_list = None |
| 40 | + self.player_list = None |
| 41 | + self.player = None |
| 42 | + |
| 43 | + def setup(self): |
| 44 | + self.window.background_color = arcade.color.AMAZON |
| 45 | + # Sprite lists |
| 46 | + self.coin_list = arcade.SpriteList(use_spatial_hash=USE_SPATIAL_HASHING) |
| 47 | + self.player_list = arcade.SpriteList() |
| 48 | + self.player = arcade.Sprite( |
| 49 | + ":resources:images/animated_characters/female_person/femalePerson_idle.png", |
| 50 | + scale=SPRITE_SCALING_PLAYER, |
| 51 | + ) |
| 52 | + self.player.center_x = random.randrange(SCREEN_WIDTH) |
| 53 | + self.player.center_y = random.randrange(SCREEN_HEIGHT) |
| 54 | + self.player.change_x = 3 |
| 55 | + self.player.change_y = 5 |
| 56 | + self.player_list.append(self.player) |
| 57 | + |
| 58 | + def add_coins(self, amount): |
| 59 | + """Add a new set of coins""" |
| 60 | + for i in range(amount): |
| 61 | + coin = arcade.Sprite(":resources:images/items/coinGold.png", SPRITE_SCALING_COIN) |
| 62 | + coin.position = ( |
| 63 | + random.randrange(SPRITE_SIZE, SCREEN_WIDTH - SPRITE_SIZE), |
| 64 | + random.randrange(SPRITE_SIZE, SCREEN_HEIGHT - SPRITE_SIZE), |
| 65 | + ) |
| 66 | + self.coin_list.append(coin) |
| 67 | + |
| 68 | + def on_draw(self): |
| 69 | + super().on_draw() |
| 70 | + self.coin_list.draw() |
| 71 | + self.player_list.draw() |
| 72 | + |
| 73 | + def on_update(self, delta_time: float): |
| 74 | + super().on_update(delta_time) |
| 75 | + |
| 76 | + self.player_list.update() |
| 77 | + if self.player.center_x < 0 and self.player.change_x < 0: |
| 78 | + self.player.change_x *= -1 |
| 79 | + if self.player.center_y < 0 and self.player.change_y < 0: |
| 80 | + self.player.change_y *= -1 |
| 81 | + |
| 82 | + if self.player.center_x > SCREEN_WIDTH and self.player.change_x > 0: |
| 83 | + self.player.change_x *= -1 |
| 84 | + if self.player.center_y > SCREEN_HEIGHT and self.player.change_y > 0: |
| 85 | + self.player.change_y *= -1 |
| 86 | + |
| 87 | + coin_hit_list = arcade.check_for_collision_with_list(self.player, self.coin_list, method=self.method) |
| 88 | + for coin in coin_hit_list: |
| 89 | + coin.center_x = random.randrange(SCREEN_WIDTH) |
| 90 | + coin.center_y = random.randrange(SCREEN_HEIGHT) |
| 91 | + |
| 92 | + def update_state(self): |
| 93 | + # Figure out if we need more coins |
| 94 | + if self.timing.target_n > len(self.coin_list): |
| 95 | + new_coin_amount = self.timing.target_n - len(self.coin_list) |
| 96 | + self.add_coins(new_coin_amount) |
| 97 | + self.coin_list.write_sprite_buffers_to_gpu() |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + Test().run_test() |
0 commit comments