11-悦创带你现场 coding 小游戏【主角打怪-文字游戏】

AI悦创原创Python 私教练习题【基础】Python 基础练习题大约 4 分钟...约 1240 字

1. 游戏名称：主角打怪-文字游戏
2. 一个玩家【Player】、一个敌人【Enemy】
3. 敌人和玩家互相攻击
4. 玩家可以选择攻击也可以选择防守，敌人也一定会攻击，防守受到的攻击为：十分之一
5. 敌人的攻击数值随机【random】
``````# -*- coding: utf-8 -*-
# @Author: clela
# @Date:   2020-03-31 14:13:35
import random
# 1
class Creature():
def attack(self):
# 我希望得到一个随机的攻击数值
attack_value = random.randint(0, 50)
return attack_value
``````

``````player = Creature()
enemy = Creature()
``````

``````while player.not_dead() and enemy.not_dead():
pass
``````

``````player = Creature(100)
enemy = Creature(80)
``````

``````class Creature():
def __init__(self, hp):
self.hp = hp
``````

``````# 写法一：
if self.hp <= 0:
return False
else:
return True
# 写法二
if self.hp <= 0:
return False
return True
``````

这里我就写的简单点，就不判断用户输入的是否符合规则，我们这里就先默认用户输入是对的，课后自行添加！
``````while player.not_dead() and enemy.not_dead():
user_input = input("Attack or Defence(A/D):")

if user_input == "A":
player_attack_value = player.attack()
enemy_attack_value = enemy.attack()
``````

``````enemy.being_attack(player_attack_value)
player.being_attack(enemy_attack_value)
``````

``````def being_attack(self, attack_value):
self.hp = self.hp - attack_value
``````

``````elif user_input == "D":
enemy_attack_value = enemy.attack()*0.1
player.being_attack(enemy_attack_value)
``````

``````player.show_status()
enemy.show_status()
``````

``````def show_status(self):
print(self.hp)
``````

``````player = Creature(100, "AI悦创")
enemy = Creature(80, "Enemy")
``````

``````def __init__(self, hp, name):
self.hp = hp
self.name = name
``````

``````def show_status(self):
print("{}' hp is {}.".format(self.name, self.hp))
``````

``````if player.not_dead():
print("You Win!")
else:
print("You Lose")
``````

AI悦创·编程一对一

AI悦创·推出辅导班啦，包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发」，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然，还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线，随时响应！微信：Jiabcdefh

C++ 信息奥赛题解，长期更新！长期招收一对一中小学信息奥赛集训，莆田、厦门地区有机会线下上门，其他地区线上。微信：Jiabcdefh

• 0
• 0
• 0
• 0
• 0
• 0

• 按正序
• 按倒序
• 按热度