用PYTHON写一个忍者神龟对打小游戏,要有代码
时间: 2023-09-08 19:04:53 浏览: 149
### 回答1:
好的,这是一个很有趣的问题!下面是用 Python 编写的简单的忍者神龟对打小游戏代码:
```
import random
player_hp = 100
enemy_hp = 100
print("忍者神龟大战小怪兽")
while player_hp > 0 and enemy_hp > 0:
print("="*30)
print("你的血量:", player_hp)
print("小怪兽的血量:", enemy_hp)
print("="*30)
player_attack = random.randint(10, 20)
enemy_attack = random.randint(10, 20)
print("请选择你的攻击方式:")
print("1. 普通攻击")
print("2. 重击(有一定几率伤害加倍)")
print("3. 治疗(消耗一定血量恢复自身血量)")
player_choice = input("请出招(输入对应数字):")
if player_choice == "1":
print("你使用了普通攻击,造成了", player_attack, "点伤害")
enemy_hp -= player_attack
elif player_choice == "2":
if random.random() < 0.3:
player_attack *= 2
print("你使用了重击,造成了", player_attack, "点伤害")
enemy_hp -= player_attack
else:
print("你使用了重击,但是没有造成额外伤害")
enemy_hp -= player_attack
elif player_choice == "3":
player_hp -= 10
player_hp = min(player_hp + 20, 100) # 最多恢复到满血
print("你使用了治疗,恢复了20点血量")
else:
print("输入错误,请重新出招")
continue
if enemy_hp <= 0:
print("你获得了胜利!")
break
print("小怪兽对你发起了攻击,造成了", enemy_attack, "点伤害")
player_hp -= enemy_attack
if player_hp <= 0:
print("你被小怪兽打败了!")
break
```
在这个游戏中,你可以选择普通攻击、重击和治疗三种方式进行战斗,小怪兽也会随机出招伤害你。注意掌握好自己的血量和使用治疗技能的时机,才能获得胜利哦!
### 回答2:
下面是一个简单的用Python写的忍者神龟对打小游戏的代码:
```python
import random
# 定义角色类
class Character:
def __init__(self, name, health, attack):
self.name = name
self.health = health
self.attack = attack
def take_damage(self, damage):
self.health -= damage
def is_alive(self):
return self.health > 0
def attack_enemy(self, enemy):
damage = random.randint(1, self.attack)
enemy.take_damage(damage)
print(f"{self.name} 对 {enemy.name} 造成了 {damage} 点伤害。")
# 创建忍者神龟角色
turtle = Character("忍者神龟", 100, 20)
# 创建敌人角色
enemy = Character("敌人", 100, 15)
# 游戏循环,直到有一方角色死亡
while turtle.is_alive() and enemy.is_alive():
# 忍者神龟攻击敌人
turtle.attack_enemy(enemy)
# 敌人攻击忍者神龟
enemy.attack_enemy(turtle)
# 判断胜负
if turtle.is_alive():
print("忍者神龟获胜!")
else:
print("敌人获胜!")
```
这个游戏中,有两个角色忍者神龟和敌人,每个角色都有生命值(health)和攻击力(attack)。游戏开始时,双方的生命值都被初始化为100。每轮游戏中,忍者神龟和敌人轮流攻击对方,造成的伤害在1到自身攻击力之间随机取值。每次攻击后,受攻击方的生命值会减去对应伤害值。游戏循环直到有一方的生命值降到0以下,判断胜负并打印结果。
### 回答3:
下面是一个简单的使用Python编写的忍者神龟对打小游戏的示例代码:
```python
import random
def fight():
ninja_health = 100
turtle_health = 100
while ninja_health > 0 and turtle_health > 0:
ninja_attack = random.randint(10, 20)
turtle_attack = random.randint(5, 15)
print("忍者神龟:", ninja_health)
print("忍者龟:", turtle_health)
print("-------------")
print("忍者神龟攻击了忍者龟,造成了", ninja_attack, "点伤害")
print("忍者龟攻击了忍者神龟,造成了", turtle_attack, "点伤害")
print("-------------")
ninja_health -= turtle_attack
turtle_health -= ninja_attack
if ninja_health <= 0 and turtle_health <= 0:
print("双方同时倒下,平局!")
elif ninja_health <= 0:
print("忍者龟获胜!")
elif turtle_health <= 0:
print("忍者神龟获胜!")
fight()
```
在游戏中,忍者神龟和忍者龟都有初始100点的生命值。每轮游戏中,两只角色都会随机产生一个攻击数值,忍者神龟的攻击力范围为10-20,忍者龟的攻击力范围为5-15。然后,根据攻击数值进行伤害计算,忍者神龟的生命值减去忍者龟的攻击数值,忍者龟的生命值减去忍者神龟的攻击数值。直到其中一方生命值小于等于0,游戏结束。
代码中的`fight()`函数实现了双方对打的逻辑,通过循环来进行多轮对打,直到有一方生命值小于等于0。在每轮对打后,都会打印出双方当前的生命值和造成的伤害,最后根据生命值判断胜负。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)