Power-ups and collectibles are essential game elements that can enhance the gameplay experience and add excitement to arcade games. They provide players with additional abilities, rewards, and challenges. With the arcade library, you'll be able to create engaging games with power-ups and collectibles in no time.
Create a Simple Game
Start with a basic setup where the player can move in four directions (left, right, up, and down) using the keyboard inputs. Additionally, introduce one enemy object to provide interaction within the game environment.
The code used in this article is available in this GitHub repository and is free for you to use under the MIT license.
Create a new file named simple-game.py and add the below code:
import arcadeblue = arcade.color.BLUE
red = arcade.color.RED
black = arcade.color.BLACK
white = arcade.color.WHITE
yellow = arcade.color.YELLOW
green = arcade.color.GREEN
orange = arcade.color.ORANGE
classGame(arcade.Window):
def__init__(self, width, height):
super().__init__(width, height)
arcade.set_background_color(black)
self.player = arcade.SpriteCircle(20, blue)
self.player.center_x = width // 2
self.player.center_y = height // 2
self.enemy = arcade.SpriteSolidColor(20, 20, red)
self.enemy.center_x = width // 4
self.enemy.center_y = height // 4
defon_draw(self):
arcade.start_render()
self.player.draw()
self.enemy.draw()
defupdate(self, delta_time):
pass
defon_key_press(self, key, modifiers):
if key == arcade.key.LEFT:
self.player.center_x -= 10
elif key == arcade.key.RIGHT:
self.player.center_x += 10
elif key == arcade.key.UP:
self.player.center_y += 10
elif key == arcade.key.DOWN:
self.player.center_y -= 10
defmain():
game = Game(800, 600)
arcade.run()
if __name__ == "__main__":
main()
Run this code, and you will have a basic game with player movement and an enemy object.
Creating Game States
To incorporate power-ups and collectibles, you need to establish game states such as score and health. You can manage score and health using a separate variable. Modify the code to include these features. The player's health will decrease when they collide with the enemy object.
classGame(arcade.Window):
def__init__(self, width, height):
super().__init__(width, height)self.score = 0
self.health = 100
defon_draw(self):
arcade.draw_text(f"Score: {self.score}", 10, 10, white)
arcade.draw_text(f"Health: {self.health}", 10, 30, white)
defupdate(self, delta_time):
if arcade.check_for_collision(self.player, self.enemy):
self.health -= 10
if self.health <= 0:
self.game_over()
defgame_over(self):
# Add game over logic here
pass
Now, the player's health decreases by 10 when colliding with the enemy, and the score and health values display on the screen.
Adding Collectibles
Next, create collectibles that will increase the player's score by 10 when collected. These collectibles will have different shapes for visual variety. Create a new file named collectibles.py and add the code with the below update:
classGame(arcade.Window):
def__init__(self, width, height):
super().__init__(width, height)self.collectibles = arcade.SpriteList()
for _ in range(5):
collectible = arcade.SpriteSolidColor(20, 40, yellow)
collectible.center_x = random.randint(0, width)
collectible.center_y = random.randint(0, height)
self.collectibles.append(collectible)
defon_draw(self):
arcade.start_render()
self.player.draw()
self.enemy.draw()
self.collectibles.draw()
arcade.draw_text(f"Score: {self.score}", 10, 10, white)
arcade.draw_text(f"Health: {self.health}", 10, 30, white)
defupdate(self, delta_time):
for collectible in self.collectibles:
if arcade.check_for_collision(self.player, collectible):
self.score += 10
collectible.remove_from_sprite_lists()
Create a list of collectibles with five instances, each represented by a yellow rectangle. When the player collides with a collectible, their score increases by 10, and the collectible is removed from the sprite list.
Adding Power-Ups
Now, you can add power-ups to your game. When the player collects a power-up, a shield will appear around them for 10 seconds. During this time, if the player collides with the enemy, they will destroy the enemy. Create a new file named power-ups.py and add the code with the below update:
classGame(arcade.Window):
def__init__(self, width, height):
super().__init__(width, height)self.power_up = arcade.SpriteSolidColor(50, 20, green)
self.power_up.center_x = random.randint(0, width)
self.power_up.center_y = random.randint(0, height)
self.shield_active = False
self.shield_duration = 10
self.shield_timer = 0
defon_draw(self):
arcade.start_render()
self.player.draw()
self.enemy.draw()
self.collectibles.draw()
self.power_up.draw()
arcade.draw_text(f"Score: {self.score}", 10, 10, white)
arcade.draw_text(f"Health: {self.health}", 10, 30, white)
defupdate(self, delta_time):
if arcade.check_for_collision(self.player, self.enemy):
ifnot self.shield_active:
self.health -= 10
if self.health <= 0:
self.game_over()
else:
self.enemy.remove_from_sprite_lists()
if self.shield_active:
self.shield_timer += delta_time
if self.shield_timer >= self.shield_duration:
self.shield_active = False
self.shield_timer = 0
for collectible in self.collectibles:
if arcade.check_for_collision(self.player, collectible):
self.score += 10
collectible.remove_from_sprite_lists()
if arcade.check_for_collision(self.player, self.power_up):
self.shield_active = True
self.power_up.remove_from_sprite_lists()
Below is the output:
Including Additional Features
You can further enhance the power-ups and collectibles system by adding additional features. For example, you can create a timer power-up that extends the gameplay time when collected. Create a new file named timer-power-up.py and add the code with the below update:
classGame(arcade.Window):
def__init__(self, width, height):
super().__init__(width, height)
arcade.set_background_color(black)self.player = arcade.SpriteCircle(20, blue)
# ...
self.timer_power_up = arcade.SpriteSolidColor(40, 20, orange)
self.timer_power_up.center_x = random.randint(0, width)
self.timer_power_up.center_y = random.randint(0, height)
self.game_time = 60# Initial game time in seconds
self.timer_power_up_duration = 10
# ...
defupdate(self, delta_time):
# ...
if arcade.check_for_collision(self.player, self.timer_power_up):
self.game_time += self.timer_power_up_duration
self.timer_power_up.remove_from_sprite_lists()
# ...
defmain():
game = Game(800, 600)
arcade.run()
if __name__ == "__main__":
main()
Best Practices for Power-Ups and Collectibles
Power-ups and collectibles play a crucial role in enhancing the gameplay experience of arcade games. To ensure that these elements are enjoyable and well-integrated into the game, it's essential to follow some best practices:
Visual Clarity and Consistency
Use distinct and visually appealing sprites for power-ups and collectibles to make them stand out from other game elements. Ensure that the appearance of power-ups and collectibles aligns with their effects and purpose in the game.
Maintain consistency in design, color schemes, and size for power-ups and collectibles throughout the game.
Balance and Challenge
Adjust the rarity and distribution of power-ups and collectibles to maintain a sense of challenge and reward for the players. Avoid overwhelming players with too many power-ups or collectibles, as it might diminish the impact and excitement of finding them.
Carefully balance the effectiveness and duration of power-ups to prevent them from being too overpowered or underwhelming.
Clear Indicators and Feedback
Provide clear visual and audio indicators when the player collects a power-up or collectible. Display temporary visual effects, such as animations or particles, to signify the activation of power-ups and their duration.
Challenge-Reward Relationship
Ensure that power-ups and collectibles provide meaningful rewards to the player, such as increased score, additional abilities, or extended gameplay time. Align the difficulty of obtaining power-ups and collectibles with the benefits they offer. More challenging tasks should yield more valuable rewards.
Playtesting and Balancing
Playtest the game extensively to observe how players interact with power-ups and collectibles. Use player feedback to fine-tune the distribution, duration, and effects of power-ups and collectibles to create a balanced and enjoyable experience.
By following these best practices, you can create a captivating and well-balanced power-up and collectibles system that enhances the gameplay experience and keeps players engaged in your arcade game.
Make Games More Fun With Power-Ups and Collectibles
Power-ups and collectibles add an extra layer of excitement and engagement to arcade games. They not only provide players with new abilities and rewards but also create interesting challenges and strategic choices.
By incorporating these features using the Python Arcade library, you can make your games more enjoyable and enhance the overall gaming experience for your players. Remember to experiment, iterate, and unleash your creativity to create unique and captivating power-ups and collectibles that align with your game's theme and mechanics.