https://www.youtube.com/watch?v=1a4azbbcngk
In this tutorial, I will guide you by making a word -making game like a wrand, such as using Azgar. Although many coding projects are focused on data analysis, game construction is not only fun but also helps strengthen basic programming concepts in creative contexts. Game Development is especially perfect for practicing object -based programming, logically thinking and creating interactive user experiences.
By the end of this tutorial, you have made a complete functional word evaluation game where:
- Players try to estimate a mystery word within a limited number of attempts
- The correct posts in the correct position come forward
- Players get opinions in wrong places and in the wrong posts
- The game tracks and tracks the terms of loss
Will you learn
Through this project, you will practice:
- To work with the built -in modules of Azgar
random
- Usage of File I/O to read external data
- To make game loops and manage them
- Implementing conditional logic for game rules
- Handling the user’s input and providing feedback
- Creating a text -based user interface
Before you start: Pre -instruction
To take the maximum of this project, follow these initial steps:
- Review the project
Get access to the project and familiarize yourself with goals and structures: Word Assessment Game Project. - Prepare your environment
- If you are using Data Quest PlatformEverything has already been arranged for you.
- If you are working locally, make sure you have azagar and Gapter notebook installed.
- Stay comfortably with the Jupiter
- New in Mark Dowan? We recommend learning the basics to format the header and adding context to your Japter notebook: Mark Dowan Guide.
- File Sharing and Project Uploads LEB, Create a Gut Hub Account Before Webnar: Sign up on Gut Hub.
To set your environment
Working with the Gapter Notebook
We will use the Gapter notebook for this project, which allows us to write and process the code in the interactive environment. If you are new to Jupyter, here’s a quick review:
- Cells: Gapter notebooks contain cells that contain a code or mark down (formed text).
- Cells operators: IT to perform a cell, select it and press the shift+inter.
- Cell types:
- The code seal: Where you write and process the codes
- Mark Down Cell: Where you write formed text (like this tutorial)
The project resources
LOYLLY of this project, you will need:
- A text file that has the words of your game (we will provide a simple version)
- Gapter Notebook to write your Game Code
Let’s make the desired files:
Create a new file that says
words.txt
And add words (one line one line). Here is the sample list we will use in this walkthrough:bears kings helps learn queen twist happy
Create a new Gapter Notebook whose name is
Word_Raider.ipynb
With the establishment of our environment, we are almost ready to start building our game, but first, let’s understand some important concepts.
Understand the game loops
An important concept of this project is a game loop, which is mostly fundamental for interactive applications and sports. Let’s get broken how it works:
- The state of the loop: Our loop continues till this time
used_turns < max_turns
When the player gets out of the twist, the game stops. - Input step: We collect and verify the player input at the beginning of every loop repetition.
- Update phase: We guess, update your game estate (tracking right, wrong and wrong letters).
- Rander Step: We show the current state of the game (partially revealed words, tracking letters, and remaining).
- Win/loss checkAfter taking any estimation, we check whether the player has won or lost, and if any condition has been met, break the loop.
Input → Update → Rader → Check Game Estate’s sample is common in multiple types of sports, such as simple text -based games like ours to complicated 3D video games.
Now, when we understand these key ideas, let’s start with this project.
Implementation of the project
Step 1: Importing desired libraries
Let’s start by importing random
The module, which will allow us to select a word from our Word Bank:
import random
Learning Insight: Always keep your imported statements in the upper part of your code. This makes your dependence on everyone who reads your code and ensures that they are available in your program.
Step 2: Creating a global variable
Next, let’s explain some global variables that will be used throughout our game:
game_name = "Word Raider"
word_bank = ()
Here we are:
- Sort our game name on “Word Rider” (Select your name without hesitation!)
- Make an empty list that will store all possible words for our game
Step 3: Load the word bank
Now, let’s load your word list from the text file we’ve created before:
with open("words.txt") as word_file:
for line in word_file:
word_bank.append(line.rstrip().lower())
# Verify our word bank loaded correctly
word_bank
Output:
('bears', 'kings', 'helps', 'learn', 'queen', 'twist', 'happy')
Learning insights: the use of context manager (
with
Statement is the proposed method of opening files in Azar. This ensures that the file is closed properly after use, no matter what error..rstrip()
The procedure left behind the White Space (such as a new line character), and.lower()
All the words of consistency make all the words smaller standard.
Step 4: Choosing the target word
With our Word Bank’s population, we can select a word in order to assess the player:
selected_word = random.choice(word_bank)
The vision of learning: gave
random.choice()
The function chooses a random item in a sequence (in this case, our list of words). This introduces the change in our game, making sure that every time the game is played, a random word is selected, which increases the replay value.
Step 5: compiling game information
Before we start the main game loop, let’s explain some variables to track the game estate:
# Defining game information
incorrect_letters = ()
misplaced_letters = ()
max_turns = 6
used_turns = 0
These variables will help us track:
- Letters guessed that the word is not in
incorrect_letters
Jes - Letters guess they are in the word but are in the wrong position (
misplaced_letters
Jes - Permitted maximum number of expectations (
max_turns
Jes - The player has used how many styles have now (
used_turns
Jes
Step 6: Creating a User Interface
Let the player provide some preliminary information to the player:
print(f"Welcome to {game_name}!")
print(f"The word to guess has {len(selected_word)} letters.")
print(f"You have {max_turns} turns to guess the word!")
Output:
Welcome to Word Raider!
The word to guess has 5 letters.
You have 6 turns to guess the word!
Learning insights: F -string (Formated String Literals) is an easy way to express inside the wire literature. They start from a ‘f’ and use curly curiosity
{}
Adding variable values ​​or expression to your wires. The F -strings were introduced in Azar 3.6 and they are more readable and faster than the old string formatting methods.
Step 7: Building Central Game Loop
We are now ready to make our game the main center of game: the central loop that will handle the player input, game logic, and the terms of win/loss.
Let’s start with the infrastructure that handles the user’s input:
# Main game loop
while used_turns < max_turns:
guess = input("Guess a word (or type 'stop' to end game):").lower().strip()
if guess == "stop":
break
This makes a loop that continues until the player uses all his turns. We also provide the option to escape by allowing the player to type “stop” to finish the game at any time.
Learning insights: When using
while
Loops, it is important to have a clear condition that will eventually becomeFalse
(In this case,used_turns < max_turns
) It is also a good process to manually break the loop manually to prevent the game from running indefinitely.
Step 8: Verify the user input
Next, let’s add verification to ensure that the player’s guess is a valid word of the right length:
# Main game loop with input validation
while used_turns < max_turns:
guess = input("Guess a word (or type 'stop' to end game):").lower().strip()
if guess == "stop":
break
if len(guess) != len(selected_word) or not guess.isalpha():
print(f"Please enter a {len(selected_word)} letter word.")
continue
The vision of learning: gave
isalpha()
The method checks if a string only contains alphabetical characters (AZ or AZ). This verification ensures that the players insert the actual words of the right length (numbers, places, or special letters).continue
The statement leaves the rest of the current loop repetition and begins the next start, effectively seeking a new guess.
Step 9: Preliminary letter processing
Now, let’s add basic logic to estimate the player’s estimates against the target word:
# Main game loop with basic letter processing
while used_turns < max_turns:
guess = input("Guess a word (or type 'stop' to end game):").lower().strip()
if guess == "stop":
break
if len(guess) != len(selected_word) or not guess.isalpha():
print(f"Please enter a {len(selected_word)} letter word.")
continue
index = 0
for letter in guess:
if letter == selected_word(index):
print(letter, end=' ')
elif letter in selected_word:
misplaced_letters.append(letter)
print("_", end=' ')
else:
incorrect_letters.append(letter)
print("_", end=' ')
index += 1
print("\n")
print(f"Misplaced letters: {misplaced_letters}")
print(f"Incorrect letters: {incorrect_letters}")
This basic implementation checks each letter in estimates and:
- If it is in the right position shows the letter
- Adds it to the wrong places and if it is in the word but in the wrong position shows the under -score
- Adds it to the wrong posts and if this word is not at all shows the under -score
Learning Insight: We use indexing to compare the roles to the same position in both strings. The index monitors our position in the word variable target when we guess. This is a common technique to compare the elements in the respective positions in different streams.
However, you may find a problem when running this code. If a player’s estimation appears several times, it will be included several times in our tracking lists. Let’s fix it.
Step 10: Damnate with duplicate posts
Improve our game impression system L, we need to handle duplicate posts more intelligently:
# Accounting for duplicates in misplaced/incorrect letter lists
while used_turns < max_turns:
guess = input("Guess a word (or type 'stop' to end game):").lower().strip()
if guess == "stop":
break
if len(guess) != len(selected_word) or not guess.isalpha():
print(f"Please enter a {len(selected_word)} letter word.")
continue
index = 0
for letter in guess:
if letter == selected_word(index):
print(letter, end=' ')
if letter in misplaced_letters:
misplaced_letters.remove(letter)
elif letter in selected_word:
if letter not in misplaced_letters:
misplaced_letters.append(letter)
print("_", end=' ')
else:
if letter not in incorrect_letters:
incorrect_letters.append(letter)
print("_", end=' ')
index += 1
print("\n")
print(f"Misplaced letters: {misplaced_letters}")
print(f"Incorrect letters: {incorrect_letters}")
Here are the key improvements:
Removing letters from
misplaced
When they are found in the correct position:
If a player first estimates a letter in the wrong place and then in the right position, we remove it from the Lordmisplaced_letters
The list of the list is no longer the wrong place.if letter == selected_word(index): print(letter, end=' ') if letter in misplaced_letters: misplaced_letters.remove(letter)
Prevent duplicate entries in our tracking lists:
We just add a letter to our tracking lists if it does not already exist, keeping our impressions clean and easy to read.elif letter in selected_word: if letter not in misplaced_letters: misplaced_letters.append(letter)
Learning Insight: State Management is a key component of interactive applications. As our game develops, we have to make sure that our tracking data is accurate and helpful. Such a state administration is a valuable process of more complex programming works where permanent data needs to be maintained.
Step 11: To include winning and loss conditions
Finally, let’s complete your game by adding the terms of win and loss:
# Complete game loop with win/loss conditions
while used_turns < max_turns:
guess = input("Guess a word (or type 'stop' to end game):").lower().strip()
if guess == "stop":
break
if len(guess) != len(selected_word) or not guess.isalpha():
print(f"Please enter a {len(selected_word)} letter word.")
continue
index = 0
for letter in guess:
if letter == selected_word(index):
print(letter, end=' ')
if letter in misplaced_letters:
misplaced_letters.remove(letter)
elif letter in selected_word:
if letter not in misplaced_letters:
misplaced_letters.append(letter)
print("_", end=' ')
else:
if letter not in incorrect_letters:
incorrect_letters.append(letter)
print("_", end=' ')
index += 1
print("\n")
# Win condition
if guess == selected_word:
print("\nCongratulations you guessed the word!")
break
used_turns += 1
# Loss condition
if used_turns == max_turns:
print(f"\nGame over, you lost. The word was: {selected_word}")
break
print(f"Misplaced letters: {misplaced_letters}")
print(f"Incorrect letters: {incorrect_letters}")
print(f"You have {max_turns - used_turns} turns left.")
Learning insights: must test the state of win before increased
used_turns
. This ensures that the player wins immediately when they make proper guesses, without unnecessarily counting this estimate like a turning point.
The full game
Let’s put them all together in the same code block for ease of implementation:
import random
# Set up game variables
game_name = "Word Raider"
word_bank = ()
# Load words from file
with open("words.txt") as word_file:
for line in word_file:
word_bank.append(line.rstrip().lower())
# Select target word
selected_word = random.choice(word_bank)
# Initialize game state
incorrect_letters = ()
misplaced_letters = ()
max_turns = 6
used_turns = 0
# Display game introduction
print(f"Welcome to {game_name}!")
print(f"The word to guess has {len(selected_word)} letters.")
print(f"You have {max_turns} turns to guess the word!")
# Main game loop
while used_turns < max_turns:
guess = input("Guess a word (or type 'stop' to end game):").lower().strip()
if guess == "stop":
break
if len(guess) != len(selected_word) or not guess.isalpha():
print(f"Please enter a {len(selected_word)} letter word.")
continue
index = 0
for letter in guess:
if letter == selected_word(index):
print(letter, end=' ')
if letter in misplaced_letters:
misplaced_letters.remove(letter)
elif letter in selected_word:
if letter not in misplaced_letters:
misplaced_letters.append(letter)
print("_", end=' ')
else:
if letter not in incorrect_letters:
incorrect_letters.append(letter)
print("_", end=' ')
index += 1
print("\n")
# Win condition
if guess == selected_word:
print("\nCongratulations you guessed the word!")
break
used_turns += 1
# Loss condition
if used_turns == max_turns:
print(f"\nGame over, you lost. The word was: {selected_word}")
break
print(f"Misplaced letters: {misplaced_letters}")
print(f"Incorrect letters: {incorrect_letters}")
print(f"You have {max_turns - used_turns} turns left.")
Playing the game
An example of a gameplay session is:
Welcome to Word Raider!
The word to guess has 5 letters.
You have 6 turns to guess the word!
Guess a word (or type 'stop' to end game): hellooo
Please enter a 5 letter word.
Guess a word (or type 'stop' to end game): hello
h _ l _ _
Misplaced letters: ('l')
Incorrect letters: ('e', 'o')
You have 5 turns left.
Guess a word (or type 'stop' to end game): helps
h e l p s
Congratulations you guessed the word!
Next steps and increases
Now that you have made a working -word assessment game, some ideas to enhance and enhance it are:
- The length of the variable word: Edit your game to work with words of different lengths.
- Hard mode: Implement a waver style “hard mode” where letters that are properly estimated should be used in subsequent estimates.
- Verification of the dictionaryOnly accept the estimates that are real words by testing against the dictionary.
- Custom Word Category: Allow players to select Word categories (animals, countries, etc.).
- Graphical user interface: Use a library like a library to create a visual interface.
- Scoring System: Add the scoring system based on how quickly the player estimates the word.
Conclusion
In this tutorial, we have made a literal style game from the beginning using Azgar. On the way, we have followed the key concepts of programming that include:
- Using the built -in modules of Azigar
- File I/O operations
- String diamond
- Loops and to use conditional
- Input verification and processing
- Game State Management
Game Development is a great way to improve your programming skills, while creating some entertainment and interactive. The concepts you have learned here, especially the state management and game loops, create the basis for many other sports and applications.
If you are new to the tiger or looking for some concepts in challenging this tutorial, our basics for the Data Enliosis course will help you learn the basic skills needed for this project. This course covers essential titles such as loops, conditional, and string manipulation we have used widely in this game. Once you are pleased with these concepts, come back to create your Word Rider game and counter the challenges of growing!