Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python Mad Libs Generator Game



The Python Mad Libs Generator Game is an application which creates and shows stories with funny words and phrases from the inputs of user. The graphic user interface of this game was developed using Tkinter while sound effects for the game are from Pygame, making the game attractive to any user who is a fun of creative writing or playful narratives.

Features and Functionalities Explanation

  • GUI Elements − It uses Tkinter for the development of the user interface with entry labels for entering inputs such as celebrity, colour, animal, clothing, food, place, and a verb that ends in -ing. These inputs are employed to take blanks of a Mad Libs story.
  • Sound Effects − Pygame is combined to play sound (“click. wav”) each time the user pushes the “Generate Mad Libs” button to have better interactivity.
  • Story Generation − When the user clicks on ‘Generate Mad Libs’ button, the application stores the inputs, create a story from these inputs and lastly displays in message box.
  • Clear Inputs − A ‘Clear’ button is present to clear all the entries made by the users, to enable them to begin anew.

Key Functions

  • play_sound() − Manages the loading and playing of a sound affect across a Pygame program. As it tries to load and play the ‘click. wav’ and there is an exception if the file is not found.
  • generate_madlib() − Reads user inputs, generates a text for a specific and unique Mad Libs and prints them on a message box through Tkinter package. It also switches on the sound effect.
  • clear_inputs() − All entry fields are rested to the initial blank state so that users can easily clear their entry inputs.

Installation

First, we have to install pygame. Tkinter is included with Python, so we don't need to install it separately.

Here is the syntax to install pygame using pip

pip install pygame

Python Code for Mad Libs Generator Game

import tkinter as tk
from tkinter import messagebox
import pygame
import os

# Set the working directory to where your script is located
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Initialize pygame mixer for sound effects
pygame.mixer.init()

# Function to play a sound
def play_sound():
   try:
      pygame.mixer.music.load("click.wav")
      pygame.mixer.music.play()
   except pygame.error:
      print("Sound file not found. Continuing without sound.")

# Function to create and display a Mad Libs story
def generate_madlib():
   play_sound()  # Play sound effect on button click
    
   # Collect inputs from the user
   celebrity = celebrity_entry.get()
   color = color_entry.get()
   animal = animal_entry.get()
   clothing = clothing_entry.get()
   food = food_entry.get()
   place = place_entry.get()
   verb_ing = verb_ing_entry.get()

   # Create the story
   story = (f"One day, {celebrity} decided to wear a {color} {clothing} and take their pet {animal} to {place}. "
      f"While there, they had an unexpected encounter and ended up {verb_ing} with a tasty {food}. "
      f"It turned out to be an unforgettable adventure, filled with excitement and {verb_ing}!")

    
   # Display the story in a message box
   messagebox.showinfo("Your Mad Libs Story", story)

# Function to clear all inputs
def clear_inputs():
   celebrity_entry.delete(0, tk.END)
   color_entry.delete(0, tk.END)
   animal_entry.delete(0, tk.END)
   clothing_entry.delete(0, tk.END)
   food_entry.delete(0, tk.END)
   place_entry.delete(0, tk.END)
   verb_ing_entry.delete(0, tk.END)

# Set up the main Tkinter window
root = tk.Tk()
root.title("Mad Libs Game")

# Create input labels and entry widgets
tk.Label(root, text="Celebrity:").grid(row=0, column=0, padx=10, pady=5)
celebrity_entry = tk.Entry(root)
celebrity_entry.grid(row=0, column=1, padx=10, pady=5)

tk.Label(root, text="Color:").grid(row=1, column=0, padx=10, pady=5)
color_entry = tk.Entry(root)
color_entry.grid(row=1, column=1, padx=10, pady=5)

tk.Label(root, text="Animal:").grid(row=2, column=0, padx=10, pady=5)
animal_entry = tk.Entry(root)
animal_entry.grid(row=2, column=1, padx=10, pady=5)

tk.Label(root, text="Clothing:").grid(row=3, column=0, padx=10, pady=5)
clothing_entry = tk.Entry(root)
clothing_entry.grid(row=3, column=1, padx=10, pady=5)

tk.Label(root, text="Food:").grid(row=4, column=0, padx=10, pady=5)
food_entry = tk.Entry(root)
food_entry.grid(row=4, column=1, padx=10, pady=5)

tk.Label(root, text="Place:").grid(row=5, column=0, padx=10, pady=5)
place_entry = tk.Entry(root)
place_entry.grid(row=5, column=1, padx=10, pady=5)

tk.Label(root, text="Verb (ending in -ing):").grid(row=6, column=0, padx=10, pady=5)
verb_ing_entry = tk.Entry(root)
verb_ing_entry.grid(row=6, column=1, padx=10, pady=5)

# Create buttons to generate the story and clear inputs
generate_button = tk.Button(root, text="Generate Mad Libs", command=generate_madlib)
generate_button.grid(row=7, column=0, columnspan=2, pady=10)

clear_button = tk.Button(root, text="Clear", command=clear_inputs)
clear_button.grid(row=8, column=0, columnspan=2, pady=5)

# Start the Tkinter event loop
root.mainloop()

Output

Mad Libs Game

After filling up the blanks

Mad Libs Game

Then we will click the Generate Mab Libs button. If you want to clear all the blocks, then you can press the clear button; otherwise, dont press it.

After click Generate Mab Libs

Mad Libs Game

The Python Mad Libs Generator Game is an entertaining application that at the same time uses narrative elements as well as having a very basic GUI. Using Tkinter for the graphical user interface and Pygame for sound effects, users get a pleasant interface they can use to generate and share their own Mad Libstories. The application is very easy to implement and as such would serve well as a training project for Python and GUI development.

python_projects_from_basic_to_advanced.htm
Advertisements