游戏机器人

#! python3
# -*- coding:utf-8 -*-
# robotPlay.py

import pyautogui, time, needBuy

# 食材和案板的位置
food = {'rice': (450, 460), 'nori': (392, 520), 'fishegg': (452, 517), 'desktop': (563, 514)}

# 空盘子的位置
plate = [(439, 338), (538, 339), (637, 336), 
		 (736, 333), (839, 339), (942, 334)]
pyautogui.PAUSE = 0.05
		 
print("-----你有3秒钟的时间调整好游戏界面-----")
print('\n ')
print('按下Ctrl-C 可以手动中止。')

try:
	time.sleep(3)	# 给3秒的准备时间
	# 如果游戏没结束就一直循环
	while pyautogui.locateOnScreen('robotToPlayGame\\end.png') == None and pyautogui.locateOnScreen('robotToPlayGame\\youWin.png') == None: 
		# 如果画面上有第一种寿司出现
		if pyautogui.locateOnScreen('robotToPlayGame\\shousi1.png') != None:
			pyautogui.click(food['rice'][0], food['rice'][1])
			pyautogui.click(food['nori'][0], food['nori'][1])
			pyautogui.click(food['rice'][0], food['rice'][1])
			pyautogui.click(food['desktop'][0], food['desktop'][1])
		# 如果画面上有第二种寿司出现
		if pyautogui.locateOnScreen('robotToPlayGame\\shousi2.png') != None:
			pyautogui.click(food['rice'][0], food['rice'][1])
			pyautogui.click(food['nori'][0], food['nori'][1])
			pyautogui.click(food['fishegg'][0], food['fishegg'][1])
			pyautogui.click(food['desktop'][0], food['desktop'][1])

		# 如果画面上有第三种寿司出现
		if pyautogui.locateOnScreen('robotToPlayGame\\shousi3.png') != None:
			pyautogui.click(food['rice'][0], food['rice'][1])
			pyautogui.click(food['fishegg'][0], food['fishegg'][1])
			pyautogui.click(food['nori'][0], food['nori'][1])
			pyautogui.click(food['fishegg'][0], food['fishegg'][1])
			pyautogui.click(food['desktop'][0], food['desktop'][1])
		
		# 收空盘子了
		for i in range(len(plate)):
			pyautogui.click(plate[i][0], plate[i][1]) 
			
		# 检查是否需要补充材料
		needBuy.buy()
		
	print('GG')
except KeyboardInterrupt:
	print('\n玩家主动中断游戏')
	

#! python3
# -*- coding:utf-8 -*-
# needBuy.py - 材料不够需要购买

import pyautogui, time

# 借助moveMouse.py获得打电话补充食材各个button的点击位置
phonecall = {'position': (942, 492), 
			 'Topping': {'position': (891, 401), 
						 'nori': {'position': (840, 399), 
								  'normal': (849, 425),
								  'close': (941, 463)}, 
						 'fishegg': {'position': (929, 409), 
						             'normal': (849, 425),
									 'close': (941, 463)}, 
						 'back': (914, 460), 
						 'close': (948, 461)},
			 'Rice': {'position': (883, 422), 
					  'rice': {'position': (900, 416),
							   'normal': (849, 425),
							   'close': (941, 463)},
					  'back': (863, 460), 
					  'close': (939, 467)},
			 'close': (943, 469)}

def buy():
	# 如果图片显示米饭为1,2或3
	if pyautogui.locateOnScreen('robotToPlayGame\\item12(2).png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item12.png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item12(3).png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item12(0).png') != None:
		print('缺少米饭, 购买中...')
		pyautogui.click(phonecall['position'][0], phonecall['position'][1])
		pyautogui.click(phonecall['Rice']['position'][0], phonecall['Rice']['position'][1])
		pyautogui.click(phonecall['Rice']['rice']['position'][0], phonecall['Rice']['rice']['position'][1])
		pyautogui.click(phonecall['Rice']['rice']['normal'][0], phonecall['Rice']['rice']['normal'][1])
		time.sleep(0.1)
		pyautogui.click(phonecall['Rice']['rice']['close'][0], phonecall['Rice']['rice']['close'][1])
	# 如果图片显示海苔为1,2或3
	if pyautogui.locateOnScreen('robotToPlayGame\\item21(2).png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item21.png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item21(3).png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item21(0).png') != None:
		print('缺少海苔, 购买中...')
		pyautogui.click(phonecall['position'][0], phonecall['position'][1])
		pyautogui.click(phonecall['Topping']['position'][0], phonecall['Topping']['position'][1])
		pyautogui.click(phonecall['Topping']['nori']['position'][0], phonecall['Topping']['nori']['position'][1])
		pyautogui.click(phonecall['Topping']['nori']['normal'][0], phonecall['Topping']['nori']['normal'][1])
		pyautogui.click(phonecall['Topping']['nori']['close'][0], phonecall['Topping']['nori']['close'][1])
		time.sleep(0.1)
	# 如果图片显示鱼子为1,2或3
	if pyautogui.locateOnScreen('robotToPlayGame\\item22(2).png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item22.png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item22(3).png') != None\
	or pyautogui.locateOnScreen('robotToPlayGame\\item22(0).png') != None:
		print('缺少鱼子, 购买中...')
		pyautogui.click(phonecall['position'][0], phonecall['position'][1])
		pyautogui.click(phonecall['Topping']['position'][0], phonecall['Topping']['position'][1])
		pyautogui.click(phonecall['Topping']['fishegg']['position'][0], phonecall['Topping']['fishegg']['position'][1])
		pyautogui.click(phonecall['Topping']['fishegg']['normal'][0], phonecall['Topping']['fishegg']['normal'][1])
		pyautogui.click(phonecall['Topping']['fishegg']['close'][0], phonecall['Topping']['fishegg']['close'][1])
		time.sleep(0.1)
		
	pyautogui.click(phonecall['Topping']['fishegg']['close'][0], phonecall['Topping']['fishegg']['close'][1])


#! python3
# mouseNow.py - Displays the mouse cursor's current position.
import pyautogui
print('Press Ctrl-C to quit.')

try:
	while True:
		# Get and print the mouse coordinates
		x, y = pyautogui.position()
		positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
		pixelColor = pyautogui.screenshot().getpixel((x, y))
		positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3) + ', '\
		+ str(pixelColor[1]).rjust(3) + ', '\
		+ str(pixelColor[2]).rjust(3) + ')'
		print(positionStr, end = '')
		print('\b' * len(positionStr), end = '', flush = True)
except KeyboardInterrupt:
	print('\nDone.')

游戏地址:https://www.miniclip.com/games/sushi-go-round/cs/focus/

游戏地址:Sushi Go Round

使用的图像都是从游戏中截取的, 通过第一关没有问题

后面的关卡只要增加寿司种类配方和截图就好

 

你可能感兴趣的:(Python)