How to activate 2 if’s at the same time in Python

Asked

Viewed 194 times

0

I’m making a bot that plays Guitarflash (Guitar Hero browser) alone, only when I need him to press both keys of notes at the same time (I use pyautogui to press the keys and Imagegrab to detect the note) he presses one at a time instead of the 2 together

import time

from PIL import ImageGrab
import pyautogui

"""
Resolução:1366 x 768
Guitar Flash 1

"""

def capture_screen():
    screen = ImageGrab.grab()
    return screen

def detect_green(screen): #pegar o pixel das cores
    color = screen.getpixel((493, 652))
    if color == (255, 255, 255):
        return True

def green(): 
    pyautogui.press("1")

def detect_red(screen): 
    color = screen.getpixel((582, 652))
    if color == (255, 255, 255):
        return True

def red(): 
    pyautogui.press("2")

def detect_yellow(screen):
    color = screen.getpixel((672, 652))
    if color == (255, 255, 255):
        return True

def yellow():
    pyautogui.press("3")

def detect_blue(screen):
    color = screen.getpixel((761, 652))
    if color == (255, 255, 255):
        return True

def blue():
    pyautogui.press("9")

def detect_orange(screen):
    color = screen.getpixel((851, 652))
    if color == (255, 255, 255):
        return True

def orange():
    pyautogui.press("0")

def active_special(screen):
    color = screen.getpixel((867, 572))
    if color == (0, 141, 255):
        return True

def special():
    pyautogui.press("space")

print('3')
time.sleep(1)
print('2')
time.sleep(1)
print('1')
time.sleep(1)
print('Start')


while True:
    screen = capture_screen()
    if detect_green(screen):
        green()
    if detect_red(screen):
        red()
    if detect_yellow(screen):
        yellow()
    if detect_blue(screen):
        blue()
    if detect_orange(screen):
        orange()
    if active_special(screen):
        special()

Would there be any way to make him press the 2 buttons together? He is detecting however when he gives the command to press goes one at a time.

  • you can use the and to make two equal comparisons

  • I tried to do this, only then it activates twice, because it detects the normal if and

1 answer

0


To press 2 buttons together, just do pyautogui.hotkey('1', '2'). This causes it to press in order and release in reverse order as described here.

Now, just take what colors are active and move on to the pyautogui.hotkey():

while True:        
    if detect_green(screen):
        colors_detected.append("1")
    if detect_red(screen):
        colors_detected.append("2")
    if detect_yellow(screen):
        colors_detected.append("3")
    if detect_blue(screen):
        colors_detected.append("9")
    if detect_orange(screen):
        colors_detected.append("0")
    if active_special(screen):
        colors_detected.append("space")

    pyautogui.press(*colors_detected)

Notice what to do *lista (unpacking) allows me to pass each element of the list as an argument.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.