python实现游戏奥特曼打怪兽
时间: 2023-07-12 16:44:07 浏览: 205
用python3制作的奥特曼打怪兽
5星 · 资源好评率100%
好的,我可以给你提供一个简单的Python实现游戏奥特曼打怪兽的代码,让你可以参考。
首先,我们需要导入一些必要的模块和库:
```python
import random
import time
```
然后,我们定义一些初始变量:
```python
player_name = input("请输入玩家名字:")
player_life = 3
player_attack = random.randint(30, 50)
monster_name = "怪兽"
monster_life = 100
monster_attack = random.randint(10, 30)
```
接下来,我们可以定义一些函数来实现游戏逻辑:
```python
# 玩家攻击函数
def player_attack_func():
print("%s 发起了攻击, %s 受到了 %d 点伤害!" % (player_name, monster_name, player_attack))
global monster_life
monster_life = monster_life - player_attack
# 怪兽攻击函数
def monster_attack_func():
print("%s 发起了攻击, %s 受到了 %d 点伤害!" % (monster_name, player_name, monster_attack))
global player_life
player_life = player_life - monster_attack
# 游戏结束函数
def game_over():
print("游戏结束!")
if player_life > 0:
print("%s 获胜!" % player_name)
else:
print("%s 被击败了!" % player_name)
```
最后,我们可以写一个主循环来实现游戏的流程:
```python
# 主循环
while True:
# 玩家攻击
player_attack_func()
if monster_life <= 0:
print("%s 被击败了!" % monster_name)
game_over()
break
# 怪兽攻击
monster_attack_func()
if player_life <= 0:
print("%s 被击败了!" % player_name)
game_over()
break
# 输出玩家和怪兽的当前状态
print("%s 的生命值:%d" % (player_name, player_life))
print("%s 的生命值:%d" % (monster_name, monster_life))
time.sleep(1)
```
完整代码如下:
```python
import random
import time
# 玩家攻击函数
def player_attack_func():
print("%s 发起了攻击, %s 受到了 %d 点伤害!" % (player_name, monster_name, player_attack))
global monster_life
monster_life = monster_life - player_attack
# 怪兽攻击函数
def monster_attack_func():
print("%s 发起了攻击, %s 受到了 %d 点伤害!" % (monster_name, player_name, monster_attack))
global player_life
player_life = player_life - monster_attack
# 游戏结束函数
def game_over():
print("游戏结束!")
if player_life > 0:
print("%s 获胜!" % player_name)
else:
print("%s 被击败了!" % player_name)
player_name = input("请输入玩家名字:")
player_life = 3
player_attack = random.randint(30, 50)
monster_name = "怪兽"
monster_life = 100
monster_attack = random.randint(10, 30)
while True:
# 玩家攻击
player_attack_func()
if monster_life <= 0:
print("%s 被击败了!" % monster_name)
game_over()
break
# 怪兽攻击
monster_attack_func()
if player_life <= 0:
print("%s 被击败了!" % player_name)
game_over()
break
# 输出玩家和怪兽的当前状态
print("%s 的生命值:%d" % (player_name, player_life))
print("%s 的生命值:%d" % (monster_name, monster_life))
time.sleep(1)
```
阅读全文