Pygame中Trivia游戏解析6-4

3.3.3 显示题目选项

在显示题目选项时,有三种情况:分别是用户还未选择答案时;用户的答案是正确时;用户的答案是错误时。

(1)用户还未选择答案时

此时,用白色显示四个备选答案,如图4①所示。

图4 题目选项

其对应的代码如下所示。

python 复制代码
print_text(font1, 5, 170, "ANSWERS")
print_text(font2, 20, 210, "1 - " + self.data[self.current+1], self.colors[0])
print_text(font2, 20, 240, "2 - " + self.data[self.current+2], self.colors[1])
print_text(font2, 20, 270, "3 - " + self.data[self.current+3], self.colors[2])
print_text(font2, 20, 300, "4 - " + self.data[self.current+4], self.colors[3])

其中,self.current是题目内容对应的行数,题目的四个选项分别在该行之后的1-4行。

(2)用户的答案是正确时

当用户的答案是正确时,用绿色字体显示该选项,并且会有绿色字体的提示,如图5①和图5②所示。

图5 答案正确时

通过一下代码实现该功能。

python 复制代码
if self.scored:
    self.colors = [white,white,white,white]
    self.colors[self.correct-1] = green
    print_text(font1, 230, 380, "CORRECT!", green)
    print_text(font2, 170, 420, "Press Enter For Next Question", green)

其中,scored是True时表示答案正确,此时将colors的第self.correct项的颜色变为绿色。变量correct的值是通过如下代码获得的。

self.correct = int(self.data[self.current+5])

其中,current表示当前题目内容在data中的行数,接下来的4行是题目的选项,之后的第5行即为正确答案,如图6所示。

图6 题目格式

(3)用户答案是错误时

当用户的答案是正确时,用绿色字体显示正确选项,红色字体显示用户选择的错误选项,并且会有红色字体的提示,如图7①和图7②所示。

图7 答案错误时

通过一下代码实现该功能。

python 复制代码
elif self.failed:
    self.colors = [white,white,white,white]
    self.colors[self.wronganswer-1] = red
    self.colors[self.correct-1] = green
    print_text(font1, 220, 380, "INCORRECT!", red)
    print_text(font2, 170, 420, "Press Enter For Next Question", red)

当failed是True时,表示答案错误,wronganser表示错误答案,correct表示正确答案。

相关推荐
技术无疆1 天前
【Python】Uvicorn:Python 异步 ASGI 服务器详解
运维·服务器·开发语言·网络·python·pygame·python3.11
不写八个2 天前
Python编写的贪吃蛇小游戏
开发语言·python·pygame
@技术无疆5 天前
【Python】Flask-Admin:构建强大、灵活的后台管理界面
开发语言·后端·python·oracle·flask·pip·pygame
@技术无疆7 天前
【Python】FeinCMS:轻量级且可扩展的Django内容管理系统
数据库·python·django·sqlite·pip·pygame·httpx
api茶飘香10 天前
如何根据拍立淘API返回值进行商品数据分析
python·数据挖掘·数据分析·django·virtualenv·pygame·tornado
技术无疆13 天前
【Python】Tartiflette:用 Python 实现的 GraphQL 服务器
服务器·开发语言·后端·python·pygame·graphql·python3.11
技术无疆14 天前
【Python】探索Magenta:音乐与艺术的机器智能创作
开发语言·人工智能·python·scikit-learn·pip·pygame·python3.11
技术无疆15 天前
【Python】Anaconda插件:Sublime Text中的Python开发利器
ide·python·编辑器·pip·pygame·sublime text·python3.11
IT小辉同学15 天前
用 Pygame 实现一个乒乓球游戏
python·游戏·pygame
小泽耳机数码推荐家18 天前
百元级蓝牙耳机推荐有哪些?四大优选品牌衷心推荐
python·django·virtualenv·pygame