Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping issue
#1
How do I make it after it loops 670+ times it does not break. It stop working and starts to lag.
from time import sleep
from random import uniform, randint
from keyboard import wait, send, add_hotkey
from pyautogui import pixelMatchesColor, keyDown, keyUp, moveRel, position, click, alert, typewrite

fail_safe = 0
shut_down = 0
on_off = 0
click_one = 1
click_two = 1
move_one = 1
look_chance = 60#%
move_chance = 50#%

def toggle():
    global on_off
    global fail_safe
    first_cord, second_cord = position()
    if pixelMatchesColor(first_cord, second_cord, (0, 0, 0), tolerance=20) is True:
        fail_safe = 1
        print('STAY SAFE!')
    if pixelMatchesColor(first_cord, second_cord, (37, 0, 18), tolerance=10) is True:
        if on_off == 0:
            on_off = 1
            print('Script toggled OFF!')
        elif on_off == 1:
            on_off = 0
            print('Script toggled back ON!')
    else:
        return

def look():
    if on_off == 0:
        first_cord, second_cord = position()  
        if pixelMatchesColor(first_cord, second_cord, (95, 95, 0), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (148, 148, 0), tolerance=30) is True:
            moveRel(-50,0, duration=0.17)
        elif pixelMatchesColor(first_cord, second_cord, (0, 89, 0), tolerance=30) is True or pixelMatchesColor(first_cord, second_cord, (0, 45, 0), tolerance=30) is True:
            moveRel(40,0, duration=0.17)
        elif pixelMatchesColor(first_cord, second_cord, (46, 0, 93), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (36, 0, 72), tolerance=20) is True:
            moveRel(-110,0, duration=0.17)
        elif pixelMatchesColor(first_cord, second_cord, (178, 89, 0), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (197, 98, 0), tolerance=20) is True:
            moveRel(110,0, duration=0.17)
        else:
            return
    else:
        return

def attack():
    global click_two
    global click_one
    if on_off == 0:
        first_cord, second_cord = position()  
        if pixelMatchesColor(first_cord, second_cord, (186, 112, 186), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (135, 81, 135), tolerance=30) is True:
            chance = randint(1, 100)
            if chance <= look_chance:
                #print('Hit Chance ='+ str(chance))
                if click_two == 1:
                    click()
                    moveRel(3,2, duration=0.1)
                    click_two = 2
                else:
                    if click_two == 2:
                        click()
                        moveRel(-3,-2, duration=0.1)
                        click_two = 1
            elif click_one == 1:
                click()
                moveRel(-40,3, duration=0.17)
                click_one = 2
            else:
                if click_one == 2:
                    click()
                    moveRel(40,-3, duration=0.17)
                    click_one = 1
        else:
            return
    else:
        return

def move():
    global move_one
    if on_off == 0:
        first_cord, second_cord = position()  
        if pixelMatchesColor(first_cord, second_cord, (169, 56, 93), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (90, 18, 90), tolerance=30) is True or pixelMatchesColor(first_cord, second_cord, (203, 76, 127), tolerance=20) is True:
            chance = randint(1, 100)
            time_to_wait = uniform(0.12, 0.16)
            if chance <= move_chance:
                #print('Move Chance ='+ str(chance))
                if move_one == 1:
                    keyDown('a')
                    send('backspace')
                    sleep(time_to_wait)
                    keyUp('a')
                    move_one = 2
                elif move_one == 2:
                    keyDown('d')
                    send('backspace')
                    sleep(time_to_wait)
                    keyUp('d')
                    move_one = 1
        else:
            return
    else:
        return

while fail_safe == 0:
    shut_down += 1
    toggle()
    look()
    attack()
    move()
    if shut_down > 669:
        fail_safe = 1
Reply
#2
I doubt someone will look into 100+ lines of spaghetti code with globals and not a single line of comment. Post minimal runnable example that reproduce the problem.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
fixing it, make this in a 30min for an example

from time import sleep
from random import uniform, randint
from keyboard import wait, send, add_hotkey
from pyautogui import pixelMatchesColor, keyDown, keyUp, moveRel, position, click

fail_safe = 0
shut_down = 0
on_off = 0

def look(): #trys to see if a pixel im looking at is one of the 4 colors, moves if so.
    first_cord, second_cord = position()  
    if pixelMatchesColor(first_cord, second_cord, (95, 95, 0), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (148, 148, 0), tolerance=30) is True:
        moveRel(-50,0, duration=0.17)
    elif pixelMatchesColor(first_cord, second_cord, (0, 89, 0), tolerance=30) is True or pixelMatchesColor(first_cord, second_cord, (0, 45, 0), tolerance=30) is True:
        moveRel(40,0, duration=0.17)
    elif pixelMatchesColor(first_cord, second_cord, (46, 0, 93), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (36, 0, 72), tolerance=20) is True:
        moveRel(-110,0, duration=0.17)
    elif pixelMatchesColor(first_cord, second_cord, (178, 89, 0), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (197, 98, 0), tolerance=20) is True:
        moveRel(110,0, duration=0.17)
    else:
        return

def attack(): #trys to see if a pixel im looking at is true then has moves a lot or little of my mouse while clicking with the chance.
    click_one = 1
    click_two = 1
    look_chance = 60#%
    first_cord, second_cord = position()  
    if pixelMatchesColor(first_cord, second_cord, (186, 112, 186), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (135, 81, 135), tolerance=30) is True:
        chance = randint(1, 100)
        if chance <= look_chance:
            #print('Hit Chance ='+ str(chance))
            if click_two == 1:
                click()
                moveRel(3,2, duration=0.1)
                click_two = 2
            else:
                if click_two == 2:
                    click()
                    moveRel(-3,-2, duration=0.1)
                    click_two = 1
        elif click_one == 1:
            click()
            moveRel(-40,3, duration=0.17)
            click_one = 2
        else:
            if click_one == 2:
                click()
                moveRel(40,-3, duration=0.17)
                click_one = 1
    else:
        return

def move(): #trys to see if a pixel im looking at is true then has moves from side to side.
    move_one = 1
    move_chance = 50#%
    first_cord, second_cord = position()  
    if pixelMatchesColor(first_cord, second_cord, (169, 56, 93), tolerance=20) is True or pixelMatchesColor(first_cord, second_cord, (90, 18, 90), tolerance=30) is True or pixelMatchesColor(first_cord, second_cord, (203, 76, 127), tolerance=20) is True:
        chance = randint(1, 100)
        time_to_wait = uniform(0.12, 0.16)
        if chance <= move_chance:
            #print('Move Chance ='+ str(chance))
            if move_one == 1:
                keyDown('a')
                send('backspace')
                sleep(time_to_wait)
                keyUp('a')
                move_one = 2
            elif move_one == 2:
                keyDown('d')
                send('backspace')
                sleep(time_to_wait)
                keyUp('d')
                move_one = 1
    else:
        return

while fail_safe == 0:
    shut_down += 1
    if shut_down > 669:
        fail_safe = 1
    first_cord, second_cord = position()
    #trys to see if a pixel im looking at is bad and stops it.
    if pixelMatchesColor(first_cord, second_cord, (0, 0, 0), tolerance=20) is True:
        fail_safe = 1
    #trys to see if a pixel im looking at is true then toggles on and off.
    if pixelMatchesColor(first_cord, second_cord, (37, 0, 18), tolerance=10) is True:
        if on_off == 0:
            on_off = 1
            print('Script toggled OFF!')
        elif on_off == 1:
            on_off = 0
            print('Script toggled back ON!')
    if on_off == 0:
        look()
        attack()
        move()

If you run it and change
if shut_down > 669:
        fail_safe = 1
to

if shut_down > 750:
        fail_safe = 1
you will see it bug out

after around 5-6min
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Infinite Looping Issue nsadams87xx 1 1,393 Jun-15-2020, 12:13 AM
Last Post: SheeppOSU
  looping and indentation issue ameydiwanji 3 2,462 Jul-01-2019, 10:53 AM
Last Post: perfringo
  Looping issue, stops working JohnOsis 7 4,466 Apr-05-2018, 09:20 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020