Python Mad Libs 生成器遊戲



Python Mad Libs 生成器遊戲是一個建立並顯示包含使用者輸入的有趣單詞和短語的故事的應用程式。此遊戲的圖形使用者介面使用 Tkinter 開發,而遊戲的聲音效果來自 Pygame,使遊戲對任何喜歡創意寫作或趣味敘事的使用者都很有吸引力。

功能特性說明

  • GUI 元素 - 它使用 Tkinter 開發使用者介面,並帶有用於輸入輸入的條目標籤,例如名人、顏色、動物、服裝、食物、地點和以“-ing”結尾的動詞。這些輸入用於填寫 Mad Libs 故事的空白處。
  • 聲音效果 - Pygame 結合使用,每次使用者按下“生成 Mad Libs”按鈕時播放聲音(“click.wav”),以獲得更好的互動性。
  • 故事生成 - 當用戶點選“生成 Mad Libs”按鈕時,應用程式會儲存輸入,根據這些輸入建立故事,最後在訊息框中顯示。
  • 清除輸入 - 提供了一個“清除”按鈕來清除使用者輸入的所有條目,使他們能夠重新開始。

關鍵函式

  • play_sound() - 管理 Pygame 程式中聲音效果的載入和播放。因為它嘗試載入並播放“click.wav”,如果找不到檔案,則會引發異常。
  • generate_madlib() - 讀取使用者輸入,為特定且唯一的 Mad Libs 生成文字,並透過 Tkinter 包將其列印在訊息框上。它還會開啟聲音效果。
  • clear_inputs() - 所有輸入欄位都重置為初始空白狀態,以便使用者可以輕鬆清除其輸入。

安裝

首先,我們必須安裝 pygame。Tkinter 包含在 Python 中,因此我們無需單獨安裝它。

以下是使用 pip 安裝 **pygame** 的語法 -

pip install pygame

Mad Libs 生成器遊戲的 Python 程式碼

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()

輸出

Mad Libs Game

填寫空白後

Mad Libs Game

然後,我們將點選“生成 Mad Libs”按鈕。如果要清除所有塊,則可以按“清除”按鈕;否則,不要按它。

點選“生成 Mad Libs”後

Mad Libs Game

Python Mad Libs 生成器遊戲是一個娛樂應用程式,同時使用敘事元素並具有非常基本的 GUI。使用 Tkinter 作為圖形使用者介面和 Pygame 作為聲音效果,使用者可以獲得一個愉快的介面,他們可以使用該介面生成和分享自己的 Mad Libs 故事。該應用程式非常易於實現,因此可以作為 Python 和 GUI 開發的培訓專案。

python_projects_from_basic_to_advanced.htm
廣告