用python写一个人机对战射击小游戏
时间: 2023-12-07 19:59:40 浏览: 177
好的,下面是一个简单的人机对战射击小游戏的Python代码:
```python
import random
player = input("请输入你的名字:")
print("欢迎来到人机对战射击小游戏,", player, "!")
enemy_health = 100
player_health = 100
while enemy_health > 0 and player_health > 0:
print("敌人还剩下", enemy_health, "点血量。")
print("你还剩下", player_health, "点血量。")
# 玩家攻击
player_attack = random.randint(10, 30)
enemy_health -= player_attack
print(player, "攻击了敌人,造成了", player_attack, "点伤害。")
if enemy_health <= 0:
print("恭喜你,你赢了!")
break
# 敌人攻击
enemy_attack = random.randint(5, 20)
player_health -= enemy_attack
print("敌人攻击了你,造成了", enemy_attack, "点伤害。")
if player_health <= 0:
print("很遗憾,你输了。")
break
```
这个小游戏的规则很简单,你需要不断攻击敌人,直到他的血量降为0。同时,敌人也会攻击你,你需要注意保护自己的血量。
阅读全文