pygame教你从0到1一步步实现点到点的智能追踪系统(其二)
Posted dhjabc_1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pygame教你从0到1一步步实现点到点的智能追踪系统(其二)相关的知识,希望对你有一定的参考价值。
续上篇,上篇实现了点到点的智能追踪系统,这篇将实现图片到目标点的智能追踪。
还是参照上篇的代码,将追踪点修改为图片,实现一个版本。
一、失败的图片到目标点版本
(一)核心代码
增加plane的对象:
plane = pygame.image.load('plane.png').convert_alpha()
在循环过程中blit图形到界面上显示:
screen.blit(plane, (int(x1), int(y1)))
(二)完整代码
import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700))
plane = pygame.image.load('plane.png').convert_alpha()
x1,y1=100,600 #导弹的初始发射位置
x,y =500,200 #目标位置
velocity=0.8 #导弹速度
clock=pygame.time.Clock()
while True:
x, y = pygame.mouse.get_pos() # 获取鼠标位置,鼠标就是需要打击的目标
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(300)
distance=sqrt(pow(x1-x,2)+pow(y1-y,2)) #两点距离公式
section=velocity #每个时间片需要移动的距离
sina=(y1-y)/distance
cosa=(x-x1)/distance
angle=atan2(y-y1,x-x1) #两点线段的弧度值
x1,y1=(x1+section*cosa,y1-section*sina)
screen.fill((0,0,0))
screen.blit(plane, (int(x1), int(y1)))
# pygame.draw.circle(screen, (255, 0, 0), (int(x1), int(y1)), 10)
pygame.draw.circle(screen, (0, 0, 255), (x,y), 10)
pygame.display.update()
(三)效果显示
可以发现,有两个问题。
二、失败原因分析
1、图片在运行过程中,不会旋转角度,让机头跟随鼠标运动。
2、图片框的中心点位置不对,需要做矫正。
三、修订版本
(一)让图片跟随鼠标运动旋转
1、核心代码
angle=atan2(y-y1,x-x1) #两点线段的弧度值
angle = atan2(y - y1, x - x1) # 两点间线段的弧度值
fangle = degrees(angle) # 弧度转角度
x1,y1=(x1+section*cosa,y1-section*sina)
planed = pygame.transform.rotate(plane, -(fangle))
screen.blit(planed, (int(x1), int(y1)))
2、完整代码
import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700))
plane = pygame.image.load('plane.png').convert_alpha()
x1,y1=100,600 #导弹的初始发射位置
x,y =500,200 #目标位置
velocity=0.8 #导弹速度
clock=pygame.time.Clock()
while True:
x, y = pygame.mouse.get_pos() # 获取鼠标位置,鼠标就是需要打击的目标
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(300)
distance=sqrt(pow(x1-x,2)+pow(y1-y,2)) #两点距离公式
section=velocity #每个时间片需要移动的距离
sina=(y1-y)/distance
cosa=(x-x1)/distance
angle=atan2(y-y1,x-x1) #两点线段的弧度值
angle = atan2(y - y1, x - x1) # 两点间线段的弧度值
fangle = degrees(angle) # 弧度转角度
x1,y1=(x1+section*cosa,y1-section*sina)
planed = pygame.transform.rotate(plane, -(fangle))
screen.fill((0,0,0))
screen.blit(planed, (int(x1), int(y1)))
# pygame.draw.circle(screen, (255, 0, 0), (int(x1), int(y1)), 10)
pygame.draw.circle(screen, (0, 0, 255), (x,y), 10)
pygame.display.update()
3、运行效果
(二)矫正图形的位置
1、核心代码
height = plane.get_height()
width = plane.get_width()
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 + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
if -180 < -fangle < -90:
A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
B = (x1 - width, A[1] + height * cosa)
C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))
2、完整代码
import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700))
plane = pygame.image.load('plane.png').convert_alpha()
x1,y1=100,600 #导弹的初始发射位置
x,y =500,200 #目标位置
velocity=0.8 #导弹速度
clock=pygame.time.Clock()
height = plane.get_height()
width = plane.get_width()
while True:
x, y = pygame.mouse.get_pos() # 获取鼠标位置,鼠标就是需要打击的目标
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(300)
distance=sqrt(pow(x1-x,2)+pow(y1-y,2)) #两点距离公式
section=velocity #每个时间片需要移动的距离
sina=(y1-y)/distance
cosa=(x-x1)/distance
angle=atan2(y-y1,x-x1) #两点线段的弧度值
angle = atan2(y - y1, x - x1) # 两点间线段的弧度值
fangle = degrees(angle) # 弧度转角度
x1,y1=(x1+section*cosa,y1-section*sina)
planed = pygame.transform.rotate(plane, -(fangle))
screen.fill((0,0,0))
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 + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
if -180 < -fangle < -90:
A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
B = (x1 - width, A[1] + height * cosa)
C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))
# screen.blit(planed, (int(x1), int(y1)))
# pygame.draw.circle(screen, (255, 0, 0), (int(x1), int(y1)), 10)
pygame.draw.circle(screen, (0, 0, 255), (x,y), 10)
pygame.display.update()
3、运行效果
大致的雏形已经出来了,后续是完善代码成函数和类,并增加更多趣味性的内容了。
四、核心代码块封装成函数
(一)核心代码块
def move(x,y,plane,x1,y1,velocity):
height = plane.get_height()
width = plane.get_width()
distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2)) # 两点距离公式
section = velocity # 每个时间片需要移动的距离
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)
planed = pygame.transform.rotate(plane, -(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 + planed.get_width(), y1 - height / 2 + planed.get_height() - height * cosa)
B = (A[0] + height * sina, y1 - height / 2 + planed.get_height())
if -180 < -fangle < -90:
A = (x1 - width - height * sina, y1 - height / 2 + planed.get_height())
B = (x1 - width, A[1] + height * cosa)
C = ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2)
screen.blit(planed, (x1 - width + (x1 - C[0]), y1 - height / 2 + (y1 - C[1])))
(二)增加碰撞检测
碰撞了返回True,否则返回False
# 碰撞检测
if abs(x-x1)<5 and abs(y - y1)<5:
print(x - x1, y - y1)
return True,x1,y1
else:
return False,x1,y1
(三)完整代码
import pygame,sys
from math import *
def move(x,y,plane,x1,y1,velocity):
height = plane.get_height()
width = plane.get_width()
distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2)) # 两点距离公式
section = velocity # 每个时间片需要移动的距离
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)
planed = pygame.transform.rotate(plane, -(fangle))
if 0 <= -fangle <= 90:
A = (width * cosa + x1 - width, y1 - height / 2)
B = 以上是关于pygame教你从0到1一步步实现点到点的智能追踪系统(其二)的主要内容,如果未能解决你的问题,请参考以下文章
pygame教你从0到1一步步实现点到点的智能追踪系统(其一)
LFS7.7一步步教你从 〇 开始撸个 Linux 系统 | 文末送书
干货! 一步步教你从Oracle 11gR2 RAC 升级至18c