簡體   English   中英

在另一個函數中的自身中調用類函數,以便屬性僅包含一個,而變量不包含,但自身函數呢?

[英]Call a class function in a self in another function so that the attributes only include one and the variable not included but is self function?

該代碼在主要功能中:

播放器和計算機是以下格式的列表:

player = ["name", 10, 20, 30]

10是健康,20是力量,30是敏捷

# Get the character for the user and the computer.
player = character_choices.get_and_remove_character(choice)
computer = character_choices.get_random_character()

# Preparation for the battle
print ("You have picked a " + player.name)
print ("The computer picked " + computer.name)
print ("Let's battle!\n")

# Battle Loop
rnd = 1
while (player.hit_points > 0 and computer.hit_points > 0):
    print ("Round: " + str(rnd))
    player.attack (computer)
    computer.attack (player)

這是功能:

def attack (self, enemy):
    self.player = [self]
    if self.player[3] == enemy[3]:

我不知道如何在這種格式的攻擊函數中調用玩家變量,我無法更改屬性,也不知道如何獲取玩家列表項以與敵人列表項進行比較並進行戰斗。

根據約定,self是通常與python中的對象相關的變量。
在您的情況下,要利用self,您應該閱讀python面向對象的編程。
https://realpython.com/python3-object-oriented-programming/

瀏覽鏈接中的教程,完成后,您將對使用代碼要實現的目標有更好的了解。 玩得開心!

暫無
暫無

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

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