python代码复刻的一个文字小游戏
·
python代码复刻的一个文字小游戏
import os
import time
import random
# ===================== 颜色输出 =====================
def gray():
print("\033[90m", end="")
def blue():
print("\033[94m", end="")
def purple():
print("\033[95m", end="")
def gold():
print("\033[93m", end="")
def green():
print("\033[92m", end="")
def red():
print("\033[91m", end="")
def reset():
print("\033[0m", end="")
def display_colored_loot(item):
if item in ["水草", "热带鱼", "木头", "野果", "藤蔓"]:
gray()
elif item in ["海玻璃", "水晶原石", "矿石"]:
blue()
elif item in ["人鱼之泪", "沙漠琉璃", "古老化石", "月光石"]:
purple()
elif item in ["三叉戟碎片", "黄金碎屑", "藏宝图", "山铜结晶"]:
gold()
elif item in ["治愈草药", "蜂蜜"]:
green()
elif item in ["剧毒蜘蛛丝"]:
red()
print(item, end="")
reset()
# ===================== 清屏函数 =====================
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
# ===================== 下载动画 =====================
def download():
for i in range(0, 101, 10):
clear_screen()
print(f"正在下载 {i}%")
time.sleep(0.5)
clear_screen()
print("下载完成,欢迎来到Special Minecraft v.f.1.ov.1.1.1-探索的时光_第一部分\n")
# ===================== 玩家类 =====================
class Player:
def __init__(self):
self.Gametimes = 1
self.Health_Value = 40.0
self.Attack_Value = 1.0
self.Coin = 0.0
self.Armor_value = 0.0
self.Hunger_value = 40.0
self.Experience_Point_value = 0.0
self.Helmet = "暂无"
self.Breastplate = "暂无"
self.Legging = "暂无"
self.Boots = "暂无"
self.Weapon1 = "木剑"
self.Weapon2 = "木斧"
# ===================== 质数判断 =====================
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# ===================== 战斗系统 =====================
def fight_monster(player: Player):
names = ["僵尸", "骷髅", "蜘蛛", "苦力怕", "末影人"]
hps = [20, 20, 16, 20, 40]
attacks = [5, 6, 4, 9, 45]
pool = []
pool += [0] * 270000
pool += [1] * 80000
pool += [2] * 128500
pool += [3] * 121500
pool += [4] * 150003
idx = random.choice(pool)
name = names[idx]
hp = hps[idx]
atk = attacks[idx]
print("请选择武器:")
print("1. 木剑")
print("2. 木斧")
try:
c = int(input())
except:
c = 1
if c == 1:
player.Attack_Value = 6.0
else:
player.Attack_Value = 7.0
player.Health_Value = 40.0
while player.Health_Value > 0 and hp > 0:
print(f"\n你的生命值: {player.Health_Value}, {name} 生命值: {hp}")
print("1.攻击 2.逃跑")
try:
c = int(input())
except:
c = 0
if c == 2:
print("你逃跑了")
player.Coin -= 10
return
if c not in [1, 2]:
print("无效输入")
continue
num = random.randint(1, 100000)
if is_prime(num):
print(f"攻击失败,{name} 对你造成 {atk} 点伤害")
player.Health_Value -= atk
else:
print(f"攻击成功,你对 {name} 造成 {player.Attack_Value} 点伤害")
hp -= player.Attack_Value
print(f"{name} 对你造成 {atk} 点伤害")
player.Health_Value -= atk
if player.Health_Value <= 0:
print(f"你被 {name} 击败了!游戏结束")
return
if hp <= 0:
print(f"你击败了 {name}")
player.Experience_Point_value += 5
level = int(player.Experience_Point_value // 25)
need = (level + 1) * 25 - player.Experience_Point_value
print(f"获得经验5点,当前总经验:{player.Experience_Point_value}")
print(f"当前等级:{level},下级还需:{need} 点经验")
player.Coin += 10
print("获得 10 金币!")
# ===================== 显示属性 =====================
def show_stats(player: Player):
print("\n=== 玩家属性 ===")
print(f"游戏次数: {player.Gametimes}")
print(f"生命值: {player.Health_Value}")
print(f"攻击力: {player.Attack_Value}")
print(f"金币: {player.Coin}")
print(f"护甲值: {player.Armor_value}")
print(f"饥饿值: {player.Hunger_value}")
print(f"经验值: {player.Experience_Point_value}")
print(f"头盔: {player.Helmet}")
print(f"胸甲: {player.Breastplate}")
print(f"护腿: {player.Legging}")
print(f"靴子: {player.Boots}")
print(f"主武器: {player.Weapon1}")
print(f"副武器: {player.Weapon2}")
print("================")
# ===================== 探索世界(全地形已实现) =====================
def explore_world(player: Player):
places = ["海洋", "原始森林", "沙漠", "地表洞穴", "山丘", "峡谷"]
while True:
p = random.choice(places)
print(f"\n你发现了【{p}】,你要:")
print("1.留下探索")
print("2.继续前进")
print("3.返回主菜单")
try:
c = int(input())
except:
c = 0
if c == 1:
print("\n开始探索...")
t = random.randint(0, 25)
for i in range(t, -1, -1):
print(f"探索剩余时间:{i} 秒")
time.sleep(1)
clear_screen()
# 1. 海洋
if p == "海洋":
pool = []
pool += ["水草"] * 50001
pool += ["热带鱼"] * 15000
pool += ["三叉戟碎片"] * 3
pool += ["海玻璃"] * 20000
pool += ["人鱼之泪"] * 14999
item = random.choice(pool)
print("\n你在海洋中探索,发现了【", end="")
display_colored_loot(item)
print("】!")
if item == "水草":
player.Hunger_value = min(player.Hunger_value + 5, 40)
green()
print("水草可以食用,饥饿值恢复5点!")
elif item == "热带鱼":
player.Hunger_value = min(player.Hunger_value + 8, 40)
green()
print("热带鱼很美味,饥饿值恢复8点!")
elif item == "海玻璃":
player.Coin += 15
print("漂亮的海玻璃,售卖获得15金币!")
elif item == "人鱼之泪":
player.Coin += 50
player.Experience_Point_value += 10
print("传说中的人鱼之泪!获得50金币、10经验!")
elif item == "三叉戟碎片":
player.Attack_Value += 2
print("三叉戟碎片加持,攻击力永久+2!")
reset()
# 2. 原始森林
elif p == "原始森林":
pool = ["木头"]*40000 + ["野果"]*30000 + ["治愈草药"]*15000 + ["蜂蜜"]*10000 + ["剧毒蜘蛛丝"]*5000
item = random.choice(pool)
print("\n你在原始森林探索,发现了【", end="")
display_colored_loot(item)
print("】!")
if item == "木头":
player.Coin += 8
print("木头变卖获得8金币!")
elif item == "野果":
player.Hunger_value = min(player.Hunger_value + 6, 40)
green()
print("野果清甜,饥饿值恢复6点!")
elif item == "治愈草药":
player.Health_Value = min(player.Health_Value + 10, 40)
green()
print("治愈草药散发清香,生命值恢复10点!")
elif item == "蜂蜜":
player.Hunger_value = min(player.Hunger_value + 12, 40)
player.Health_Value = min(player.Health_Value + 5, 40)
green()
print("香甜蜂蜜,饥饿+12、生命+5!")
elif item == "剧毒蜘蛛丝":
player.Health_Value -= 8
red()
print("被毒丝划伤,损失8点生命值!")
reset()
# 3. 沙漠
elif p == "沙漠":
pool = ["沙漠碎石"]*45000 + ["沙漠琉璃"]*25000 + ["黄金碎屑"]*20000 + ["古老化石"]*10000
item = random.choice(pool)
print("\n你在茫茫沙漠探索,发现了【", end="")
display_colored_loot(item)
print("】!")
if item == "沙漠碎石":
player.Coin += 5
print("普通碎石,售卖获得5金币!")
elif item == "沙漠琉璃":
player.Coin += 25
print("稀有沙漠琉璃,售卖获得25金币!")
elif item == "黄金碎屑":
player.Coin += 40
player.Attack_Value += 1
print("黄金碎屑!金币+40、攻击力永久+1!")
elif item == "古老化石":
player.Experience_Point_value += 15
player.Coin += 30
print("远古生物化石!经验+15、金币+30!")
reset()
# 4. 地表洞穴
elif p == "地表洞穴":
pool = ["普通矿石"]*40000 + ["水晶原石"]*25000 + ["月光石"]*20000 + ["藏宝图"]*15000
item = random.choice(pool)
print("\n你进入幽暗洞穴,发现了【", end="")
display_colored_loot(item)
print("】!")
if item == "普通矿石":
player.Coin += 12
print("普通矿石,售卖获得12金币!")
elif item == "水晶原石":
player.Coin += 35
player.Armor_value += 1
print("水晶原石!金币+35、护甲永久+1!")
elif item == "月光石":
player.Experience_Point_value += 20
player.Health_Value = min(player.Health_Value + 15, 40)
print("月光石散发微光!经验+20、生命+15!")
elif item == "藏宝图":
player.Coin += 80
print("古老藏宝图!直接获得80金币!")
reset()
# 5. 山丘
elif p == "山丘":
pool = ["山石"]*50000 + ["藤蔓"]*25000 + ["山铜结晶"]*15000 + ["云雾仙草"]*10000
item = random.choice(pool)
print("\n你攀爬巍峨山丘,发现了【", end="")
display_colored_loot(item)
print("】!")
if item == "山石":
player.Coin += 6
print("坚硬山石,售卖获得6金币!")
elif item == "藤蔓":
player.Hunger_value = min(player.Hunger_value + 4, 40)
print("野藤果腹,饥饿值恢复4点!")
elif item == "山铜结晶":
player.Attack_Value += 1
player.Armor_value += 1
print("山铜结晶!攻击+1、护甲+1!")
elif item == "云雾仙草":
player.Experience_Point_value += 25
player.Health_Value = 40.0
print("云雾仙草奇效!满血恢复、经验+25!")
reset()
# 6. 峡谷
elif p == "峡谷":
pool = ["峡谷砂岩"]*42000 + ["深层矿石"]*28000 + ["流光晶石"]*20000 + ["远古残刃"]*10000
item = random.choice(pool)
print("\n你深入深邃峡谷,发现了【", end="")
display_colored_loot(item)
print("】!")
if item == "峡谷砂岩":
player.Coin += 7
print("峡谷砂岩,售卖获得7金币!")
elif item == "深层矿石":
player.Coin += 20
print("高纯度深层矿石,售卖20金币!")
elif item == "流光晶石":
player.Coin += 45
player.Experience_Point_value += 12
print("流光晶石!金币+45、经验+12!")
elif item == "远古残刃":
player.Attack_Value += 3
print("远古残刃蕴含力量!攻击力永久+3!")
reset()
elif c == 2:
print("\n你继续向前探索......")
time.sleep(1)
elif c == 3:
print("\n返回主菜单......")
break
else:
print("\n无效输入,请重新选择!")
time.sleep(1)
# ===================== 主菜单 =====================
def main():
download()
print("========================================")
print("免责声明:")
print("非官方 MINECRAFT 同人游戏")
print("未经 MOJANG 或 MICROSOFT 核准或与之关联")
print("========================================\n")
print("参与制作:\n代码实现:豆包\n创意总监:ll\n")
player = Player()
running = True
while running:
print("\n请进行以下操作")
print("1.开始试炼")
print("2.探索世界")
print("3.查看属性")
print("4.退出游戏")
try:
c = int(input())
except:
c = 0
if c == 1:
fight_monster(player)
elif c == 2:
explore_world(player)
elif c == 3:
show_stats(player)
elif c == 4:
print("感谢游玩,再见!")
running = False
else:
print("无效输入,请重新选择")
# 死亡重启
if player.Health_Value <= 0:
print("\n游戏结束!是否重新开始?(1.是 2.否): ")
try:
r = int(input())
except:
r = 2
if r == 1:
player = Player()
player.Gametimes += 1
else:
running = False
if __name__ == "__main__":
main()
更多推荐


所有评论(0)