python导弹自动追踪以及实时图片旋转算法(吕万友)
2017-12-21 11:19
465 查看
以下是 Python-pygame代码实现:
代码中需要的照片:
分别命名为: rect1.png 、bomb.png
import sys, pygame from math import * pygame.init() font1 = pygame.font.SysFont('microsoftyaheimicrosoftyaheiui', 23) textc = font1.render('*', True, (250, 0, 0)) # this is the target that missile wants to attack screen = pygame.display.set_mode((800, 700), 0, 32) # set the attribution of screen missile = pygame.image.load('C:/Users/win10/Desktop/rect1.png').convert_alpha() bomb = pygame.image.load('C:/Users/win10/Desktop/bomb.png').convert_alpha() height = missile.get_height() width = missile.get_width() pygame.mouse.set_visible(0) x1, y1 = 100, 600 # 导弹的初始发射位置 velocity = 800 # 导弹速度 time = 1 / 1000 # 每个时间片的长度 clock = pygame.time.Clock() A = () B = () C = () while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() clock.tick(300) x, y = pygame.mouse.get_pos() # 获取鼠标位置,鼠标就是需要打击的目标 distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2)) # 两点距离公式 print('distance : ', distance) section = velocity * time # 每个时间片需要移动的距离 sina = (y1 - y) / distance cosa = (x - x1) / distance angle = atan2(y - y1, x - x1) # 两点间线段的弧度值 fangle = degrees(angle) # 弧度转角度 x1, y1 = (x1 + section * cosa, y1 - section * sina) missiled = pygame.transform.rotate(missile, -(fangle)) # 得到旋转角度后的missile图片 if 0 <= -fangle <= 90: A = (width * cosa + x1 - width, y1 - height / 2) B = (A[0] + height * sina, A[1] + height * cosa) if 90 < -fangle <= 180: A = (x1 - width, y1 - height / 2 + height * (-cosa)) B = (x1 - width + height * sina, y1 - height / 2) if -90 <= -fangle < 0: A = (x1 - width + missiled.get_width(), y1 - height / 2 + missiled.get_height() - height * cosa) B = (A[0] + height * sina, y1 - height / 2 + missiled.get_height()) if -180 < -fangle < -90: A = (x1 - width - height * sina, y1 - height / 2 + missiled.get_height()) B = (x1 - width, A[1] + height * cosa) C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2) screen.fill((0, 0, 0)) if distance > 1: screen.blit(missiled, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1]))) # 旋转后的missile 位块显示在 C点位置。 screen.blit(textc, (x, y)) # 鼠标用一个红色*代替 else: screen.blit(bomb, (x, y)) # 旋转后的missile 位块显示在 C点位置。 pygame.display.update()
代码中需要的照片:
分别命名为: rect1.png 、bomb.png
相关文章推荐
- Unity导弹自动追踪算法
- 根据权重决定展示哪张图片的小算法(python+java)板砖
- iOS拍照之后图片自动旋转90度解决办法
- 采用Python实现自动发送摄像头图片到微博
- ply python 图片压缩 图片裁剪 旋转
- android中图片压缩以及图片旋转的方法
- android 照相以及关于自动生成的图片分辨率和面积太小问题 (转)
- opencv+python 巧用霍夫变换实现图片旋转角度计算
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- iOS中(相册)摄像头获取的图片上传至服务器被自动旋转了
- 如何设置图片以及plist文件,使能够自动识别ipad以及iphone
- imageAnt中的图片放大缩小以及旋转的优化
- python学习笔记(一)爬虫实战:图片自动下载器
- python2转3格式自动工具:2to3 , 以及python3相对于python2 修改的一些内容
- [ios开发]锁屏后的相机的方向检查,与图片的自动旋转
- android中图片压缩以及图片旋转的方法
- iOS 归档UIImageView后,再解档,imageview的图片出现自动旋转
- 采用Python实现自动发送摄像头图片到微博
- cocos2d-x3.0中图片分辨率自动适配设置以及源码,原理分析。
- 使用Python导出Excel图表以及到处为图片的方法