用python编写一个闯关游戏
时间: 2023-09-27 19:09:24 浏览: 48
好的,下面是一个简单的闯关游戏的 Python 代码示例,供您参考。
```python
import time
# 定义关卡
level_1 = "level 1: What is the capital city of France?"
level_2 = "level 2: What is the largest mammal in the world?"
level_3 = "level 3: What is the highest mountain in the world?"
# 定义答案
answer_1 = "Paris"
answer_2 = "Blue whale"
answer_3 = "Mount Everest"
# 欢迎语
print("Welcome to the game! There are three levels to pass.")
# 进入关卡 1
print(level_1)
time.sleep(1)
user_input = input("Please enter your answer: ")
if user_input == answer_1:
print("Congratulations! You have passed level 1.")
else:
print("Sorry, your answer is incorrect.")
exit()
# 进入关卡 2
print(level_2)
time.sleep(1)
user_input = input("Please enter your answer: ")
if user_input == answer_2:
print("Congratulations! You have passed level 2.")
else:
print("Sorry, your answer is incorrect.")
exit()
# 进入关卡 3
print(level_3)
time.sleep(1)
user_input = input("Please enter your answer: ")
if user_input == answer_3:
print("Congratulations! You have passed level 3.")
else:
print("Sorry, your answer is incorrect.")
exit()
# 通关
print("Congratulations! You have passed all levels and won the game!")
```
这个游戏有三个关卡,分别需要回答问题才能通过。您可以根据自己的需求和喜好来修改这个代码示例。