![]() |
VOOZH | about |
In this article, we will learn How we can add enemies in arcade.
To add enemy to our game we are going to follow the below steps:
Syntax:
self.enemy = None
Syntax:
self.enemy_move = velocity
Syntax:
self.enemy = arcade.Sprite("path",1)Syntax:
self.enemy.center_x = value self.enemy.center_y = value
Syntax:
self.scene.add_sprite("Enemy", self.enemy)Syntax:
self.enemy.center_x += self.enemy_move
Syntax:
enemy_collide = arcade.check_for_collision_with_list(
self.player_sprite, self.scene.get_sprite_list("Enemy")
)
Syntax:
for enemy in enemy_collide:
self.player_sprite.remove_from_sprite_lists()
Syntax: arcade.draw_text(text, x, y, color, size, width, align, font_name)
Parameters:
text: Text we want to display
x : x coordinate
y : y coordinate
color : color of the text
size : Size of the font
width : Width of the text
align : Alignment of the text
font_name : Name of the font
Syntax: arcade.Camera( width , height, window)
Parameters:
width: width of the viewport
height: height of the viewport
window: Window to associate with this camera
Syntax: arcade.Scene(sprite_lists , name_mapping)
Parameters:
sprite_lists: A list of SpriteList objects
name_mapping: A dictionary of SpriteList objects
Syntax: arcade.PhysicsEnginePlatformer( player_sprite , platforms, gravity, ladders)
Parameters:
player_sprite: sprite of the player
platforms: The sprites it canβt move through
gravity: Downward acceleration per frame
ladders: Ladders the user can climb on
In the below example, we are going to create a MainGame() class. Inside this class first, we are going to initialize some variables for velocity, camera, score, level, enemy, enemy sprite, and player's sprite then we will create 6 functions inside this class.
Example: Adding enemy
Output:
π ImageNow we want to kill the enemy when the player jumps on top of it. For this, we will check if the bottom other player is equal to the top of the enemy or not. If it is equal then we will remove the layer sprite.
Syntax:
x = int(self.player_sprite.center_y - 14)
Syntax:
y = int(self.enemy.center_y + 25)
Syntax:
if x == y:
self.enemy.remove_from_sprite_lists()