会玩,有人用 Python 模拟导弹防御!
![](https://filescdn.proginn.com/fbbd824c2182f3b1468011e8f13d04ff/fd0bc17d2bbd3adb15da2aa330edb5ce.webp)
![](https://filescdn.proginn.com/c5ea5e2787f5c8a0e87fae0b458ebe88/1f9b396b443a0dc40b90cb605904f3b6.webp)
![](https://filescdn.proginn.com/18a84625b3e60fa73c46b698565ed822/95a71535039d739c35b6fc32476d7319.webp)
![](https://filescdn.proginn.com/fb0898ef33a9da4c311d4df11a14e223/bb71f80c9753406642d6f7dc6b50200c.webp)
![](https://filescdn.proginn.com/c96b5dcd82ab58e84a0fb6f5ab0c20c8/0cc698720e08f30cd3b18d655ee09e26.webp)
![](https://filescdn.proginn.com/c620baf316c5a984e5f1a3b05d6a5090/1f291709e465930d13d296eb43ec30d6.webp)
import pygame,sys
from math import *
pygame.init()
screen=pygame.display.set_mode((800,700),0,32)
missile=pygame.image.load('element/red_pointer.png').convert_alpha()
x1,y1=100,600 #导弹的初始发射位置
velocity=800 #导弹速度
time=1/1000 #每个时间片的长度
clock=pygame.time.Clock()
old_angle=0
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) #两点线段的弧度值
x1,y1=(x1+section*cosa,y1-section*sina)
d_angle = degrees(angle) #弧度转角度
screen.blit(missile, (x1-missile.get_width(), y1-missile.get_height()/2))
dis_angle=d_angle-old_angle #dis_angle就是到下一个位置需要改变的角度
old_angle=d_angle #更新初始角度
pygame.display.update()
![](https://filescdn.proginn.com/bb909d15970ce73d52365a1036b0445b/63e066c826ec435c56d0730e08fd9d9d.webp)
missiled = pygame.transform.rotate(missile, -(d_angle))
screen.blit(missiled, (x1-missile.get_width(), y1-missile.get_height()/2))
![](https://filescdn.proginn.com/2cefdf763131405a52ac210b6342831d/e07aee43f19a82a64c6e6399e359ee2e.webp)
![](https://filescdn.proginn.com/a8fbae9f023c8986227cfcfacf5a4bcb/217a8458b71c321b41dc51e19348469b.webp)
![](https://filescdn.proginn.com/ed9acbf03ff33c09ed32e871a0044d22/8a380f0130b1b2e49ad3afe69e463619.webp)
![](https://filescdn.proginn.com/9de2752171c72eaefdf095e8f75a67dc/24e8ea8c475692c445b50a6c10a31bee.webp)
![](https://filescdn.proginn.com/45c0650e0ebafd7747add1d03fc13569/c65db3b12ca5af01828c86b7bdd1b17b.webp)
![](https://filescdn.proginn.com/d3840e2c2215e0170c8429f802e9dc41/b9792906a6860c4156f9f37a6e1d3ab3.webp)
![](https://filescdn.proginn.com/10b91bdb16e81eb261807e799b5c0be3/e1f3f4ba5c1924a57c23bcb4fb49ec30.webp)
screen.blit(missiled, (x1-width+(x1-C[0]),y1-height/2+(y1-C[1])))
![](https://filescdn.proginn.com/28dff935e61b75f392113f9a98a1c8ce/22a3af41694f32054149b8ff685a7ad2.webp)
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('element/rect1.png').convert_alpha()
height=missile.get_height()
width=missile.get_width()
pygame.mouse.set_visible(0)
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)
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+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))
(x1-width+(x1-C[0]),y1-height/2+(y1-C[1])))
(x,y)) #鼠标用一个红色*代替
pygame.display.update()
以上便是用Python模拟导弹自动追踪的代码实例。
—————END—————
推荐阅读:
最近面试BAT,整理一份Python资料《Python学习手册》,覆盖了Python知识点、人工智能、深度学习、机器学习等方面。
获取方式:关注公众号并回复 Python 领取,更多内容陆续奉上。
觉得不错,点个“在看”然后转发出去
评论