欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程语言 > c/c >内容正文

c/c

c 代码好玩-凯发k8官方网

发布时间:2024/10/8 c/c 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 c 代码好玩_py之pygame:有趣好玩—利用pygame库实现鱼儿自动实时目标跟踪(附完整代码)... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

py之pygame:有趣好玩—利用pygame库实现鱼儿自动实时目标跟踪(附完整代码)

目录

输出结果

实现代码


输出结果

实现代码

#py之pygame:利用pygame库实现鱼儿自动实时目标跟踪import pygame,sys from math import * pygame.init() font1=pygame.font.sysfont('microsoftyaheimicrosoftyaheiui',23) textc=font1.render('.',true,(250,0,0)) screen=pygame.display.set_mode((800,700),0,32) missile=pygame.image.load('f:/file_python/resources/fish02.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)) section=velocity*time sina=(y1-y)/distancecosa=(x-x1)/distanceangle=atan2(y-y1,x-x1) fangle=degrees(angle) x1,y1=(x1 section*cosa,y1-section*sina)missiled=pygame.transform.rotate(missile,-(fangle))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))screen.blit(missiled, (x1-width (x1-c[0]),y1-height/2 (y1-c[1])))screen.blit(textc, (x,y)) pygame.display.update()

扩展思路:比如做个无人机实时定点,抓取目标……

参考文章:

一个简单的导弹自动追踪以及实时图片旋转算法

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是凯发k8官方网为你收集整理的c 代码好玩_py之pygame:有趣好玩—利用pygame库实现鱼儿自动实时目标跟踪(附完整代码)...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图