python编程快速上手之第18章实践项目参考答案(18.14.3)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python编程快速上手之第18章实践项目参考答案(18.14.3)相关的知识,希望对你有一定的参考价值。

#! python3
# encoding: UTF-8
import pyautogui, os, time
from PIL import ImageGrab, ImageOps
import os
import time
import pyautogui
from numpy import *


foodOnHand = {shrimp:5,           #设定食材初始数量
              rice:10,
              nori:10,
              roe:10,
              salmon:5,
              unagi:5}
 
def screenGrab():
    box = ()
    im = pyautogui.screenshot()
    #im.save(os.getcwd() + ‘\\\\full_snap__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)

def grab():
    box = (x_pad + 1,y_pad+1,x_pad+640,y_pad+480)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    return a

def main():                       #开始整个项目
    startGame()                   #开始游戏
    while True:
        check_bubs()              #检查座位
 

def startGame():
    #location of first menu
    pyautogui.click(541, 374, button=left)  #点开始
    time.sleep(.1)
     
    #location of second menu
    pyautogui.click(542, 610, button=left)   #点继续
    time.sleep(.1)
     
    #location of third menu
    pyautogui.click(804, 645, button=left)   #点跳过教程
    time.sleep(.1)
     
    #location of fourth menu
    pyautogui.click(548, 572, button=left)   #点继续
    time.sleep(.1)

class Cord:           #设置按键位置,包括加食材、购买食材
    f_shrimp = (259,527)  
    f_rice = (315, 524)
    f_nori = (256, 582)
    f_roe = (311, 583)
    f_salmon = (256, 636)
    f_unagi = (315, 637)

    phone = (802, 560)

    menu_toppings = (771, 460)

    t_shrimp = (690, 395)
    t_nori = (690, 451)
    t_roe = (774, 455)
    t_salmon = (690, 510)
    t_unagi = (774, 399)
    t_exit = (815, 522)
 
    menu_rice = (770, 481)
    buy_rice = (769, 474)
     
    delivery_norm = (722, 487)

def clear_tables():      #设置清理桌面点击位置
    pyautogui.click(310, 403, button=left)
    pyautogui.click(410, 403, button=left)
    pyautogui.click(510, 403, button=left)
    pyautogui.click(610, 403, button=left)
    pyautogui.click(710, 403, button=left)
    pyautogui.click(810, 403, button=left)


def makeFood(food):     #制作寿司
    if food == caliroll:
        print (Making a caliroll)
        foodOnHand[rice] -= 1     #相应食材减少
        foodOnHand[nori] -= 1
        foodOnHand[roe] -= 1
        pyautogui.click((Cord.f_rice), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_nori), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_roe), button=left)
        time.sleep(.1)
        foldMat()            #点竹垫,包寿司,具体方法见后面
        time.sleep(1.5)
     
    elif food == onigiri:
        print (Making a onigiri)
        foodOnHand[rice] -= 2 
        foodOnHand[nori] -= 1
        pyautogui.click((Cord.f_rice), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_rice), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_nori), button=left)
        time.sleep(.1)
        foldMat()
        time.sleep(.05)
         
        time.sleep(1.5)
 
    elif food == gunkan:
        print (Making a gunkan)
        foodOnHand[rice] -= 1 
        foodOnHand[nori] -= 1 
        foodOnHand[roe] -= 2 
        pyautogui.click((Cord.f_rice), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_nori), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_roe), button=left)
        time.sleep(.05)
        pyautogui.click((Cord.f_roe), button=left)
        time.sleep(.1)
        foldMat()
        time.sleep(1.5)

sushiTypes = {5132:onigiri,    #三种寿司对应识别颜色相加值作为唯一识别索引
              6943:caliroll,
              4205:gunkan,}

def foldMat():
    pyautogui.click(486, 511, button=left)  #点竹垫,包寿司
    time.sleep(.1)

def buyFood(food):                    #购买食物
     
    if food == rice:
        pyautogui.click((Cord.phone), button=left)
        time.sleep(.1)
        pyautogui.click((Cord.menu_rice), button=left)
        time.sleep(.05)
        
        s = pyautogui.screenshot()
        if s.getpixel(Cord.buy_rice) != (127, 127, 127):   #通过鼠标位置的颜色核对购买条件是否满足
            print (rice is available)
            pyautogui.click((Cord.buy_rice), button=left)
            time.sleep(.1)            
            pyautogui.click((Cord.delivery_norm), button=left)
            time.sleep(.1)
            foodOnHand[rice] += 10            
            time.sleep(2.5)
        else:
            print (rice is NOT available)
            pyautogui.click(870,530, button=left)            
            time.sleep(1)
            buyFood(food)
             
 
             
    if food == nori:
        pyautogui.click((Cord.phone), button=left)
        time.sleep(.1)        
        pyautogui.click((Cord.menu_toppings), button=left)
        time.sleep(.05)        
        s = pyautogui.screenshot()
        print (test)
        time.sleep(.1)
        if s.getpixel(Cord.t_nori) != (33, 30, 11):
            print (nori is available)
            pyautogui.click((Cord.t_nori), button=left)
            time.sleep(.1)            
            pyautogui.click((Cord.delivery_norm), button=left)
            time.sleep(.1)
            foodOnHand[nori] += 10            
            time.sleep(2.5)
        else:
            print (nori is NOT available)
            pyautogui.click((Cord.t_exit), button=left)            
            time.sleep(1)
            buyFood(food)
 
    if food == roe:
        pyautogui.click((Cord.phone), button=left)
        time.sleep(.1)        
        pyautogui.click((Cord.menu_toppings), button=left)
        time.sleep(.05)
        
        s = pyautogui.screenshot()
         
        time.sleep(.1)
        if s.getpixel(Cord.t_roe) != (127, 61, 0):
            print (roe is available)
            pyautogui.click((Cord.t_roe), button=left)
            time.sleep(.1)            
            pyautogui.click((Cord.delivery_norm), button=left)
            time.sleep(.1)
            foodOnHand[roe] += 10 
            time.sleep(2.5)
        else:
            print (roe is NOT available)
            pyautogui.click((Cord.t_exit), button=left)            
            time.sleep(1)
            buyFood(food)

def checkFood():                              #检查食材数量
    for i, j in foodOnHand.items():
        if i == nori or i == rice or i == roe:
            if j <= 4:                        #食材少于4个进行购买
                print (%s is low and needs to be replenished % i)
                buyFood(i)

def get_seat_one():                  #匹配座位一的气泡
    box = (247,252,247+60,252+20)   #注意每个座位的气泡核对旷大小必须一致,否则无法核对寿司需求
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()      #以三原色相加作为索引核对需求
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_one__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a
 
def get_seat_two():             #匹配座位二的气泡
    box = (348,252,348+60,252+20)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_two__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a
 
def get_seat_three():         #匹配座位三的气泡
    box = (449,252,449+60,252+20)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_three__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a
 
def get_seat_four():         #匹配座位四的气泡
    box = (550,252,550+60,252+20)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_four__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a
 
def get_seat_five():         #匹配座位五的气泡
    box = (651,252,651+60,252+20)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_five__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a
 
def get_seat_six():        #匹配座位六的气泡
    box = (752,252,752+60,252+20)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_six__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a

def get_seat_continue():    #匹配进入下一关弹窗
    box = (348,560,348+40,560+20)
    im = ImageOps.grayscale(ImageGrab.grab(box))
    a = array(im.getcolors())
    a = a.sum()
    print (a)
    #im.save(os.getcwd() + ‘\\\\seat_six__‘ + str(int(time.time())) + ‘.png‘, ‘PNG‘)    
    return a
 
def get_all_seats():
    get_seat_one()
    get_seat_two()
    get_seat_three()
    get_seat_four()
    get_seat_five()
    get_seat_six()
    get_seat_continue()




class Blank:     #当无用户需求时相应位置颜色索引
    seat_1 = 6123
    seat_2 = 11727
    seat_3 = 12482
    seat_4 = 12086
    seat_5 = 10434
    seat_6 = 6416
    
    
def check_bubs():    #检查气泡
 
    checkFood()    #核对食材是否足够
    s1 = get_seat_one()
    if s1 != Blank.seat_1:   #气泡1不为空
        if s1 in sushiTypes:   #匹配气泡1的需求
            print (table 1 is occupied and needs %s % sushiTypes[s1])
            makeFood(sushiTypes[s1])   #制作相应寿司
        else:
            print (sushi not found!\\n sushiType = %i % s1) 
    else:
        print (Table 1 unoccupied)
 
    clear_tables()
    checkFood()
    s2 = get_seat_two()
    if s2 != Blank.seat_2:
        if s2 in sushiTypes:
            print (table 2 is occupied and needs %s % sushiTypes[s2])
            makeFood(sushiTypes[s2])
        else:
            print (sushi not found!\\n sushiType = %i % s2) 
    else:
        print (Table 2 unoccupied)
 
    checkFood()
    s3 = get_seat_three()
    if s3 != Blank.seat_3:
        if s3 in sushiTypes:
            print (table 3 is occupied and needs %s % sushiTypes[s3])
            makeFood(sushiTypes[s3])
        else:
            print (sushi not found!\\n sushiType = %i % s3) 
    else:
        print (Table 3 unoccupied)
 
    checkFood()
    s4 = get_seat_four()
    if s4 != Blank.seat_4:
        if s4 in sushiTypes:
            print (table 4 is occupied and needs %s % sushiTypes[s4])
            makeFood(sushiTypes[s4])
        else:
            print (sushi not found!\\n sushiType = %i % s4) 
    else:
        print (Table 4 unoccupied)
 
    clear_tables()
    checkFood()
    s5 = get_seat_five()
    if s5 != Blank.seat_5:
        if s5 in sushiTypes: 
            print (table 5 is occupied and needs %s % sushiTypes[s5])
            makeFood(sushiTypes[s5])
        else:
            print (sushi not found!\\n sushiType = %i % s5) 
    else:
        print (Table 5 unoccupied)
 
    checkFood()
    s6 = get_seat_six()
    if s6 != Blank.seat_6:
        if s6 in sushiTypes: 
            print (table 1 is occupied and needs %s % sushiTypes[s6])
            makeFood(sushiTypes[s6])
        else:
            print (sushi not found!\\n sushiType = %i % s6) 
    else:
        print (Table 6 unoccupied) 
    clear_tables()

    s7 = get_seat_continue()      
    if s7 == 1252 :               #用颜色索引核对是否弹窗要求进行下一关
      pyautogui.click(521,573, button=left)    #点击继续
      time.sleep(.5)
      pyautogui.click(521,573, button=left)    #点击第二天继续
    
    

技术分享

以上是关于python编程快速上手之第18章实践项目参考答案(18.14.3)的主要内容,如果未能解决你的问题,请参考以下文章

python编程快速上手之第18章实践项目参考答案(18.4.2)

python编程快速上手之第3章实践项目参考答案

python编程快速上手之第6章实践项目参考答案

python编程快速上手之第10章实践项目参考答案(11.11.2)

python编程快速上手之第15章实践项目参考答案(17.7.2)

python编程快速上手之第5章实践项目参考答案