Python homework, text version of fighting game.

Text version of the fighting game, set the player's nickname, blood volume, damage, and start the game.
Learn the use of classes and methods along the way.

# -*- coding:utf-8 -*-
class 玩家:
    def __init__(self,昵称,血量,伤害):
        self.昵称 = 昵称
        self.血量 = 血量
        self.伤害 = 伤害
    def 攻击(self,player):
        print('战斗开始,玩家[%s]当前生命值:%s。'%(player.昵称,player.血量))
        player.血量 = player.血量 - self.伤害
        if player.血量<=0:
            player.血量 = 0
            print('  玩家[%s]攻击玩家[%s],造成[%s]伤害,对方血量剩余[%s]。'%(self.昵称,player.昵称,self.伤害,player.血量))
            print('  玩家[%s]被击败。'%(player.昵称))
        else:
            print('  玩家[%s]攻击玩家[%s],造成[%s]伤害,对方血量剩余[%s]。'%(self.昵称,player.昵称,self.伤害,player.血量))
        print('战斗结束。')
player1 = 玩家 (昵称="A",血量=200,伤害=56)
player2 = 玩家 (昵称="B",血量=150,伤害=60)
player1.攻击(player2)
player1.攻击(player2)
player1.攻击(player2)

Guess you like

Origin blog.csdn.net/Deng7326/article/details/127625223