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)/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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?