Cat brother teach you to write reptiles 023-- Classes and Objects (lower) - Job

Today's job

Object-oriented manner, to rewrite the code ...

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
复制代码

Object-oriented approach ...

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
复制代码

Quick Jump:

Cat brother teach you to write reptile 000-- begins .md
cat brother teach you to write reptile 001 - print () functions and variables .md
cat brother teach you to write reptile 002-- job - Pikachu .md print
cat brother teach you to write reptiles 003 data type conversion .md
cat brother teach you to write reptile 004-- data type conversion - small practice .md
cat brother teach you to write reptile 005-- data type conversion - small jobs .md
cat brother teach you to write reptile 006- - conditional and nested conditions .md
cat brother teach you to write 007 reptile conditional and nested conditions - small operating .md
cat brother teach you to write reptile 008 - input () function .md
cat brother teach you to write reptiles 009 - input () function - AI little love students .md
cat brother teach you to write a list of 010 reptiles, dictionaries, circulation .md
cat brother teach you to write reptile 011-- lists, dictionaries, circulation - small jobs .md
cat brother teach you to write a Boolean value, and four reptile 012-- statements .md
cat brother teach you to write a Boolean value, and four reptile 013-- statements - smaller jobs .md
cat brother teach you to write reptile 014 - pk game. md
cat brother teach you to write reptile 015 - pk game (new revision) .md
cat brother teach you to write reptile 016-- function .md
cat brother teach you to write reptile 017-- function - a small job .md
cat brother to teach you write reptile 018--debug.md
cat brother teach you to write reptile 019 - debug- job. md
cat brother teach you to write reptiles 020-- Classes and Objects (on) .md
cat brother teach you to write reptiles 021-- Classes and Objects (a) - Job .md
Cat brother teach you to write reptiles 022-- Classes and Objects (lower) .md
cat brother teach you to write reptiles 023-- Classes and Objects (lower) - Job .md
cat brother teach you to write reptile 024-- decoding coded && .md
cat brother teach you to write reptile 025 && decoding coded - small jobs .md
cat brother teach you to write reptile 026-- module .md
cat brother teach you to write reptile 027-- module introduces .md
cat brother teach you to write reptile 028- - introduction module - small job - billboards .md
cat brother teach you to write Preliminary -requests.md reptile reptilian 029--
cat brother teach you to write reptile reptilian 030-- Preliminary -requests- job .md
cat brother teach you to write 031 reptiles - reptile basis -html.md
cat brother teach you to write reptile reptilian 032-- first experience -BeautifulSoup.md
cat brother teach you to write reptile reptilian 033-- first experience -BeautifulSoup- job .md
cat brother teach you to write reptile 034- - reptile -BeautifulSoup practice .md
cat brother teach you to write 035-- reptile reptilian -BeautifulSoup practice - job - film top250.md
cat brother teach you to write 036-- reptile reptilian -BeautifulSoup practice - work - work to resolve .md movie top250-
cat brother teach you to write 037-- reptile reptiles - to listen to songs .md baby
cat brother teach you to write reptile 038-- arguments request .md
cat brother teach you to write data stored reptile 039-- .md
cat brother teach you to write reptiles 040-- store data - Job .md
cat brother teach you to write reptile 041-- analog login -cookie.md
Cat brother teach you to write reptile 042 - session usage .md
cat brother teach you to write reptile 043-- analog browser .md
cat brother teach you to write reptile 044-- analog browser - job .md
cat brother teach you to write reptiles 045-- coroutine .md
cat brother teach you to write reptile 046-- coroutine - practice - what to eat not fat .md
cat brother teach you to write reptile 047 - scrapy framework .md
cat brother teach you to write reptile 048-- .md reptile reptiles and anti-
cat brother teach you to write reptile 049-- end Sahua .md

Reproduced in: https: //juejin.im/post/5cfc4ad95188254cef547a4d

Guess you like

Origin blog.csdn.net/weixin_34197488/article/details/91469767