
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境設定
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - match-case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表項
- Python - 修改列表項
- Python - 新增列表項
- Python - 刪除列表項
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組項
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合項
- Python - 新增集合項
- Python - 刪除集合項
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典項
- Python - 修改字典項
- Python - 新增字典項
- Python - 刪除字典項
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列項
- Python - 新增陣列項
- Python - 刪除陣列項
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝器類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 程式碼塊
- Python - try-finally 程式碼塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 程式碼塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 合併執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
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”按鈕。如果要清除所有塊,則可以按“清除”按鈕;否則,不要按它。
點選“生成 Mad Libs”後

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