- 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
广告