簡體   English   中英

OOP 和 python 的新手,需要幫助更新我的 __init__ function 中的 self.lifepoints,

[英]New to OOP with python, need help updating a self.lifepoints within my __init__ function,

我正在編寫一個名為 battle bots 的程序,它非常簡單,但我是 OOP 和 python 的新手,所以我很難讓它正常工作。 我最大的問題是更新我的init方法中的生命點。 初始化中,我有 self.lifepoints = 100,但是當“機器人”受到損壞時,我需要將該數字更新為相當於損壞的數字。 這是代碼,我真的可以使用一些建議

import random

class player:

    def __init__(self):
        self.lifepoints = 100

    def getStrength(self):
        self.strength = random.randint(1, 40)
        return self.strength
    
    def doDamage(self):
        self.damage = self.lifepoints - self.strength
        
        return self.damage
    
        

class botGame:
    
    bot1 = player()
    bot2 = player() 
    while True:

        print("Welcome to Battle Bots...")
        choice = input("Bot 1 it's your turn, press 'h' to Hit or 'q' to Quit: ")
        while True:
            print("Bot 1 life points: ", bot1.lifepoints, '\n', "Bot 2 life points: ", bot2.lifepoints)
            

            if choice == 'q':
                quit
            
            if choice == 'h':
                print("Bot 1's strength: ",bot1.getStrength())
                print("Bot 2's strength: ",bot2.getStrength())
                # if statement for .getstrength() for each bot
                if bot1.strength > bot2.strength:
                    print(bot1.doDamage())
                else:
                    print(bot2.doDamage())
                
                print("Bot 1 life points: ",bot1.lifepoints)
                print("Bot 2 life points: ",bot2.lifepoints)
                break
        while True:    
            print("Bot 2, your turn!")
            choice = input("Bot 1 it's your turn, press 'h' to Hit or 'q' to Quit: ")
                
            if choice == 'h':
                print("Bot 1's strength: ",bot1.getStrength())
                print("Bot 2's strength: ",bot2.getStrength())
                print(player.doDamage(bot1, bot2))
                print("Bot 1 life points: ",bot1.lifepoints)
                print("Bot 2 life points: ",bot2.lifepoints)

                if bot1.lifepoints > bot2.lifepoints:
                    print("Bot 1 Wins this round!", '\n'," Thanks for playing!", '\n', "Goodbye!")
                else:
                    print("Bot 2 Wins this round!", '\n'," Thanks for playing!", '\n', "Goodbye!")
                break

這是我最終使用的最終程序

import random

class player:

    def __init__(self):
        self.lifepoints = 100

    def getStrength(self):
        self.strength = random.randint(1, 40)
        
        return self.strength
    
    def doDamage(self, attack):
        self.attack = attack
        return self.attack
    
        
count = 0

class botGame:
    
    bot1 = player()
    bot2 = player() 
    
    print("Welcome to Battle Bots...")
    print("Bot 1 life points: ", bot1.lifepoints, '\n', "Bot 2 life points: ", bot2.lifepoints)
        
    choice = input("Bot 1 it's your turn, press 'h' to Hit or 'q' to Quit: ")
    while count < 2:
            
            

            if choice == 'q':
                quit
            
            if choice == 'h':
                print("Bot 1's strength: ",bot1.getStrength())
                print("Bot 2's strength: ",bot2.getStrength())
                # if statement for .getstrength() for each bot
                if bot1.strength > bot2.strength:
                    attack = bot1.strength - bot2.strength
                    bot2.lifepoints -= bot1.doDamage(attack)
                    print("Bot 2 takes", bot1.doDamage(attack),"points of damage")
                else:
                    attack = bot2.strength - bot1.strength
                    bot1.lifepoints -= bot2.doDamage(attack)
                    print("Bot 1 takes",bot2.doDamage(attack),"points of damage")
                
                print("Bot 1 life points: ",bot1.lifepoints)
                print("Bot 2 life points: ",bot2.lifepoints)

                count += 1
                
                
            choice = input("Bot 2 it's your turn, press 'h' to Hit or 'q' to Quit: ")
                    
            if choice == 'h':
                print("Bot 1's strength: ",bot1.getStrength())
                print("Bot 2's strength: ",bot2.getStrength())

                if bot1.strength > bot2.strength:
                    attack = bot1.strength - bot2.strength
                    bot2.lifepoints -= bot1.doDamage(attack)
                    print("Bot 2 takes", bot1.doDamage(attack),"points of damage")
                else:
                    attack = bot2.strength - bot1.strength
                    bot1.lifepoints -= bot2.doDamage(attack)
                    print("Bot 1 takes",bot2.doDamage(attack),"points of damage")
                    
                print("Bot 1 life points: ",bot1.lifepoints, "Bot 2 life points: ",bot2.lifepoints)
                
                    
                    
            if bot1.lifepoints > bot2.lifepoints:
                    print("Bot 1 Wins this round!",'\n',"Thanks for playing!",'\n',"Goodbye!")
                    break
            if bot1.lifepoints == bot2.lifepoints:
                    print("It's a tie :,(")
                    break
            if bot2.lifepoints > bot1.lifepoints:
                print("Bot 2 Wins this round!",'\n',"Thanks for playing!",'\n',"Goodbye!")
                break
            count += 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM