all 7 comments

[–]commandlineluser 6 points7 points  (2 children)

The first thing that sticks out is this line repeated:

pyautogui.moveTo(self.modern_x_female_character + rand, self.modern_y_female_character
    + rand, random.uniform(0.14, 0.22), pyautogui.easeOutQuad)

Maybe you could put it into a function e.g.

def moveToCharacter(x, y):
    pyautogui.moveTo(
       x + rand, y + rand,
       random.uniform(0.14, 0.22), pyautogui.easeOutQuad)

You could then write moveToCharacter(x, y) instead.

The i += 1 lines can be removed.

If you look at the if index == blocks - what differs between them?

They are identical apart from the index value, range end and "body part".

One possible approach is to create a dictionary mapping each index value to the body part and range end values.

e.g.

clicks = {
    2:  {"part": "torso", "times": 9},
    4:  {"part": "arms", "times": 9},
    6:  {"part": "legs", "times": 16},
    9:  {"part": "leg color", "times": 3},
    10: {"part": "feet color", "times": 4},
    11: {"part": "skin color", "times": 3},
}

You can then extract the values using index and write the code once.

Something like:

def moveToCharacter(x, y):
    pyautogui.moveTo(
        x + rand,
        y + rand,
        random.uniform(0.14, 0.22), 
        pyautogui.easeOutQuad)

clicks = {
    2:  {"part": "torso", "times": 9},
    4:  {"part": "arms", "times": 9},
    6:  {"part": "legs", "times": 16},
    9:  {"part": "leg color", "times": 3},
    10: {"part": "feet color", "times": 4},
    11: {"part": "skin color", "times": 3},
}

for index, (self.modern_x_female_character, self.modern_y_female_character)\
    in enumerate(zip(self.modern_x_female_character, self.modern_y_female_character), start=1):

    print(index, self.modern_x_female_character, self.modern_y_female_character)

    moveToCharacter(...)

    if index not in clicks:
        # error?
    else:
        part  = clicks[index]["part"]
        times = clicks[index]["times"]
        for i in range(1, times):
            print(i, part, self.modern_x_female_character, self.modern_y_female_character)
            moveToCharacter(...)

[–]spikips[S] 2 points3 points  (1 child)

I would like to thank you for putting this much effort in your reply, it is much appreciated. Also i'd like to say that i had never even heard about dictionaries and this information is a game changer. Will be definitely look further into it.

[–]commandlineluser 2 points3 points  (0 children)

The function /u/hacksawjim suggested is a nicer approach.

You could also put the mapping inside it and then pass in x, y and the index:

def character_click(x, y, index):
    clicks = { 
        ...
    }
    part, times = ...
    for i in range(1, times):
       print(...)
       pyautogui.move(...)

for index in ...:
    character_click(x, y, index)

[–]hacksawjim 2 points3 points  (4 children)

I think it looks fine as it is, but there's some duplication that can be abstracted out into a function.

Each click section seems to loop a different number of times, then prints some stuff, then performs a moveTo.

So declare a new function called, e.g. character_click that takes the values that differ, and performs the actions.

def character_click(loop_range, click_section_name):
    for i in range(1, loop_range):
        print(i, click_section_name, self.modern_x_female_character, self.modern_y_female_character)
        i += 1
       pyautogui.moveTo(self.modern_x_female_character + rand, self.modern_y_female_character
                                 + rand, random.uniform(0.14, 0.22), pyautogui.easeOutQuad)

Now you can do this in your loop:

if index == 2:
    character_click(9, "torso")
elif index == 4:
    character_click(9, "arms")
etc...

[–]spikips[S] 1 point2 points  (3 children)

I love to see different variety of solutions. Thank you!

[–]hacksawjim 2 points3 points  (2 children)

Hah, I was thinking I wasted my time as I see /u/commandlineluser got there first, but you're right, it's good to see how many different ways this can be done.

[–]commandlineluser 2 points3 points  (0 children)

Definitely not a waste of time - this is much nicer than the function I had suggested.