簡體   English   中英

加載的武器不會在循環內隨機重新加載

[英]Loaded weapon doesnt reload randomly inside loop

我決定在暑假期間挑戰自己用python創建一個基於txt的小游戲來練習編碼。

我面臨的問題是,當您攻擊並造成傷害時,它可以滾動並減少怪物 hp 的值,但無法在循環內重新滾動武器傷害。

不管攻擊多少次都是這樣

任何幫助將是最appriciated !

我使用一個類來創建武器(有一個單獨的武器列表,但添加了其中一個作為示例)

import random
class create_weapon:

    def __init__(self, damage, weapon_type):
        self.damage = damage
        self.weapon_type = weapon_type

    def __str__(self):
        return str(self.damage)

axe = create_weapon(random.randint(1, 12), 'Axe')

戰斗遭遇代碼:

from Spel.NPCS.Monsters import * # this contains the created monsters, created via a class etc.

current_monster = Bandit
player = Orc # i havent created a player entity yet so i used orc as a stand in
current_monster_hp = current_monster.health
current_player_hp = player.health

def load_dmg_player():
    current_player_weapon = player.weapon_armor.weapon.damage
    return current_player_weapon

current_player_weapon = load_dmg_player()

def combat_encounter_form():

    print('\nYou are facing ' + current_monster.name + ''' what will you do?\n
        1: Combat
        2: Wait and see
        3: Try to Flee
    ''')
    player_input_combat_form = input('\nWhat is your choice?: ')
    if player_input_combat_form == '1':
        combat()
    elif player_input_combat_form == '2':
        print('Works also')
    elif player_input_combat_form == '3':
        print('probably working?')
        
def combat():

    print('''\nCombat choices:\n
    1: Attack the creature!
    2: Change your mind and flee?
    ''')
    player_input_combat = input('\nWhat is your choice?: ')

    if player_input_combat == '1':
        while player_input_combat != 'Flee':
            global current_monster_hp
            global current_player_hp
            print('''\nHow do you attack?:\n
        1: Attack
        2: Block
        3: Try to flee
            ''')
            mini_combat_input = input('\nWhat is your choice?: ')
            if mini_combat_input == '1':
                current_monster_hp = current_monster_hp - current_player_weapon
                print('You dealt: ' + str(current_player_weapon) + ' damage!')
                print(current_monster_hp)

            elif mini_combat_input == '2':
                print('Block stuff goes here.')
            elif mini_combat_input == '3'
                print('Cant flee due to not enough coding')

    elif player_input_combat == '2':
        print('yes')

    elif player_input_combat == '3':
        print('kek')

當您調用create_weapon(random.randint(1, 12), 'Axe')random.randint將生成一個固定數字,我認為您在這里的目的是讓它始終生成一個隨機數。

有幾種方法可以處理它,但最通用的是傳入一個函數來計算傷害。

例如,用lambda替換初始化並用self.damage()替換對self.damage引用應該每次都給你隨機值:

In [22]: import random
    ...: class create_weapon:
    ...:
    ...:     def __init__(self, damage, weapon_type):
    ...:         self.damage = damage
    ...:         self.weapon_type = weapon_type
    ...:
    ...:     def __str__(self):
    ...:         return str(self.damage())
    ...:
    ...: axe = create_weapon(lambda: random.randint(1, 12), 'Axe')

In [23]: str(axe)
Out[23]: '1'

In [24]: str(axe)
Out[24]: '7'

In [25]: str(axe)
Out[25]: '9'

作為另一個風格點, import *可能會變得有點笨拙,因為它經常將很多東西轉儲到全局命名空間中。 您可能會考慮改變from Spel.NPCS.Monsters import *為類似import Spel.NPCS.Monsters as monsters ,然后你可以參考monsters.Orc等。

暫無
暫無

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

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