用python写个黄金矿工
时间: 2023-11-19 09:45:34 浏览: 162
好的,以下是一个简单的 Python 黄金矿工游戏示例代码:
``` python
import random
# 定义黄金矿块的名称和价值
GOLD_BLOCKS = {
'small': 2,
'medium': 5,
'large': 10
}
# 游戏文本内容
WELCOME_MESSAGE = "欢迎来到黄金矿工游戏!"
GOOD_LUCK_MESSAGE = "祝你好运!"
SCORE_MESSAGE_PREFIX = "你当前的得分是:"
GAME_OVER_MESSAGE_PREFIX = "你的得分是:"
GAME_OVER_MESSAGE_SUFFIX = "\n游戏结束!"
# 定义黄金矿工游戏类
class GoldMiner:
def __init__(self):
self.score = 0
# 开始游戏
def start_game(self):
print(WELCOME_MESSAGE)
print(GOOD_LUCK_MESSAGE)
while True:
choice = input("你要继续挖矿吗?(y/n) ")
if choice.lower() == 'y':
self.mine_gold()
else:
break
print(GAME_OVER_MESSAGE_PREFIX + str(self.score) + GAME_OVER_MESSAGE_SUFFIX)
# 挖矿
def mine_gold(self):
block_type = random.choice(list(GOLD_BLOCKS.keys())) # 随机选择一个矿块类型
block_value = GOLD_BLOCKS[block_type]
self.score += block_value
print(SCORE_MESSAGE_PREFIX + str(self.score))
print("你挖到了一个 " + block_type + " 黄金矿块,价值为 " + str(block_value))
# 实例化黄金矿工游戏对象并开始游戏
gold_miner = GoldMiner()
gold_miner.start_game()
```
你可以直接运行这段代码,即可体验黄金矿工游戏。
阅读全文