Home > Blockchain >  How do I print an element into a grid box of 2 spaces?
How do I print an element into a grid box of 2 spaces?

Time:08-06

I am making a tower defence strategy game, similar to plants vs zombie.

The code I have so far works nicely however I can't get the zombie to be substituted in the space within the grid whilst having the name displayed above the hp so that it fits in the box.

letter = ['A1', 'B1', 'C1', 'D1', 'E1', 'A2', 'B2', 'C2', 'D2', 'E2', 'A3', 'B3', 'C3', 'D3', 'E3']
field = [[' ', ' ', ' ', ' ', ' ', ' ', ' '.format(letter[0],letter[1], letter[2])],\
         [' ', ' ', ' ', ' ', ' ', ' ', ' '.format(letter[3],letter[4], letter[5])],\
         [' ', ' ', ' ', ' ', ' ', ' ', ' '.format(letter[6],letter[7], letter[8])],\
         [' ', ' ', ' ', ' ', ' ', ' ', ' '.format(letter[9],letter[10], letter[11])],\
         [' ', ' ', ' ', ' ', ' ', ' ', ' '.format(letter[12],letter[13], letter[14])]]

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

zcurrentHP = zombie ['maxHP']

def randomlane():
#zombie spawns once anywhere in the last lane.
    from random import randint
    for mob in range(1):
        c_x = randint(0, len(field)-1)
        c_y = randint((6), len(field[0])-1)
        z =  zombie['shortform']   str(zcurrentHP)   '/'   str(zombie['maxHP'])
        field[c_x][c_y] = z
    return

def draw_field():
#format number horizontally above grid and letters vertically on left side of grid
    letters = ['A', 'B', 'C', 'D', 'E']
    numbers = [1, 2, 3]
    i = 0
    print("     %d     %d     %d" % (numbers[0], numbers[1],numbers[2]))

#printing the field
    for row in range(len(field)):
        print('   ----- ----- ----- ----- ----- ----- ----- ')
        print(letters[i], end = ' ')
        i  = 1
        for col in range(len(field[0])):
            print('|', field[row][col], end = '   ')
        print('|')
        randomlane()
        for col in range(len(field[0])):
            print('  |', field[row][col], end = ' ')
        print('  |') 
    print('   ----- ----- ----- ----- ----- ----- ----- ')
    return

draw_field()

here is a sample output:

     1     2     3
   ----- ----- ----- ----- ----- ----- ----- 
A |     |     |     |     |     |     | ZOMBI15/15   |
  |     |     |     |     |     |     | ZOMBI15/15   |
   ----- ----- ----- ----- ----- ----- ----- 
B |     |     |     |     |     |     | ZOMBI15/15   |
  |     |     |     |     |     |     | ZOMBI15/15   |
   ----- ----- ----- ----- ----- ----- ----- 
C |     |     |     |     |     |     | ZOMBI15/15   |
  |     |     |     |     |     |     | ZOMBI15/15   |
   ----- ----- ----- ----- ----- ----- ----- 
D |     |     |     |     |     |     | ZOMBI15/15   |
  |     |     |     |     |     |     | ZOMBI15/15   |
   ----- ----- ----- ----- ----- ----- ----- 
E |     |     |     |     |     |     | ZOMBI15/15   |
  |     |     |     |     |     |     | ZOMBI15/15   |
   ----- ----- ----- ----- ----- ----- ----- 

But this is how I want it to look:

    1     2     3
  ----- ----- ----- ----- ----- ----- ----- 
A|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
B|     |     |     |     |     |     |ZOMBI|
 |     |     |     |     |     |     |15/15|
  ----- ----- ----- ----- ----- ----- ----- 
C|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
D|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 
E|     |     |     |     |     |     |     |
 |     |     |     |     |     |     |     |
  ----- ----- ----- ----- ----- ----- ----- 

I do apologize for any inconvenience in advance.

CodePudding user response:

Well, if it's always 5 and 5, the answer is pretty easy:

    for row in range(len(field)):
        print('   ----- ----- ----- ----- ----- ----- ----- ')
        print(letters[i], end = ' ')
        i  = 1
        for col in range(len(field[0])):
            print('|', field[row][col][:5], end = '   ')
        print('|')
        randomlane()
        for col in range(len(field[0])):
            print('  |', field[row][col][5:], end = ' ')
        print('  |') 
    print('   ----- ----- ----- ----- ----- ----- ----- ')
    return
  • Related