生活随笔
收集整理的這篇文章主要介紹了
Python键鼠模拟,DNF自动刷图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
聲明:
這篇文章本來是和上一遍關(guān)于DNF掃拍的文章在一起的,但是隨著后續(xù)代碼的增加刷圖功能涉及的東西太多,都擠在一篇文章中太長也太亂,最后還是單獨(dú)出來,不定期更新新的功能和原先的功能的迭代版本。
有這方面興趣的可以一起交流學(xué)習(xí),每個(gè)人的不同思路都是進(jìn)步的源泉。
再次聲明,本文章僅作學(xué)習(xí)交流使用,對(duì)產(chǎn)生的任何影響,本人概不負(fù)責(zé).
轉(zhuǎn)載請(qǐng)注明出處:https://editor.csdn.net/md?articleId=103674748
基于、依賴
1.語言
本文使用Python語言來實(shí)現(xiàn)。
2.鍵鼠模擬
鍵鼠模擬可以使用win32api,最好使用硬件模擬。
自己封裝為適合本腳本調(diào)用的函數(shù):
1,鍵盤:模擬按鍵,按下,抬起,組合按鍵
2. 鼠標(biāo):按下,抬起,單擊,雙擊,拖動(dòng)
3.圖色識(shí)別、深度學(xué)習(xí)
python有很多個(gè)圖片處理庫,這也正是python腳本的優(yōu)勢之處本文主要使用以下兩種
1.PIL
2.CV2
3.numpy
4.pytorch
這些庫功能強(qiáng)大,本人了解深點(diǎn)的也僅限于使用到的這些方向,當(dāng)然也可以使用其他圖片處理庫,看個(gè)人喜好了,比如調(diào)用大漠等等。PIL、openCV用來處理圖片
主要功能函數(shù):
1.找圖,找色
2.找字
3.圖片識(shí)別
3.圖片內(nèi)容提取
4.深度學(xué)習(xí)識(shí)別
以上條件具備后我們就可以正式開發(fā)腳本了。
依賴的模塊
一.圖片獲取及識(shí)別處理模塊
二.鍵鼠模擬模塊
三.運(yùn)行與停止控制模塊
四.尋路模塊
五:業(yè)務(wù)邏輯處理模塊
由于這里的代碼共九百多行就簡單展示下,后續(xù)調(diào)用這些封裝實(shí)現(xiàn)腳本功能。
以下為代碼展示。因本人業(yè)余時(shí)間有限,人力也有限,需完善的細(xì)節(jié)處理還有很多未完成。
1.導(dǎo)入各模塊并創(chuàng)建保存數(shù)據(jù)的類
每個(gè)識(shí)別后的物體基本信息采用類保存。
from random
import randint
import utils
.set_canshu
as CS
from time
import sleep
, time
from yaml
import safe_load
from core
import shibie
, model_run
, Block
, KeyBord
, set_window
, HotKey
, get_ski
from math
import sqrtbo
= KeyBord
()
no_off
= HotKey
()
VK_UP
, VK_DOWN
, VK_LEFT
, VK_RIGHT
= 'a_u', 'a_d', 'a_l', 'a_r'
with open('../data/fb.shi', errors
='ignore') as f
:f
= safe_load
(f
)names
= f
['names']BUFF
= f
["buff"]ski_boss
= f
['s_boss']zhiye
= f
['zhiye']ski1
= f
['q_ski']ski2
= f
['f_x_ski']ONE_KEY_UP
= f
['ONE_KEY_UP'] REV
= f
['REV'] hit_off
= f
['GIT_OFF'] class Block:def __init__(self
, name
, x
, y
, w
, h,x_s
):self
.name
= nameself
.x
= xself
.y
= yself
.w
= wself
.h
= hself
.x_s
= x_s
def __str__(self
):return f"{self.name}xy:{self.x, self.y},wh:{self.w,self.h},s_x{self.x_s}"def __eq__(self
, other
):return self
.x
== other
.x
and self
.y
== other
.y
2.房間找門
判斷門是否開啟,找到這個(gè)門返回終點(diǎn)坐標(biāo)
class Search:def __init__(self
):self
.DATA
= Noneself
.f_x
= None self
.tuijian_f_x
= None self
.start
= None self
.is_door
= None self
.prev_men
= None self
.prev_min_me
= None self
.last_att
= None self
.ski
= None self
.g_center
= None self
.guai
= None self
.is_boss
= False self
.min_boss
= Noneself
.min_door
= Noneself
.boss_prev
= False self
.pl
= 0def men(self
): self
.is_door
= Nonefor k
, v
in self
.dt
.items
():if k
.endswith
('門'):for s
in v
:if s
.conf
< 60:print(65, '相似度', s
, s
.conf
)continueif self
.__f_x
== VK_DOWN
:if s
.y
< 490 or k
== '時(shí)空門':returns
.y
+= 40 if s
.y
> 600:s
.y
= 600s
.x
+= s
.w
* 0.5if self
.__f_x
== VK_UP
:if k
== '時(shí)空門' or s
.y
> 450:returns
.y
-= 70 s
.x
+= s
.w
* 0.5 if self
.__f_x
== VK_LEFT
:if s
.x
> 400:returns
.y
-= 35if self
.__f_x
== VK_RIGHT
:if s
.x
<= 400:returns
.x
+= s
.w
if s
.x
> 800:s
.x
= 800s
.y
-= 35if not self
.is_door
:self
.is_door
= s
continueif s
.conf
> self
.is_door
.conf
:self
.is_door
= s
return self
.is_door
3.房間找怪
找到怪物返回終點(diǎn)坐標(biāo)
def monsters(self
, is_off
=hit_off
):self
.guai
= Noneself
.g_center
= Nonefor k
, v
in self
.dt
.items
():if k
.startswith
('g'):for s
in v
:if s
.y
> 550: s
.y
= 510if not self
.guai
:self
.guai
= s
continueif abs(sqrt
((self
.hero
.x
- s
.x
) ** 2 + (self
.hero
.y
- s
.y
) ** 2)) < abs(sqrt
((self
.hero
.x
- self
.guai
.x
) ** 2 + (self
.hero
.y
- self
.guai
.y
) ** 2)):self
.guai
= s
else:self
.g_center
= s
if not self
.guai
:returnif self
.guai
.name
.endswith
('浮空'):self
.guai
.y
+= 50if self
.g_center
:self
.g_center
.x
= (self
.guai
.x
+ self
.g_center
.x
) / 2self
.g_center
.y
= (self
.guai
.y
+ self
.g_center
.y
) / 2if self
.hero
.x
<= self
.guai
.x
: self
.last_att
= VK_RIGHTself
.guai
.x
-= is_off
return self
.guai
if self
.hero
.x
> self
.guai
.x
+ self
.guai
.w
or self
.hero
.x
>= self
.guai
.x
: self
.last_att
= VK_LEFTself
.guai
.x
+= is_off
return self
.guai
def get_end(self
, data
):self
.DATA
= dataself
.s_fenjie
()self
.new_room
() if self
.men
():self
.prev_men
= self
.is_doorself
.men_quyu
= self
.is_door
.nameself
.tuijian_f_x
= Nonereturn self
.is_door
if self
.monsters
():if self
.g_center
:self
.guai
= self
.g_centerself
.tuijian_f_x
= Nonereturn self
.guai
return self
.__tuijian_end
4.圖內(nèi)判斷邏輯
對(duì)數(shù)據(jù)進(jìn)行處理,判定各種情況。
注意此時(shí)人物位置為起點(diǎn),其他位置為終點(diǎn)
class Auto:def __init__(self
):set_window
() self
.DATA
= {} self
.img0
= Noneself
.tong
= Noneself
.buff
= Noneself
.dev
, self
.model
= model_run
()def _move(self
):self
.end
= se
.get_end
(self
.DATA
) if se
.tuijian_f_x
:se
.f_x
= se
.tuijian_f_x
if move
.move
(se
.start
, self
.end
, se
.f_x
) == 2: se
.prev_men
= Noneif se
.guai
:se
.guai
= Nonese
.ski
= get_ski
(self
.img0
, ski1
, ski2
)se
.put_skills
(se
.is_boss
)returnif se
.is_door
:if not se
.new_room
():move
.update_self
() def hei_ping(self
):if self
.DATA
.get
("黑屏"):move
.r
, move
.d
, move
.l
, move
.u
= False, False, False, Falsebo
.down_up
(0)sleep
(0.5)return Truedef _tongguan(self
):res
= boss
.is_tongguan
(self
.DATA
)if self
.tong
and self
.DATA
.get
('小地圖boss'):boss
.xiu
= Noneself
.tong
= Nonereturnif res
== 2:self
.tong
= Truemove
.r
, move
.d
, move
.l
, move
.u
= False, False, False, Falsese
.is_boss
= Falseself
.buff
= Falsereturn res
5.主函數(shù)
def main(self
):while True:off
= no_off
.on_off
()if not off
:move
.r
, move
.d
, move
.l
, move
.u
= False, False, False, Falseif off
is False:bo
.down_up
(0)returnbo
.down_up
(0)sleep
(1)continueself
.DATA
, self
.img0
= shibie
(self
.dev
, self
.model
)if self
.hei_ping
(): continueif self
._tongguan
(): sleep
(1)continueself
._move
()
6.效果展示
9c1d5bf1
9月更新不再使用圖色識(shí)別,改用深度學(xué)習(xí)識(shí)別,可使用yolov5模型,更改為適用游戲內(nèi)的識(shí)別如上視頻展示
不定期持續(xù)更新中…
總結(jié)
以上是生活随笔為你收集整理的Python键鼠模拟,DNF自动刷图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。