Created
May 19, 2019 23:17
-
-
Save satetheus/35f4e943f6d31dafb37a915120a880d7 to your computer and use it in GitHub Desktop.
test of the detect_invader function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
from pyautogui import screenshot | |
from time import time | |
def timer(f): | |
def wrapper(): | |
start_time = time() | |
f() | |
elapsedTime = time() - start_time | |
print('In {} seconds.'.format(elapsedTime)) | |
return wrapper | |
@timer | |
def invader_detect_original(): | |
"""Takes a screen and looks for red clumps""" | |
image = screenshot() | |
red_count = 0 | |
pink_count = 0 | |
total = 0 | |
for pixel in image.getdata(): | |
total += 1 | |
if pixel[0] > 160 and pixel[1] < 55 and pixel[2] < 60: | |
red_count += 1 | |
elif pixel[0] > 150 and pixel[1] < 50 and pixel[2] > 150: | |
pink_count += 1 | |
image.save('temp.png') | |
if red_count > 1000: | |
print('Red Invader detected') | |
return True | |
elif pink_count > 50: | |
print('Pink Invader detected') | |
return True | |
else: | |
return False | |
@timer | |
def invader_detect(): | |
"""Takes a screenshot and looks for red clumps""" | |
image = screenshot(); image.save('temp.png') | |
red_count, pink_count = 0, 0 | |
total = 0 # not used | |
for pixel in image.getdata(): | |
total += 1 # not used | |
if pixel[0] > 100 and pixel[1] < 50: | |
if pixel[2] < 50: red_count += 1 | |
elif pixel[2] > 150: pink_count += 1 | |
if red_count > 1000: | |
print('Red Invader detected') | |
return True | |
elif pink_count > 25: | |
print('Pink Invader detected') | |
return True | |
return False | |
if __name__ == '__main__': | |
print('Original Function: ') | |
invader_detect_original() | |
print('New Function: ') | |
invader_detect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Picture 1:
Pink: true
red: true
Picture 2:
Pink: true
red: false
Picture 3:
Pink: false
red: false
Picture 4:
Pink: true
Red: true
Picture 5:
Pink: false
Red: false
Picture 6:
Pink: false
Red: true
Picture 7:
Pink: false
Red: false
Picture 8:
Pink: false
Red: true