吃豆人大作战
用布尔逻辑实现经典街机游戏**吃豆人(Pac-Man)**的规则判定!你将学习 Python 的 True/False 和 and/or/not 运算符。
1. eat_ghost — 能否吃鬼
创建 eat_ghost(power_pellet_active, touching_ghost) 函数,接收两个布尔参数:
power_pellet_active— 大力丸是否生效touching_ghost— 是否碰到了鬼
有大力丸且碰到鬼 → 能吃鬼。返回一个布尔值。
>>> eat_ghost(True, True)
True
>>> eat_ghost(False, True)
False
2. score — 是否得分
创建 score(touching_power_pellet, touching_dot) 函数,接收两个布尔参数:
touching_power_pellet— 是否碰到能量豆touching_dot— 是否碰到普通豆
碰到能量豆或普通豆 → 得分。
>>> score(False, True)
True
>>> score(False, False)
False
3. lose — 是否输了
创建 lose(power_pellet_active, touching_ghost) 函数,接收两个布尔参数:
power_pellet_active— 大力丸是否生效touching_ghost— 是否碰到了鬼
没有大力丸且碰到鬼 → 输了。
>>> lose(False, True)
True
>>> lose(True, True)
False
4. win — 是否赢了
创建 win(has_eaten_all_dots, power_pellet_active, touching_ghost) 函数,接收三个参数:
has_eaten_all_dots— 是否吃完了所有豆子power_pellet_active— 大力丸是否生效touching_ghost— 是否碰到了鬼
吃完了所有豆子且没有输 → 赢。你需要调用 lose() 函数来判断是否输了。
>>> win(True, False, False)
True
>>> win(True, False, True)
False
你将学到的知识
| 概念 | 对应函数 |
|---|---|
and(与)— 两个条件都满足 | eat_ghost |
or(或)— 任一条件满足 | score |
not(非)— 取反 | lose |
| 函数调用函数(组合) | win 调用 lose |