How to add dialogue system in Python game using Arcade library
The actual characters interact often by conversing with each other. Make sure you present your character's dialogue in the most accessible way when developing the game.
If your game includes multiple characters, you'll probably want them to talk to each other. A dialogue system that provides interactive chats, story progression, and character development. The Python Arcade library makes it easy to integrate a conversation system into your existing codebase.
With a conversation system, you can create a conversation between a player and an existing character (NPC). Players can choose to respond or make decisions that affect the game's story or outcome.
This system allows you to create engaging stories and realistic interactions, providing a more immersive gameplay experience.
Create a simple game
To get started, create a simple game using the Arcade library. This will act as the foundation for perfecting the conversation system.
Create a new file named simple-game.py and define a Game class that inherits arcade.Window . Set screen size, create player and platform positions, implement basic drawing functions. The player can move left and right with the arrow keys.
Result:
Add dialog box
Now that you have a basic game structure, add a dialog box that pops up when the player collides with an enemy. You can create a dialog box using simple shapes and display it on the screen.
Add the dialogue_active flag to the Game class , indicating whether the dialogue is present or not. In the on_update method , check for enemy collisions. If detected, set dialogue_active to True .
In the on_draw method , call draw_dialogue_box if the dialog is active. Draw_dialogue_box draws a rectangle as the background of the dialog box and displays a text message.
Create a new file named dialogue-box.py and add the code with the updates below:
# Bên trong class Game class Game(arcade.Window): def __init__(self): # . self.dialogue_active = False def on_update(self, delta_time): if self.check_enemy_collision(): self.dialogue_active = True def on_draw(self): # . if self.dialogue_active: self.draw_dialogue_box() def check_enemy_collision(self): player_radius = 25 distance_x = abs(self.player_x - self.platform_x) distance_y = abs(self.player_y - self.platform_y) combined_radius = player_radius + 25 if distance_x < combined_radius and distance_y < combined_radius: return True else: return False def draw_dialogue_box(self): text = "Hello, player! How are you?" arcade.draw_rectangle_filled(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, 500, 200, white) arcade.draw_text(text, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 20, black, font_size=16, anchor_x="center", anchor_y="center")
Result:
Show conversation content
You can now enhance the conversation system by displaying dynamic text messages. Create a list of conversation threads and repeat them to simulate a conversation. Each time the dialog box appears, it will show a new message.
# Bên trong class Game class Game(arcade.Window): def __init__(self): # . self.dialogue_active = False self.dialogue_messages = [ "Hello, player! How are you?", "Nice weather today, isn't it?", "Beware of the enemies lurking ahead!" ] self.dialogue_index = 0 def draw_dialogue_box(self): arcade.draw_rectangle_filled(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2, 500, 200, white) arcade.draw_text(self.dialogue_messages[self.dialogue_index], SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 20, black, font_size=16, anchor_x="center", anchor_y="center")
Introduced 3 new variables: dialogue_messages , dialogue_index , and dialogue_active . The dialogue_messages list contains the text messages that the dialog will display. Dialog_index keeps track of the current message to show. Each time the dialog appears, you can increase the dialogue_index to show the next message.
More buttons
To create a more interactive dialogue system, add a button to change the content of the dialogue when the player presses a key. Create a new file named multi-text.py and add the code with the updates below:
# Bên trong class Game class Game(arcade.Window): def __init__(self): # . self.dialogue_active = False self.dialogue_messages = [ "Hello, player! How are you?", "Nice weather today, isn't it?", "Beware of the enemies lurking ahead!" ] self.dialogue_index = 0 def on_mouse_press(self, x, y, button, modifiers): index = (self.dialogue_index + 1) % len(self.dialogue_messages) if self.dialogue_active: if button == arcade.MOUSE_BUTTON_LEFT: self.dialogue_index = index def draw_dialogue_box(self): # . arcade.draw_text("Next", SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 50, black, font_size=16, anchor_x="center", anchor_y="center")
Add the on_mouse_press method to the Game class . When the dialogue is active and you press the left mouse button, this method increases dialogue_index to show the next message in the dialogue_messages list . % len(self.dialogue_messages) ensures this index wraps around the beginning of the list when the end is reached.
Includes additional features
To enhance the conversation system, you can consider adding the following features:
- Multi-selection allows the player to influence the outcome of a conversation or make decisions that affect the course of the game.
- Combined with character portraits helps players identify the speaker and add depth to the story.
- Text effects make conversations more engaging.
- Sound effects for more vivid game experience.
Above is how to add a dialogue system to the game using the Arcade library in Python . Hope the article is useful to you.
You should read it
- How to use sprites in Arcade to develop games
- How to Add Randomly Moving Objects Using Python's Arcade Library
- How to Create Power-Ups and Collections in Pygame
- How to create scrolling backgrounds in Pygame
- How to set up Python to program on WSL
- What is Python? Why choose Python?
- Easier game development with Pygame Zero
- Why should you learn Python programming language?
- 5 choose the best Python IDE for you
- How to make Hangman game in Python
- More than 100 Python exercises have solutions (sample code)
- Top 10 best games on Apple Arcade
Maybe you are interested
The Easiest Way to Motivate Yourself to Clean
Top 10 best quality selfie apps for Android
Quickly and effectively fix your computer's self-refresh
Elon Musk posted a photo of a supercomputer running a self-designed chip
Shudder at humanity's 'last selfies' predicted by AI
How to send self-deleted photos on Telegram without secret chat