猫哥教你写爬虫 023--类与对象(下)-作业

今日作业

使用面向对象的方式, 来改写代码...

import time
import random
user1 = {
    'name': '',
    'life': 0,
    'victory':0
}
user2 = {
    'name': '',
    'life': 0,
    'victory':0
}
attack_list = [
    {
        'desc':'{} 挥剑向 {} 砍去',
        'num':20
    },
    {
        'desc':'{} 准备刚正面, 对 {} 使出一记"如来神掌"',
        'num':30
    },
    {
        'desc':'{} 撸起了袖子给 {} 一顿胖揍',
        'num':25
    },
    {
        'desc':'{} 向 {} 抛了个媚眼',
        'num':10
    },
    {
        'desc':'{} 抄起冲锋枪就是一梭子, {} 走位风骚, 轻松躲过',
        'num':0
    },
    {
        'desc':'{} 出了一个大招, {} 躲闪不及, 正中要害',
        'num':40
    }
]
user1['name'] = input('请输入玩家1的昵称: ')
user2['name'] = input('请输入玩家2的昵称: ')
while True:
    user1['victory'] = 0
    user2['victory'] = 0
    for i in range(3):
        print('  \n——————现在是第 {} 局——————'.format(i+1))
        user1['life'] = random.randint(100, 150)
        user2['life'] = random.randint(100, 150)
        print(user1['name']+'\n血量:{}'.format(user1['life']))
        print('------------------------')
        print(user2['name']+'\n血量:{}'.format(user2['life']))
        print('-----------------------')
        time.sleep(4)
        first_attack = random.randint(0,1)
        users_list = []
        if first_attack:
            user1['isfirst'] = 1
            print('漂亮!!! '+user1['name']+' 获得先发优势!')
            print('')
            users_list.append(user1)
            users_list.append(user2)
        else:
            user2['isfirst'] = 1
            print('难以置信! '+user2['name']+' 获得先发优势!')
            print('')
            users_list.append(user2)
            users_list.append(user1)
        time.sleep(2)
        while user1['life'] > 0 and user2['life'] > 0:
            tmp_rand = random.randint(0,len(attack_list)-1)
            users_list[1]['life'] = users_list[1]['life'] - attack_list[tmp_rand]['num']
            print(attack_list[tmp_rand]['desc'].format(users_list[0]['name'],users_list[1]['name'])+' 造成了{}点伤害 ==> '.format(attack_list[tmp_rand]['num'])+users_list[1]['name']+' 的剩余血量:{}'.format(users_list[1]['life']))
            time.sleep(4)
            if users_list[1]['life'] <= 0:
                print('')
                print(users_list[1]['name']+' 惨败 -_-||')
                users_list[0]['victory']+=1
                break
            tmp_rand = random.randint(0,len(attack_list)-1)
            users_list[0]['life'] = users_list[0]['life'] - attack_list[tmp_rand]['num']
            print(attack_list[tmp_rand]['desc'].format(users_list[1]['name'],users_list[0]['name'])+' 造成了{}点伤害 ==> '.format(attack_list[tmp_rand]['num'])+users_list[0]['name']+' 的剩余血量:{}'.format(users_list[0]['life']))
            time.sleep(4)
            if users_list[0]['life'] <= 0:
                print('')
                print(users_list[0]['name']+' 惨败 -_-||')
                time.sleep(3)
                users_list[1]['victory']+=1
                break
            print('-----------------------')
        if user1['victory'] == 2:
            print('')
            print('三局两胜中 '+user1['name']+' 获胜!')
            break
        if user2['victory'] == 2:
            print('')
            print('三局两胜中 '+user2['name']+' 获胜!')
            break
    print('')
    res = input('要不要再来一局? (回复"是"再来一局, 其他退出...) ') 
    if res != '是':
        break
复制代码

面向对象的方式...

import random
import time
class Fight():
    def __init__(self, name1, name2):
        self.player1 = Player(name1)
        self.player2 = Player(name2)
    def get_first_attack(self):
        if random.randint(0, 1):
            self.player1.is_first = True
            self.player2.is_first = False
            self.f_print('漂亮!!! {} 获得先发优势!'.format(self.player1.name))
        else:
            self.player2.is_first = True
            self.player1.is_first = False
            self.f_print('漂亮!!! {} 获得先发优势!'.format(self.player2.name))
    def start_fight(self):
        for i in range(3):
            self.player1.make_player()
            self.player2.make_player()
            self.get_first_attack()
            if self.player1.is_first:
                while self.player1.life >= 0 and self.player2.life >= 0:
                    self.player1.attack(self.player2)
                    if self.check_life(self.player2):
                        break
                    self.player2.attack(self.player1)
                    self.check_life(self.player1)
            else:
                while self.player1.life >= 0 and self.player2.life >= 0:
                    self.player2.attack(self.player1)
                    if self.check_life(self.player1):
                        break
                    self.player1.attack(self.player2)
                    self.check_life(self.player2)
            if self.player1.victory == 1 or self.player2.victory == 1:
                break
        self.final_result(self.player1, self.player2)
    def check_life(self, cls):
        if cls.life <= 0:
            self.f_print('{} 惨败!'.format(cls.name))
            cls.victory -= 1
            return True
    def final_result(self, cls1, cls2):
        if cls1.victory <= 1:
            self.f_print('在三局两胜中, {} 获得了最后的胜利!!!'.format(cls2.name))
        else:
            self.f_print('在三局两胜中, {} 获得了最后的胜利!!!'.format(cls1.name))
        cls1.victory = 3
        cls2.victory = 3
    def f_print(self, str1):
        print(str1)
        print('-------------------')
        time.sleep(3)
class Player():
    name = ''
    life = 0
    victory = 3
    is_first = False
    skills = attack_list = [
        {
            'desc': '{} 挥剑向 {} 砍去',
            'num': 20
        },
        {
            'desc': '{} 准备刚正面, 对 {} 使出一记"如来神掌"',
            'num': 30
        },
        {
            'desc': '{} 撸起了袖子给 {} 一顿胖揍',
            'num': 25
        },
        {
            'desc': '{} 向 {} 抛了个媚眼',
            'num': 10
        },
        {
            'desc': '{} 抄起冲锋枪就是一梭子, {} 走位风骚, 轻松躲过',
            'num': 0
        },
        {
            'desc': '{} 出了一个大招, {} 躲闪不及, 正中要害',
            'num': 40
        }
    ]
    def __init__(self, name):
        self.name = name
    def make_player(self):
        self.life = random.randint(100, 150)
        fight.f_print(self.name+'\n血量:{}'.format(self.life))
    def attack(self, other):
        skills_index = random.randint(0, len(self.skills)-1)
        attack_num = self.skills[skills_index]['num']
        attack_desc = self.skills[skills_index]['desc']
        other.life = other.life - attack_num
        attack_print = attack_desc.format(self.name, other.name)
        attack_print += "造成了 {} 点伤害... ".format(attack_num)
        attack_print += "{} 的生命值为 {}".format(other.name, other.life)
        fight.f_print(attack_print)
fight = Fight(input('请输入玩家1的姓名: '), input('请输入玩家2的姓名: '))
while True:
    fight.start_fight()  # 开打
    if input('是否继续, 如果回复"是", 则继续, 回复其他则退出... ') != "是":
        break
复制代码

快速跳转:

猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--作业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小作业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小作业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同学.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小作业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小作业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小作业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-作业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-作业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-作业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小作业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小作业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-作业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-作业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-作业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-作业-电影top250-作业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-作业.md
猫哥教你写爬虫 041--模拟登录-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-作业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.md

转载于:https://juejin.im/post/5cfc4ad95188254cef547a4d

猜你喜欢

转载自blog.csdn.net/weixin_34197488/article/details/91469767
今日推荐