对于游戏编程来说,使用脚本语言可以快速地开发游戏逻辑,节省开发新型自定义语言的时间和开销,并且脚本语言通常要比编程者创造的语言更加的强大。小伙伴们学习python有一段时间了,有没有发现python语言的很强大和简洁,并且很容易嵌入,是一个不错的脚本语言呢?今天我们就用python做个游戏脚本练练手。
以4399的一款叫做(玩命打地鼠)的游戏作为案例,实现自动打地鼠的功能~
代码:
from?time?import?sleep from?pymouse?import?PyMouse from?PIL?import?Image,ImageGrab from?selenium?import?webdriver import?time url?=?"http://HdhCmsTest4399测试数据/flash/178030_3.htm" #9个坑所在的矩形图的坐标 coordinate?=?(395,530,1065,930) #x,y坐标,n为缩放倍数的倒数,a为需要结合缩放倍数的横坐标,b为需要结合缩放倍数的纵坐标 x,y=0,0 n=1/1.5 a,b=0,0 #9个打地鼠的坐标 loc1?=?(488,562) loc2?=?(721,581) loc3?=?(969,578) loc4?=?(466,732) loc5?=?(725,706) loc6?=?(1000,707) loc7?=?(469,878) loc8?=?(716,869) loc9?=?(975,877) Loc_list?=?[(488,562)] Loc_list.append(loc2) Loc_list.append(loc3) Loc_list.append(loc4) Loc_list.append(loc5) Loc_list.append(loc6) Loc_list.append(loc7) Loc_list.append(loc8) Loc_list.append(loc9) def?touch(x,y,mouse=1): ????a?=?x*n ????b?=?y*n ????m.click(int(a),int(b),mouse) ???? ??? class?GameScript: ????def?__init__(self): ????????chrome?=?webdriver.Chrome('D:/googledriver/chromedriver.exe') ????????chrome.maximize_window() ????????chrome.get(url) ????????chrome.implicitly_wait(30)? ???????? ????def?FlashOpen(self): ????????touch(1660,80) ????????touch(1400,217) ????????#等网页加载出来,点击允许 ????????sleep(3) ????????touch(1482,469) ????????#关闭页面 ????????sleep(1) ????????touch(707,25) ????????#授权FLASH ????????sleep(1) ????????touch(723,700) ????????sleep(1) ????????touch(476,290) ????????#关闭游戏声音 ????????touch(168,26,2) ????????sleep(1) ????????touch(282,258) ???? ????def?start(self): ????????#点击开始游戏按钮 ????????touch(750,800) ????????touch(1075,322) ????????sleep(1) ???????? ????def?CutImage(self): ????????image?=?ImageGrab.grab(coordinate) ????????width,height?=?image.size ????????#用于存放九张小图的左上和右下坐标 ????????box_list?=?[] ????????#小图的宽 ????????cut_width?=?int(width/3) ????????#小图的高 ????????cut_height?=?int(height/3) ????????#分切9图,先获得9组crop函数需要的坐标,再用crop函数截出来 ????????for?i?in?range(0,3): ????????????for?j?in?range(0,3): ????????????????box?=?(j*cut_width,?i*cut_height,?(j+1)*cut_width,?(i+1)*cut_height) ????????????????box_list.append(box)?? ????????image_list?=?[image.crop(box)?for?box?in?box_list] ????????#返回的列表里面为图片 ????????return?image_list #使两张图片统一格式,方便比较 def?Get_Same_Image(image): ????size?=?(256,256) ????return?image.resize(size).convert('RGB') #比较两张图片的直方图,以获得相似度 def?Difference(list1,list2): ????sum1?=?0 ????for?i?in?range(len(list1)): ????????if(list1[i]?==?list2[i]): ????????????sum1?+=?1 ????????else: ????????????#依照公式可获得 ????????????sum1?+=?1-(abs(list1[i]?-?list2[i])?/?max(list1[i],list2[i])?) ????return?sum1?/?len(list1) ???? def?Get_Similarity(image1,image2): ????#统一格式 ????img1?=?Get_Same_Image(image1) ????img2?=?Get_Same_Image(image2) ????#获得直方图 ????list1?=?img1.histogram() ????list2?=?img2.histogram() ????return?Difference(list1,?list2) def?AutoPlay(image_list): ????Timage?=?Image.open("C:/Users/Fatzj/Desktop/game?script/yangping.png") ????for?n?in?range(len(image_list)): ????????#相似度大于0.45就拍一下 ????????if(Get_Similarity(image_list[n],?Timage)?>=?0.45): ????????????#从Loc_list获得要点击的坐标 ????????????x?=?Loc_list[n][0] ????????????y?=?Loc_list[n][1] ????????????touch(x,y) ????????????#移开锤子,避免干扰对比 ????????????touch(1075,322) ????????????????????? ???????? if?__name__?==?"__main__": ????m?=?PyMouse() ????demo?=?GameScript() ????demo.FlashOpen() ????sleep(25) ????demo.start() ????time1?=?0 ????while(time1?<?60): ????????start?=?time.perf_counter() ???????? ????????image_list?=?demo.CutImage() ????????AutoPlay(image_list) ???????? ????????end?=?time.perf_counter() ????????time1?+=?end-start ????print("结束运行")
以上就是自动打地鼠的游戏脚本啦,是不是挺有趣的,用python制作游戏脚本并没有那么难,勤加练习,你会发现更多关于python制作脚本的技巧的。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did181105