forked from pythonarcade/arcade
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import arcade | ||
|
||
SPRITE_IMAGE_SIZE = 128 | ||
SPRITE_SCALING = 0.25 | ||
SPRITE_SIZE = SPRITE_IMAGE_SIZE * SPRITE_SCALING | ||
|
||
def test_astar(): | ||
grid_size = SPRITE_SIZE | ||
|
||
# Sprite lists | ||
player_list = arcade.SpriteList() | ||
wall_list = arcade.SpriteList(use_spatial_hash=True, spatial_hash_cell_size=128) | ||
enemy_list = arcade.SpriteList() | ||
|
||
# Set up the player | ||
player = arcade.Sprite(":resources:images/animated_characters/female_person/femalePerson_idle.png", | ||
SPRITE_SCALING) | ||
player.center_x = SPRITE_SIZE * 1 | ||
player.center_y = SPRITE_SIZE * 1 | ||
player_list.append(player) | ||
|
||
# Set enemies | ||
enemy = arcade.Sprite(":resources:images/animated_characters/zombie/zombie_idle.png", SPRITE_SCALING) | ||
enemy.center_x = SPRITE_SIZE * 5 | ||
enemy.center_y = SPRITE_SIZE * 5 | ||
enemy_list.append(enemy) | ||
|
||
# Calculate the playing field size. We can't generate paths outside of | ||
# this. | ||
playing_field_left_boundary = -SPRITE_SIZE * 2 | ||
playing_field_right_boundary = SPRITE_SIZE * 35 | ||
playing_field_top_boundary = SPRITE_SIZE * 17 | ||
playing_field_bottom_boundary = -SPRITE_SIZE * 2 | ||
|
||
# This calculates a list of barriers. By calculating it here in the | ||
# init, we are assuming this list does not change. In this example, | ||
# our walls don't move, so that is ok. If we want moving barriers (such as | ||
# moving platforms or enemies) we need to recalculate. This can be an | ||
# time-intensive process depending on the playing field size and grid | ||
# resolution. | ||
|
||
# Note: If the enemy sprites are the same size, we only need to calculate | ||
# one of these. We do NOT need a different one for each enemy. The sprite | ||
# is just used for a size calculation. | ||
barrier_list = arcade.AStarBarrierList(enemy, | ||
wall_list, | ||
grid_size, | ||
playing_field_left_boundary, | ||
playing_field_right_boundary, | ||
playing_field_bottom_boundary, | ||
playing_field_top_boundary) | ||
|
||
|
||
# print() | ||
path = arcade.astar_calculate_path(enemy.position, | ||
player.position, | ||
barrier_list, | ||
diagonal_movement=False) | ||
|
||
# print(path) | ||
assert path == [(160, 160), (128, 160), (128, 128), (128, 96), (96, 96), (96, 64), (64, 64), (64, 32), (32, 32)] | ||
|
||
path = arcade.astar_calculate_path(enemy.position, | ||
player.position, | ||
barrier_list, | ||
diagonal_movement=True) | ||
assert path == [(160, 160), (128, 128), (96, 96), (64, 64), (32, 32)] | ||
# print(path) | ||
|
||
|
||
sprite = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING) | ||
sprite.center_x = SPRITE_SIZE * 3 | ||
sprite.center_y = SPRITE_SIZE * 1 | ||
wall_list.append(sprite) | ||
|
||
sprite = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING) | ||
sprite.center_x = SPRITE_SIZE * 3 | ||
sprite.center_y = SPRITE_SIZE * 2 | ||
wall_list.append(sprite) | ||
|
||
sprite = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING) | ||
sprite.center_x = SPRITE_SIZE * 3 | ||
sprite.center_y = SPRITE_SIZE * 3 | ||
wall_list.append(sprite) | ||
|
||
sprite = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING) | ||
sprite.center_x = SPRITE_SIZE * 3 | ||
sprite.center_y = SPRITE_SIZE * 4 | ||
wall_list.append(sprite) | ||
|
||
sprite = arcade.Sprite(":resources:images/tiles/grassCenter.png", SPRITE_SCALING) | ||
sprite.center_x = SPRITE_SIZE * 3 | ||
sprite.center_y = SPRITE_SIZE * 5 | ||
wall_list.append(sprite) | ||
|
||
barrier_list.recalculate() | ||
|
||
path = arcade.astar_calculate_path(enemy.position, | ||
player.position, | ||
barrier_list, | ||
diagonal_movement=True) | ||
|
||
assert path == [(160, 160), (128, 128), (128, 96), (128, 64), (128, 32), (96, 0), (64, 0), (32, 32)] |