簡體   English   中英

如何為 2 個元組創建檢查嵌套列表作為條件

[英]How to create check nested list for 2 tuples as a condition

我有一個任務是通過打印來編寫一個簡單的塔防游戲,我在編寫防御者單位來攻擊和怪物 HP 減值方面遇到了困難。 我正在嘗試創建一個 function 來檢查每一行的archr和zombi,如果是,則調用攻擊function並減去怪物HP,然后繼續下一行

這是我當前的代碼以及我的程序應該如何 function



import random

archer = {'shortform' : 'ARCHR',
          'name': 'Archer',
          'currentHP' : 5, 
          'maxHP': 5,
          'min_damage': 1,
          'max_damage': 4,
          'price': 5
          }

zombie = {'shortform': 'ZOMBI',
          'name': 'Zombie',
          'currentHP': 15,
          'maxHP': 15,
          'min_damage': 3,
          'max_damage': 6,
          'moves' : 1,
          'reward': 2
          }

field = [ [("ARCHR"," 5/5 "), None, None, None, ("ZOMBI", "15/15"), None, None],
          [None, ("ARCHR"," 5/5 "), None, None, None, None, ("ZOMBI", "15/15")],
          [None, None, ("ARCHR"," 5/5 "), None, None, ("ZOMBI", "15/15"), None],
          [None, ("ARCHR"," 5/5 "), None, None, ("ZOMBI", "15/15"), None, None],
          [("ARCHR"," 5/5 "), None, None, None, None, None, ("ZOMBI", "15/15")] ]

num_rows=len(field)
num_columns=len(field[0])
row_abcde="ABCDE"

def draw_field(field):
    print(' {:^7}{:^5}{:^7}'.format('1','2','3'))
    
    print(' ',end='')
    for column in range(num_columns): 
        print('+-----',end='')
    print('+')

    for row in range(num_rows):
        print('{}'.format(row_indexes[row]), end='') # print row index
        for column in range(num_columns): #for each column,
            element = field[row][column]      
            if element is None:      #print spacings
                print('|     ' , end='') 
            else:                     #print monster name
                print('|' + element[0], end='')
        print('|') #end row

        print(' ', end='') #spacing for 2nd | row 
        for column in range(num_columns):
            element = field[row][column]
            if element is None:  #print spacing agn
                print('|     ' , end='')
            else:                     #print monster helth
                print('|' + element[1], end='')
        print('|') #end row

        print(' ', end='') #spacing for letter
        for column in range(num_columns):
            print('+-----', end = '')
        print('+')#end of row

    return field


def defender_attack(defender_name, field, row, column):
    damage= random.randint(1,4)
    print('{} in lane {} shoots {} for {} damage!'.format(defender_name['shortform'], row_indexes[row], element['shortform'], damage))
    element['currentHP'] -= damage
    if element['currentHP'] <= 0:
         print('someone died')
    element = None
    return

#start of code

draw_field(field) 

for row in field:
    if 'ARCHR' and 'ZOMBI' in row: #im trying to check the list for archr and zombi, but i know they're in a tuple, how should i do this?
        
        for column in row:
            element = field[row][column][0]      
            if element == 'ZOMBI':
                element = zombie
                print(element)
                defender_attack(archer,field,[row][column])

draw_field(field)
     

for row in field:
    isArch, isZombi = False, False
    for cell in row:
        if cell: 
            name, hp = cell
            if name == 'ARCHR': isArch = True
            elif name == 'ZOMBI': isZombi = True
    if isArch and isZombi:
        #do_something

暫無
暫無

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

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