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

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

输出结果

https://img-blog.csdn.net/20180501120414824


实现代码

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


相关文章
|
4月前
|
Python
python小项目之利用pygame实现代码雨动画效果(附源码 可供学习)
python小项目之利用pygame实现代码雨动画效果(附源码 可供学习)
67 1
|
Python
【pygame】Python小游戏开发之看代码学编程
【pygame】Python小游戏开发之看代码学编程
217 0
【pygame】Python小游戏开发之看代码学编程
|
IDE 开发工具 Python
最详细python安装库的方法(以安装pygame库为例)
最详细python安装库的方法(以安装pygame库为例)
156 0
|
Python
Python 技术篇-使用pygame库播放音乐没有声音问题解决办法
Python 技术篇-使用pygame库播放音乐没有声音问题解决办法
496 0
Python 技术篇-使用pygame库播放音乐没有声音问题解决办法
|
Python
Python 技术篇-使用pygame库展示界面添加图片不显示问题解决办法
Python 技术篇-使用pygame库展示界面添加图片不显示问题解决办法
442 0
Python 技术篇-使用pygame库展示界面添加图片不显示问题解决办法
|
Python
Python 技术篇-使用pygame库实现音乐播放实例演示,带漂亮小界面!
Python 技术篇-使用pygame库实现音乐播放实例演示,带漂亮小界面!
305 0
Python 技术篇-使用pygame库实现音乐播放实例演示,带漂亮小界面!
|
关系型数据库 Python
Py之pygame:有趣好玩——利用pygame库实现一个移动底座弹球的小游戏
Py之pygame:有趣好玩——利用pygame库实现一个移动底座弹球的小游戏
Py之pygame:有趣好玩——利用pygame库实现一个移动底座弹球的小游戏
|
关系型数据库 C++ Python
Py之pygame:Python的pygame库的简介、安装、使用方法详细攻略
Py之pygame:Python的pygame库的简介、安装、使用方法详细攻略
Py之pygame:Python的pygame库的简介、安装、使用方法详细攻略
|
3月前
|
Python
Python使用pygame播放MP3
Python使用pygame播放MP3
20 0