Python Tutorial: Game Examples šŸŽÆ

beginner
14 min

Python Tutorial: Game Examples šŸŽÆ

Welcome to the exciting world of Python programming, where we'll be learning to create games! In this tutorial, we'll explore various game examples that will help you understand the concepts from the ground up.

Getting Started šŸ“

Before we dive into the games, let's make sure you have Python installed on your computer. You can download it from official Python website.

Once Python is installed, you can run the code in this tutorial by creating a new .py file, copying the code snippets, and pasting them into the file.

Tic-Tac-Toe Game šŸŽ²

Let's start with a classic game, Tic-Tac-Toe. This game will help you understand user input, loops, and basic game logic.

python
# Tic Tac Toe game in Python def print_board(board): for row in board: print(" | ".join(row)) print("-" * 9) def check_winner(board, player): win_conditions = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ] for condition in win_conditions: if all([board[i] == player[0] for i in condition]): return True return False def main(): board = [" "] * 9 players = [("X", "O")] current_player = 0 while True: print_board(board) move = input(f"Player {players[current_player][1]}'s turn. Enter the position (1-9): ") if move.isdigit() and int(move) in range(1, 10) and board[int(move) - 1] == " ": board[int(move) - 1] = players[current_player] if check_winner(board, players): print_board(board) print(f"Congratulations, Player {players[current_player][0]} wins!") break current_player = (current_player + 1) % 2 else: print("Invalid input. Try again.") if __name__ == "__main__": main()

šŸ’” Pro Tip: Try running the Tic-Tac-Toe game and experiment with it to understand how user input, loops, and game logic work.

Quick Quiz
Question 1 of 1

What is the purpose of the `print_board` function in the Tic-Tac-Toe game?

Hangman Game 🧩

Next, we'll create a Hangman game, which will help you learn about strings, lists, and conditional statements.

python
# Hangman game in Python import random def get_random_word(word_list): return random.choice(word_list) def is_guessed(word, guessed_letters): return all(letter in guessed_letters for letter in word) def get_available_letters(word, guessed_letters): letters = set(word) for letter in guessed_letters: letters.discard(letter) return list(letters) def hangman(word, guessed_letters, lives=6): print(f"_" * len(word)) while lives > 0 and not is_guessed(word, guessed_letters): available_letters = get_available_letters(word, guessed_letters) guess = input("Guess a letter: ").lower() if guess in guessed_letters or guess not in available_letters: print(f"Invalid input or letter already guessed. Lives left: {lives}") continue guessed_letters.add(guess) if guess in word: print("Correct guess!") word_as_list = list(word) indices = [i for i, letter in enumerate(word_as_list) if letter == guess] for index in indices: word_as_list[index] = guess print("".join(word_as_list)) else: print(f"Incorrect guess. Lives left: {lives - 1}") lives -= 1 if lives == 0: print("You lost! The word was:", word) if is_guessed(word, guessed_letters): print("Congratulations! You won!") word_list = ["python", "java", "javascript", "ruby", "csharp", "php"] word = get_random_word(word_list) lives = 6 guessed_letters = set() hangman(word, guessed_letters, lives)

šŸ’” Pro Tip: Experiment with the Hangman game to understand how strings, lists, conditional statements, and functions work together in a real-world application.

Quick Quiz
Question 1 of 1

What is the purpose of the `get_available_letters` function in the Hangman game?

Happy coding, and remember to keep learning and practicing! šŸŽ‰