'''
小游戏别踩黑块http://www.4399.com/flash/138560_3.htm
获取屏幕坐标、截图、分析每块的颜色,确定点击位置
'''
import os
import time
import pyautogui
from pymouse import PyMouse
class Game:
def __init__(self):
self.pic_name = 'snap{}.png'
self.sleep_start_time = 1.74
# self.sleep_start_time = 1.1
self.snap_position_x = 784
self.snap_position_y = 629
self.one_pic_pos = (50,68)
self.two_pic_pos = (150,68)
self.three_pic_pos = (250,68)
self.four_pic_pos = (350,68)
def click(self,x,y,m):
m.click(self.snap_position_x+x,self.snap_position_y+y)
def run(self):
m = PyMouse()
# print(m.position()) # 获取鼠标当前坐标
m.click(988, 620)
m.click(988, 620)
time.sleep(self.sleep_start_time)
# while True:
i = 0
while True:
print('================================')
im = pyautogui.screenshot(region=(self.snap_position_x, self.snap_position_y, 399, 135)) # 左上,宽高
im.save(self.pic_name.format(i))
one = im.getpixel(self.one_pic_pos)
two = im.getpixel(self.two_pic_pos)
three = im.getpixel(self.three_pic_pos)
four = im.getpixel(self.four_pic_pos)
print(one)
print(two)
print(three)
print(four)
if one[0]>50 and one[0] <200:
os.remove(self.pic_name.format(i))
break
if one == two == three:
self.click(self.four_pic_pos[0], self.four_pic_pos[1],m)
# m.move(self.four_pic_pos[0], self.four_pic_pos[1])
print('点4')
elif one == two == four:
self.click(self.three_pic_pos[0], self.three_pic_pos[1],m)
# m.move(self.three_pic_pos[0], self.three_pic_pos[1])
print('点3')
elif two == three == four:
self.click(self.one_pic_pos[0], self.one_pic_pos[1],m)
# m.move(self.one_pic_pos[0], self.one_pic_pos[1])
print('点1')
elif one == three == four:
self.click(self.two_pic_pos[0], self.two_pic_pos[1],m)
# m.move(self.two_pic_pos[0], self.two_pic_pos[1])
print()
print('点2')
time.sleep(0.35)
os.remove(self.pic_name.format(i))
i+=1
print('================================')
if __name__ == '__main__':
Game().run()