日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()

?

擴展思路:比如做個無人機實時定點,抓取目標……

參考文章:

一個簡單的導彈自動追蹤以及實時圖片旋轉算法

總結

以上是生活随笔為你收集整理的Py之pygame:有趣好玩—利用pygame库实现鱼儿自动实时目标跟踪(附完整代码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。