python双人格斗类益智代码游戏
时间: 2023-12-26 07:28:30 浏览: 49
以下是一个简单的Python双人格斗类益智代码游戏的示例:
```python
import random
# 定义玩家类
class Player:
def __init__(self, name):
self.name = name
self.health = 100
self.attack_power = random.randint(5, 10)
def attack(self, opponent):
damage = self.attack_power
opponent.health -= damage
print(f"{self.name}攻击了{opponent.name},造成了{damage}点伤害。")
# 创建两个玩家对象
player1 = Player("玩家1")
player2 = Player("玩家2")
# 游戏循环
while player1.health > 0 and player2.health > 0:
# 玩家1攻击玩家2
player1.attack(player2)
if player2.health <= 0:
break
# 玩家2攻击玩家1
player2.attack(player1)
if player1.health <= 0:
break
# 判断胜利者
if player1.health > 0:
print(f"{player1.name}获胜!")
else:
print(f"{player2.name}获胜!")
```