如何用Python制作支持用户输入而非随机的笼斗模拟程序
笼斗模拟:手动选择招式实现方案
招式与克制关系
- Punch(直拳):克制 Elbow(肘击)
- Kick(踢腿):克制 Punch(直拳)
- Body Shot(躯干击打):克制 Kick(踢腿)
- Take Down(抱摔):克制 Body Shot(躯干击打)
- Pinned Against Cage(笼边压制):克制 Take Down(抱摔)
- Clinch(缠抱):克制 Pinned Against Cage(笼边压制)
- Elbow(肘击):克制 Clinch(缠抱)
完整实现代码
import random # 定义招式列表和克制关系字典 MOVES = ["Punch", "Kick", "Body Shot", "Take Down", "Pinned Against Cage", "Clinch", "Elbow"] COUNTERS = { "Punch": "Elbow", "Kick": "Punch", "Body Shot": "Kick", "Take Down": "Body Shot", "Pinned Against Cage": "Take Down", "Clinch": "Pinned Against Cage", "Elbow": "Clinch" } def get_player_move(): """获取玩家输入的招式,做输入验证""" while True: print("\n可用招式:") for idx, move in enumerate(MOVES, 1): print(f"{idx}. {move}") choice = input("请选择你的招式(输入序号或招式名称):").strip() # 处理序号输入 if choice.isdigit(): idx = int(choice) - 1 if 0 <= idx < len(MOVES): return MOVES[idx] else: print("无效序号,请重新输入") # 处理名称输入 elif choice in MOVES: return choice else: print("无效招式名称,请重新输入") def determine_winner(player_move, cpu_move): """判断胜负:玩家招式克制CPU则赢,反之则输,平局则继续""" if COUNTERS[player_move] == cpu_move: return "你赢了这一回合!" elif COUNTERS[cpu_move] == player_move: return "CPU赢了这一回合!" else: return "平局,再来一回合!" def main(): print("欢迎来到笼斗模拟游戏!") print("规则:选择克制对手的招式即可获胜,具体克制关系见上方") while True: player_move = get_player_move() cpu_move = random.choice(MOVES) print(f"\n你选择了:{player_move}") print(f"CPU选择了:{cpu_move}") result = determine_winner(player_move, cpu_move) print(result) play_again = input("\n是否继续游戏?(y/n):").strip().lower() if play_again != 'y': print("游戏结束!") break if __name__ == "__main__": main()
代码说明
- 输入验证:支持输入招式序号或名称,避免无效输入导致报错
- 克制逻辑:通过
COUNTERS字典存储每个招式能克制的目标,判断时直接比对即可 - 循环对战:每回合结束后可选择继续或退出,提升游戏体验
内容的提问来源于stack exchange,提问作者Loki
相关产品推荐
相关产品推荐

