遊戲機器人

#! 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

使用的圖像都是從遊戲中截取的, 通過第一關沒有問題

後面的關卡只要增加壽司種類配方和截圖就好

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章