相关文件

想学Python的小伙伴可以关注小编的公众号【Python日志】
有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!!
需要源码的小伙伴可以在公众号回复射击游戏
Python源码、问题解答学习交流群:773162165

开发环境

Python版本:3.7.8
相关模块:
time
mss
numpy
_init_paths
cv2
opts
multiprocessing
以及一些python自带的模块。

环境搭建

安装Python并添加到环境变量,pip安装需要的相关模块即可。

效果展示


代码实现

单线程瞄准射击代码

'''
单线程瞄准射击代码
'''from __future__ import absolute_import  #绝对引用
from __future__ import division
from __future__ import print_function
import torch
import pyautogui
from input_controllers.native_win32_input_controller import NativeWin32InputController
from win32dll_input import Mouseimport mss
import time
import numpy as np
import cv2
import _init_pathsfrom input_controller import InputController, MouseButton, KeyboardKey, character_keyboard_key_mappingimport os
import cv2
from opts import opts
from detectors.detector_factory import detector_factorynames  = ['enemy_0_head', 'enemy_0_body', 'enemy_1_head', 'enemy_1_body', 'enemy_2_head','enemy_2_body', 'hostage_0']test={"force":True}
def draw_res(img,names, results,show_txt=False,enable_bot=False):heads_loc =[]body_loc = []for j in range(1, len(names) + 1):for bbox in results[j]:if bbox[4] > 0.3:bbox = np.array(bbox, dtype=np.int32)# cat = (int(cat) + 1) % 80cat = int(j-1)# print('cat', cat, self.names[cat])head_c = (0,0,255)body_c = (255,0,0)c = (0,255,0)if not enable_bot:head_c =cbody_c =c#print('cat', cat, self.names[cat])conf = bbox[4]txt = '{}{:.1f}'.format(names[cat], conf)font = cv2.FONT_HERSHEY_SIMPLEXcat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]if "head" in txt:img = cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), head_c, 2)heads_loc.append((int((bbox[0]+bbox[2])/2),int((bbox[1]+bbox[3])/2)))elif 'body' in txt:img = cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), body_c, 2)body_loc.append((int((bbox[0]+bbox[2])/2),int((bbox[1]+bbox[3])/2)))if show_txt:img = cv2.rectangle(img,(bbox[0], bbox[1] - cat_size[1] - 2),(bbox[0] + cat_size[0], bbox[1] - 2), c, -1)img =cv2.putText(img, txt, (bbox[0], bbox[1] - 2),font, 0.5, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)return  img,heads_loc,body_locdef demo(opt):os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_stropt.debug = max(opt.debug, 1)Detector = detector_factory[opt.task]detector = Detector(opt)input = NativeWin32InputController()mouse = Mouse()detector.pause = Falseenable_bot= Truepyautogui.FAILSAFE = False#mouse.move_mouse((0,0))pyautogui.move(0,0)mouse.move_mouse((0,0))time.sleep(1)input.click(**test)input.click(**test)#pyautogui.click(clicks=2)with mss.mss() as sct:# Part of the screen to capturemonitor = {"top": 0, "left": 0, "width": 900, "height": 650}cv2.namedWindow("OpenCV/Numpy normal")cv2.moveWindow("OpenCV/Numpy normal", 1020, 10)pre_pos = (0, 0)while "Screen capturing":last_time = time.time()  #初始化点位# Get raw pixels from the screen, save it to a Numpy arrayimg = np.array(sct.grab(monitor))[:,:,0:3]ret = detector.run(img)results = ret['results']img,heads,bodys = draw_res(img,names,results,show_txt=False,enable_bot=enable_bot)if enable_bot:print("bodys",bodys)for body in bodys:shot_x = body[0]shot_y = body[1]shot_x =int(shot_x/1000.0*800)shot_y = int(shot_y/750.0*600)shot = (shot_x,shot_y)mouse.move_mouse(shot)mouse.double_click(shot)#input.click(**test)#input.click(**test)#mouse.click(body,button_name='right')input.click(button=MouseButton.RIGHT,duration=0.05, **test)#pyautogui.rightClick()cv2.imshow("OpenCV/Numpy normal", img)#print('pred time:',ret['tot'])# Display the pictureprint("fps: {}".format(1 / (time.time() - last_time)))# Press "q" to quitk =cv2.waitKey(2)if  k == ord("q"):cv2.destroyAllWindows()breakelif k== ord("s"):enable_bot = not  enable_botif __name__ == '__main__':args = ["ctdet","--load_model","C:\\MyData\\AI_game\\model_last_res_18.pth","--arch","res_18","--head_conv","64"]opt = opts().init(args=args)demo(opt)

多线程瞄准-射击代码

'''
多线程瞄准-射击代码
'''from __future__ import absolute_import  #绝对引用
from __future__ import division
from __future__ import print_function
import numpy as np
import mss
import time
import multiprocessing
from multiprocessing import Pipe
from win32dll_input import Mouse
from input_controller import InputController, MouseButton, KeyboardKey, character_keyboard_key_mapping
import os
import cv2
from opts import opts
from detectors.detector_factory import detector_factory
from input_controllers.native_win32_input_controller import NativeWin32InputControllerdisplay_time = 2  #每隔dispaly_time显示一次fps
title = "aimbot"
fps =0
pos_scale=1.25   #重要!!!屏幕放大分辨率,游戏鼠标灵敏度必须调为0
names  = ['enemy_0_head', 'enemy_0_body', 'enemy_1_head', 'enemy_1_body', 'enemy_2_head','enemy_2_body', 'hostage_0']
test={"force":True}sct = mss.mss()
mouse = Mouse()
calibShotPos = (0,0)  #没有目标时在左上角放空枪用于校准射击点
start_time = time.time()
monitor = {"top": 0, "left": 0, "width": int(800*pos_scale)-100, "height": int(600*pos_scale)-50}
args = ["ctdet","--load_model", "C:\\MyData\\AI_game\\model_last_res_18.pth","--arch", "res_18","--head_conv", "64"
]
opt = opts().init(args=args)
os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str
opt.debug = max(opt.debug, 1)
Detector = detector_factory[opt.task]
detector = Detector(opt)
input = NativeWin32InputController()
mouse = Mouse()
detector.pause = Falsedef draw_res(img,names, results,show_txt=False,enable_bot=False):heads_loc =[]body_loc = []#cv2.circle(img, shotPos2PixelLoc(calibShotPos), 22, (0, 255, 255), thickness=2)  # 用于校正射击点#cv2.circle(img, shotPos2PixelLoc(calibShotPos2), 22, (0, 255, 255), thickness=2)  # 用于校正射击点for j in range(1, len(names) + 1):for bbox in results[j]:if bbox[4] > 0.3:bbox = np.array(bbox, dtype=np.int32)# cat = (int(cat) + 1) % 80cat = int(j-1)# print('cat', cat, self.names[cat])head_c = (124,252,0)body_c = (127,255,212)c = (0,255,0)if not enable_bot:head_c =cbody_c =c#print('cat', cat, self.names[cat])conf = bbox[4]txt = '{}{:.1f}'.format(names[cat], conf)font = cv2.FONT_HERSHEY_SIMPLEXcat_size = cv2.getTextSize(txt, font, 0.5, 2)[0]if "head" in txt:img = cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), head_c, 1)heads_loc.append((int((bbox[0]+bbox[2])/2),int((bbox[1]+bbox[3])/2)))elif 'body' in txt:img = cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), body_c, 1)body_loc.append((int((bbox[0]+bbox[2])/2),int((bbox[1]+bbox[3])/2)))if show_txt:img = cv2.rectangle(img,(bbox[0], bbox[1] - cat_size[1] - 2),(bbox[0] + cat_size[0], bbox[1] - 2), c, -1)img =cv2.putText(img, txt, (bbox[0], bbox[1] - 2),font, 0.5, (0, 0, 0), thickness=1, lineType=cv2.LINE_AA)return  img,heads_loc,body_locdef grab_screen(p_input):while True:# Get raw pixels from the screen, save it to a Numpy array ,and convert BGRA to BGRimg = np.array(sct.grab(monitor))[:,:,0:3]p_input.send(img)def show_screen(p_output2):global fps, start_timecv2.namedWindow(title)cv2.moveWindow(title, 1000, 10)while True:img = p_output2.recv()cv2.imshow(title, img)fps+=1TIME = time.time() - start_timeif (TIME) >= display_time :print("FPS: ", fps / (TIME))fps = 0start_time = time.time()# Press "q" to quitif cv2.waitKey(3) & 0xFF == ord("q"):cv2.destroyAllWindows()break
def shot(target:tuple):shot_x = target[0]shot_y = target[1]shot_x = int(shot_x / pos_scale)shot_y = int(shot_y / pos_scale)shot = (shot_x, shot_y)# print('mouse loc:',shot_x,shot_y)mouse.move_mouse(shot)input.click(**test)input.click(**test)input.click(button=MouseButton.RIGHT, duration=0.05, **test)def pixelLoc2shotPos(target:tuple):shot_x = target[0]shot_y = target[1]shot_x = int(shot_x / pos_scale)shot_y = int(shot_y / pos_scale)shot = (shot_x, shot_y)return  shot
def shotPos2PixelLoc(pos:tuple):pos_x = pos[0]pos_y = pos[1]loc_x = int(pos_x / pos_scale)loc_y = int(pos_y /pos_scale)loc = (loc_x, loc_y)return  locdef detection(p_output,p_intput2):mouse.move_mouse((0,0))input.click(**test)while True:img = p_output.recv()ret = detector.run(img)results = ret['results']showimg, heads, bodys = draw_res(img, names, results, show_txt=False, enable_bot=True)p_intput2.send(showimg)if len(bodys) is 0:shot(calibShotPos)#shot(calibShotPos2)else:for body in bodys:shot(body)if len(bodys)<2:for head in heads:shot(head)if __name__=="__main__":p_output,p_input = Pipe()p_output2,p_input2 = Pipe()# creating new processesp1 = multiprocessing.Process(target=grab_screen, args=(p_input, ))p2 = multiprocessing.Process(target=detection, args=(p_output,p_input2, ))p3 = multiprocessing.Process(target=show_screen, args=(p_output2, ))# starting our processesp1.start()p2.start()p3.start()

公众号:Python日志
需要源码的小伙伴可以在公众号回复射击游戏
Python源码、问题解答学习交流群:773162165

【Python游戏】用Python基于centernet在win10平台开发,射击游戏 | 附带源码相关推荐

  1. 计算机毕业设计Java基于的电商平台的设计与实现(源码+系统+mysql数据库+lW文档)

    计算机毕业设计Java基于的电商平台的设计与实现(源码+系统+mysql数据库+lW文档) 计算机毕业设计Java基于的电商平台的设计与实现(源码+系统+mysql数据库+lW文档) 本源码技术栈: ...

  2. Python+Django实现基于人脸识别的门禁管理系统,附带源码!!

    已下项目为实战开发经验,微信搜索关注公众号 [ python语言空间 ],获取更多项目源码及资源. 项目介绍 基于人脸识别的门禁管理系统(Python+Django+RESTframework+Jso ...

  3. python基于django校园信息管理平台设计与实现(项目源码+视频录制+截图)

    实现校园 学生老师账号.通知公告.校园新闻动态.学生老师论文.学生毕业设计等数据的发布和管理. 3种类型的账号:管理员.老师.学生, 均使用该接口进行登录. 前端发送的登录请求中包含账号.密码. 后端 ...

  4. Python爬取腾讯动漫全站漫画详细教程(附带源码)

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:merlin& PS:如有需要Python学习资料的小伙伴可 ...

  5. 经典《像素鸟》游戏,难道你不想自己动手开发一个嘛(附源码免费下载)

    经典的飞机大战游戏,难道你不想自己动手开发一个嘛! 语言:Java 代码的编译软件:Eclipse 如果你用的也是eclipse的话 直接导入文件 就可以 ,如果不是的话,自己建一个项目,将代码cop ...

  6. 经典《飞机大战》游戏,难道你不想自己动手开发一个嘛(附源码免费下载)

    经典的飞机大战游戏,难道你不想自己动手开发一个嘛! 语言:Java 代码的编译软件:Eclipse 如果你用的也是eclipse的话  直接导入文件 就可以  ,如果不是的话,自己建一个项目,将代码c ...

  7. 基于SSM框架图书管理系统开发与设计(附源码资料)-毕业设计

    文章目录 1. 适用人群 2. 你将收获 3.项目简介 4.技术实现 5.系统功能 5.1.管理员身份登录 5.1.1.登录 5.1.2.管理员登录首页 5.1.3.借阅管理 5.1.4.图书管理 5 ...

  8. Python+Vue计算机毕业设计基于的营养配餐评价系统d6255(源码+程序+LW+部署)

    该项目含有源码.文档.程序.数据库.配套开发软件.软件安装教程 项目运行环境配置: Python3.7.7+Django+Mysql5.7+pip list+HBuilderX(Vscode也行)+V ...

  9. 物联网平台搭建的全过程介绍(五)——基于阿里云物联网平台的Android聊天app源码

    本例程Android源码请点此处免费下载 物联网平台搭建的全过程介绍(四)两台设备之间通过云数据流转实现远程通信之Android studio例程中介绍了两台Android设备通过物联网平台进行通信的 ...

最新文章

  1. “WPF老矣,尚能饭否”—且说说WPF今生未来(上):担心
  2. 竟有如此沙雕的代码注释!
  3. double类型数值计算出现误差的解决办法
  4. mtk camera 移植步骤
  5. mysql协议重传_TCP协议详解
  6. 16 --删除排序数组中的重复项
  7. groupby索引有效吗_SQL IN 一定走索引吗?
  8. 博文写作——摘要摘要图标
  9. 权威可信 | 华为云云测通过中国电子技术标准院软件测试工具能力评价
  10. 快速理解孤儿进程和僵尸进程
  11. MFC关闭对话框可以不析构吗_Win10关闭Defender和防火墙
  12. 源生php怎么打印,PHP如何实现云打印
  13. LightOJ 1419 – Necklace 用m个颜色去涂n个球(环状) 要求相邻可同色
  14. Java框架学习顺序是哪些
  15. 信而泰 X-Snapper测试系统,助力家庭路由器IPv6支持度测试
  16. 十年优秀网络玄幻小说大点评(推荐)
  17. android 自定义locale,关于android:设置Locale.setDefault(locale)后,如何获取手机语言?...
  18. 系统调用【简单总结】
  19. oracle循环数据字典,Oracle DUL的工作原理和技术实现
  20. 中望3D 2021 倒圆角

热门文章

  1. aquarius数据库建模配置详解
  2. Datawhale组队学习周报(第048周)
  3. 安装Docker所遇到的问题
  4. 如何设置计算机自动连接宽带,宽带自动连接设置,小编教你电脑怎么设置宽带自动连接...
  5. 数据预处理常用方法流程
  6. Qt中LineEdit编辑框限制数字输入整理
  7. 关于TI的28335芯片概述
  8. [转]阿里云的这群疯子
  9. 鸿蒙harmonyOS 方舟框架ARK使用ets的页面间转场动画的使用
  10. 谷粒商城高级篇(36)——商品上架之上传数据到Elasticsearch