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

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

目录

输出结果

实现代码


输出结果

9c195277563dfecc93e5cca98c280fc5.png

38c47bbb0c6dc69ce5b4801196849700.png

实现代码

#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)/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))
    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()

38c47bbb0c6dc69ce5b4801196849700.png

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

参考文章:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值