用python写一个大于100行代码的小游戏多注释
时间: 2024-01-26 10:02:58 浏览: 21
好的,我给您提供一个比较复杂的小游戏,带有注释,总共超过100行代码。这个游戏是一个简化版的石头剪刀布游戏,玩家和电脑对战,可以选择不同的角色和游戏模式。
```python
import random # 导入随机数模块
# 游戏角色类,包括角色名、角色属性和角色技能
class Character:
def __init__(self, name, hp, atk, skill):
self.name = name
self.hp = hp
self.atk = atk
self.skill = skill
# 游戏技能类,包括技能名、技能属性和技能效果
class Skill:
def __init__(self, name, attribute, effect):
self.name = name
self.attribute = attribute
self.effect = effect
# 游戏属性类,包括属性名、属性优劣和属性克制关系
class Attribute:
def __init__(self, name, advantage, disadvantage, counter):
self.name = name
self.advantage = advantage
self.disadvantage = disadvantage
self.counter = counter
# 定义游戏角色、技能和属性
rockman = Character("洛克人", 100, 20, Skill("洛克炮", "炮击", 30))
cutman = Character("卡特人", 80, 30, Skill("切割器", "斩击", 25))
bombman = Character("爆弹人", 120, 15, Skill("爆弹", "爆炸", 35))
fireman = Character("火焰人", 90, 25, Skill("火焰", "火焰", 20))
piercing = Attribute("炮击", "斩击", "火焰", "爆炸")
slashing = Attribute("斩击", "火焰", "炮击", "爆炸")
explosion = Attribute("爆炸", "炮击", "斩击", "火焰")
fire = Attribute("火焰", "斩击", "炮击", "爆炸")
# 定义游戏模式
modes = {
"1": ("剪刀", "石头", "布"),
"2": ("炮击", "斩击", "爆炸", "火焰")
}
# 游戏开始
print("欢迎来到石头剪刀布游戏!")
print("请选择角色:")
print(f"1. {rockman.name}")
print(f"2. {cutman.name}")
print(f"3. {bombman.name}")
print(f"4. {fireman.name}")
while True:
player_choice = input("输入数字选择角色:") # 玩家选择角色
if player_choice in ("1", "2", "3", "4"):
break
else:
print("输入有误,请重新选择!")
# 根据玩家选择的角色,设置电脑角色
if player_choice == "1":
computer = random.choice((cutman, bombman, fireman))
elif player_choice == "2":
computer = random.choice((rockman, bombman, fireman))
elif player_choice == "3":
computer = random.choice((rockman, cutman, fireman))
else:
computer = random.choice((rockman, cutman, bombman))
print(f"你选择了{eval(f'rockman.name if player_choice == "1" else cutman.name if player_choice == "2" else bombman.name if player_choice == "3" else fireman.name')},电脑选择了{computer.name}。")
# 等待玩家选择游戏模式
while True:
print("请选择游戏模式:")
print("1. 简单模式(剪刀石头布)")
print("2. 困难模式(炮击斩击爆炸火焰)")
mode_choice = input("输入数字选择模式:")
if mode_choice in ("1", "2"):
break
else:
print("输入有误,请重新选择!")
mode = modes[mode_choice]
player_score = 0 # 玩家得分
computer_score = 0 # 电脑得分
round_count = 1 # 当前回合数
while True:
print(f"第{round_count}回合:")
print(f"{rockman.name}的生命值:{rockman.hp},{cutman.name}的生命值:{cutman.hp},{bombman.name}的生命值:{bombman.hp},{fireman.name}的生命值:{fireman.hp}。")
print(f"你的得分:{player_score},电脑的得分:{computer_score}。")
print(f"请选择你的出招(1-{len(mode)}):")
for i in range(len(mode)):
print(f"{i+1}. {mode[i]}")
player_choice = input("输入数字选择出招:") # 等待玩家出招
if player_choice not in tuple(map(str, range(1, len(mode)+1))):
print("输入有误,请重新出招!")
continue
player_choice = mode[int(player_choice)-1]
computer_choice = random.choice(mode) # 随机选择电脑出招
# 根据出招属性和技能,计算出招伤害
if getattr(player_choice, "attribute") == getattr(computer_choice, "advantage"):
damage = getattr(player_choice, "skill").effect
print(f"你使用了{player_choice.name},电脑使用了{computer_choice.name},造成了{damage}点伤害!")
setattr(computer, "hp", getattr(computer, "hp") - damage)
elif getattr(player_choice, "attribute") == getattr(computer_choice, "disadvantage"):
damage = getattr(computer_choice, "skill").effect
print(f"你使用了{player_choice.name},电脑使用了{computer_choice.name},但是被电脑{getattr(computer_choice, 'attribute')}属性克制,你受到了{damage}点伤害!")
setattr(rockman if player_choice == rockman.skill.attribute else cutman if player_choice == cutman.skill.attribute else bombman if player_choice == bombman.skill.attribute else fireman, "hp", getattr(rockman if player_choice == rockman.skill.attribute else cutman if player_choice == cutman.skill.attribute else bombman if player_choice == bombman.skill.attribute else fireman, "hp") - damage)
else:
print(f"你使用了{player_choice.name},电脑使用了{computer_choice.name},但是没有造成伤害。")
# 根据角色的生命值判断胜负
if getattr(computer, "hp") <= 0:
print(f"恭喜你,你赢了!")
player_score += 1
break
elif getattr(rockman if player_choice == rockman.skill.attribute else cutman if player_choice == cutman.skill.attribute else bombman if player_choice == bombman.skill.attribute else fireman, "hp") <= 0:
print(f"很遗憾,你输了!")
computer_score += 1
break
else:
round_count += 1
# 如果回合数超过10,强制结束游戏
if round_count > 10:
print("回合数超过10,游戏强制结束!")
break
# 如果当前得分差距大于等于3,强制结束游戏
if abs(player_score - computer_score) >= 3:
print("当前得分差距大于等于3,游戏强制结束!")
break
# 打印最终结果
if player_score > computer_score:
print("你赢了!")
elif player_score < computer_score:
print("你输了!")
else:
print("平局!")
print("谢谢游玩,再见!")
```
这个游戏比较复杂,包含了角色、技能和属性等多个类别,还有不同的游戏模式和回合制的对战机制。希望这个游戏能够为您提供参考。