mardi 5 mai 2020

Unable to Use List Slices in For Loop, with if, elif

Attempting a simple automation of castle & cannon game wherein I might create a List of Possible Combinations of coordinates, then send this list into PyAutoGUI module to direct the cannon fire to destroy a fortress.

Time & Ammunition are constraints.

import pyautogui as pag
import itertools
flatten = itertools.chain.from_iterable

pag.click(955, 96)

topRow = [1, 2, 3]  # blocks in the top row
middleRow = [4, 5, 6]
bottomRow = [7, 8, 9]
fireAway = [20]

combo = []
for a in topRow:
    for b in middleRow:
        for c in bottomRow:
            for d in fireAway:
                combo.append([a, b, c, d])
barrage = list(flatten(combo))

fire_1 = barrage[0::4] # slicing combo by target row
fire_2 = barrage[1::4]
fire_3 = barrage[2::4]
reload = barrage[3::4] # insert cannon reload command after each round of shots

for salvo in barrage:
    if 1 in fire_1:
        pag.click(1010, 570),
    elif 2 in fire_1:
        pag.click(1075, 570),
    elif 3 in fire_1:
        pag.click(1140, 570),
    if 4 in fire_2:
        pag.click(1010, 540),
    elif 5 in fire_2:
        pag.click(1075, 540),
    elif 6 in fire_2:
        pag.click(1140, 540),
    if 7 in fire_3:
        pag.click(1010, 500),
    elif 8 in fire_3:
        pag.click(1075, 500),
    elif 9 in fire_3:
        pag.click(1140, 500),
    if 20 in reload:
        pag.click(1420, 733, 1, 5),
        pag.click(1340, 500, 1, 5),

each print function returns the expected results

 print(fire_2)  # test the results
    [4, 4, 5, 5, 4, 4, 5, 5, ...]
    print(barrage)  # test the result
    [1, 4, 7, 20, 1, 4, 8, 20, 1, 5, 7, 20, 1, 5, 8, 20, ...]

However what I am unable to accomplish, or reproduce here, are the PyAutoGUI actions, in order. This continually acts upon only the first three items of each slice, continually firing only upon the coordinates (1, 4, 7), for a total of 108 cycles, which is one complete Salvo for every Item in (List)Barrage...?

How would I go about improving this code to achieve: (firing upon) (1, 4, 7, reload), (1, 4, 8, reload), (1, 4, 9, reload), etc.... through all possible combinations ?

Thank You for Any Input or Assistance !

Aucun commentaire:

Enregistrer un commentaire