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‘)    #点击第二天继续

时间: 2024-11-13 01:37:31

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

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

  本章主要讲了python程序的调试,当程序有BUG或异常的时候,我们如何调试代码找出问题点.其实在本章之前的章节我们做练习的时候都会遇到各种各样的错语和异常,最初当不知道程序哪里出错的情况下不可否认的都使用了print语句进行输出并调试代码.没错print也是调试代码的一种工具,直观简单,便也有缺点,就是调试好后要进行删除,也是件麻烦事,于是就有了本章介绍的assert(断言),logging(日志)以及各种调试工具的出现. 首先来回顾一下python的异常. 一.python常见的异常类型

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

第8章实践项目之疯狂填词 创建一个一个疯狂填词(Mad Libs),程序,它将读入文本文件,并让用户在该文本文件中出现 ADJECTIVE,NOUN,VERB等单词的地方,加上他们自己的文本. 首先准备一个a.txt的文本文件 程序代码如下: #!/usr/bin/env python3.4 # coding:utf-8 # 8.9.2 import re f1 = open('a.txt','r') strf1 = f1.read() print("原文件内容为:") print(s

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

1 #!/usr/bin/env python3.5 2 # coding:utf-8 3 # 5.6.1 4 # 好玩游戏的物品清单 5 # 给定一个字典,包含物品名称和数量,并打印出数量对应的物品 6 7 dict_stuff = {'rope':1,'torch':6,'gold coin':42,'dagger':1,'arrow':12} 8 print("5.6.1参考答案") 9 print('=' * 80) 10 print("给定字典:",dic

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

1 #!/usr/bin/env python 2 # coding:utf-8 3 # write by mfyang 4 # collatz.py 5 # 从用户读入一个值,并判断这个值是不是一个int类型的整数,如果不是给出异常提示 6 # 如果这个值是偶数 那么让这个数//2 7 # 如果这个值是奇数 那么让这个数 ×3 + 1 8 # 通过不断调用函数的返回值 并打印这个返回值 直到这个返回值为1 9 10 num = raw_input("please input a number:

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

1 #!/usr/bin/env python3.5 2 # coding:utf-8 3 # 假定有一个列表,编写函数以一个列表值作为参数,返回一个字条串 4 # 该字符串包含所有表项,之间以逗号和空格分隔,并在最后一个值前插入 and 5 # 要求函数能处理传递给它的任何列表 6 7 # spam = input('please input a list:') 8 # 刚开始想从用户输入进行传递列表方式,但没成功 9 10 # 4.10.1 11 print("4.10.1 answer:&

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

#!/usr/bin/env python3.5 2 #coding:utf-8 3 # 4 # 这个项目主要目的是字符串的处理,简单格式化输出 5 tableData = [['apples','oranges','cherries','banana'], 6 ['Alice','Bob','Carol','David'], 7 ['dogs','cats','moose','goose']] 8 # 要求输出如下: 9 # apples Alice dogs 10 # dranges Bob

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

#!/usr/bin/env python # -*- coding:utf-8 -*- import os import re import urllib import json import socket import urllib.request import urllib.parse import urllib.error # 设置超时 import time timeout = 5 socket.setdefaulttimeout(timeout) class Crawler: # 睡

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

#! python3 # resizeAndAddLogo.py - Resizes all images in current working directory to fit # in a 300x300 square, and adds catlogo.png to the lower-right corner. import os from PIL import Image os.chdir('C:\\Users\\Administrator\\Python35-32\\test\\ku

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

import os,PyPDF2 os.chdir('D:\\My Documents') for folderName, subfolders, filenames in os.walk('D:\\My Documents'): for file in filenames: if file.endswith('.pdf'): pdfFile = open(file, 'rb') pdfReader = PyPDF2.PdfFileReader(pdfFile) pdfWriter = PyPD

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

#! python3 # Import modules and write comments to describe this program. import zipfile, os from PIL import Image from PIL import ImageFile #os.chdir('D:\\My Documents\\') ImageFile.LOAD_TRUNCATED_IMAGES = True for foldername, subfolders, filenames i