zoukankan      html  css  js  c++  java
  • Python跳一跳小游戏

    一:准备工具

    PyCharm:专门用于Python开发的IDE工具

    实现原理

    • 获取手机的实时的截图
    • 点击起始位置和落地位置
    • 计算两个点的距离
    • 计算按压时间
    • 发送按压指令
    • 重新刷新手机截图

      1.安装此次程序所用到的模块(根据自己安装的Windows版本和python版本进行下载)。

      numpy-1.14.0rc1-cp36-none-win_amd64.whl

      Pillow-5.0.0-cp36-cp36m-win_amd64.whl

      matplotlib-2.1.1-cp36-cp36m-win_amd64.whl

      下载链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/

      使用cmd进入下载好的文件的目录,安装指令:(opencv是最后安装的)

      pip3.6 install C:python36Scripts umpy-1.14.0rc1-cp36-none-win_amd64.whl

      pip3.6 install C:python36ScriptsPillow-5.0.0-cp36-cp36m-win_amd64.whl

      pip3.6 install C:python36Scriptsmatplotlib-2.1.1-cp36-cp36m-win_amd64.whl

    • 2.结果
    • 3.代码部分

       
      # !/usr/bin/env python
      # -*- coding:utf-8 -*-
      __author__ = 'wechat'
      import os
      import PIL,numpy
      import matplotlib.pyplot as plt
      from matplotlib.animation import FuncAnimation
      import time
      
      need_update = True
      
      def get_screen_image():
          os.system('adb shell screencap -p /sdcard/screen.png')#获取当前界面的手机截图
          os.system('adb pull /sdcard/screen.png')#下载当前这个截图到当前电脑当前文件夹下
          return numpy.array(PIL.Image.open('screen.png'))
      
      def jump_to_next(point1, point2):#计算炫的长度
          x1, y1 = point1; x2, y2 = point2
          distance = ((x2-x1)**2 + (y2-y1)**2)**0.5
          os.system('adb shell input swipe 320 410 320 410 {}'.format(int(distance*1.35)))
      
      def on_calck(event, coor=[]):#绑定的鼠标单击事件
          global need_update
          coor.append((event.xdata, event.ydata))
          if len(coor) == 2:
              jump_to_next(coor.pop(), coor.pop())
          need_update = True
      
      def update_screen(frame):#更新图片 /从画图片
          global need_update
          if need_update:
              time.sleep(1)
              axes_image.set_array(get_screen_image())
              need_update = False
          return axes_image,
      
      figure = plt.figure()#创建一个空白的图片对象/创建一张图片
      axes_image = plt.imshow(get_screen_image(), animated=True)#把获取的图片话在坐标轴上面
      figure.canvas.mpl_connect('button_press_event', on_calck)
      ani = FuncAnimation(figure, update_screen, interval=50, blit=True)
      plt.show()
      View Code
  • 相关阅读:
    hdu 3072 Intelligence System(Tarjan 求连通块间最小值)
    HDU 4635 Strongly connected (Tarjan+一点数学分析)
    HDU 2767 Proving Equivalences (Tarjan)
    codeforce 853A Planning
    codeforces 851C Five Dimensional Points(鸽巢原理)
    Lucas模板
    LCA 模板
    hdu 2874 Connections between cities(st&rmq LCA)
    寒假练习赛总结
    ACM常用模板整理
  • 原文地址:https://www.cnblogs.com/study007/p/13196659.html
Copyright © 2011-2022 走看看