基于Pygame模拟导弹追踪

import pygame
import sys
import math

def main():
    pygame.init()
    screen = pygame.display.set_mode((800, 700), 0, 32)
    missile_image = pygame.image.load('D:/daku/导弹追踪/red_pointer.png').convert_alpha()

    # 调整导弹图像的大小
    missile_size = (50, 50)  # 初始大小
    missile_image = pygame.transform.scale(missile_image, missile_size)

    x1, y1 = 100, 600  # 导弹的初始位置
    velocity = 400  # 导弹速度
    clock = pygame.time.Clock()
    angle = 0
    trail = []  # 导弹轨迹列表
    trail_max_length = 100  # 轨迹的最大长度

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        # 获取两帧之间的时间差
        dt = clock.tick(300) / 1000.0

        x, y = pygame.mouse.get_pos()
        distance = math.sqrt((x1 - x) ** 2 + (y1 - y) ** 2)

        # 计算新的位置
        section = velocity * dt
        sina = (y1 - y) / distance
        cosa = (x - x1) / distance
        angle = math.atan2(y - y1, x - x1)

        # 更新导弹的位置
        x1 += section * cosa
        y1 -= section * sina

        # 保存导弹位置到轨迹列表
        trail.append((x1, y1))

        # 如果轨迹列表超过了最大长度,则删除最老的位置
        if len(trail) > trail_max_length:
            trail.pop(0)

        # 旋转导弹图像
        rotated_missile = pygame.transform.rotate(missile_image, math.degrees(angle))

        # 调整导弹图像大小,使其随着距离目标越近而变小
        size_reduction_factor = max(1, min(2, distance / 200))  # 限制大小变化在 1 到 2 之间
        adjusted_size = (int(missile_size[0] / size_reduction_factor), int(missile_size[1] / size_reduction_factor))
        adjusted_rotated_missile = pygame.transform.scale(rotated_missile, adjusted_size)

        # 获取旋转后导弹的新矩形
        rotated_rect = adjusted_rotated_missile.get_rect(center=missile_image.get_rect(center=(x1, y1)).center)

        # 清除屏幕并绘制导弹
        screen.fill((0, 0, 0))  # 清除屏幕
        screen.blit(adjusted_rotated_missile, rotated_rect.topleft)

        # 绘制导弹轨迹
        for pos in trail:
            pygame.draw.circle(screen, (173, 216, 230), pos, 2)

        pygame.display.update()

if __name__ == "__main__":
    main()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pygame是一个流行的Python库,用于创建2D游戏、图形界面和其他多媒体应用。要使用Pygame模拟一个小球的运动,你需要了解以下基本步骤: 1. **安装Pygame**: 首先确保已经安装了pygame,如果没有,可以通过pip进行安装: ``` pip install pygame ``` 2. **导入模块**: 导入pygame库中的必要模块,如`pygame.display`, `pygame.time`, 和 `pygame.event`。 3. **初始化Pygame**: ```python import pygame pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("小球运动") clock = pygame.time.Clock() ``` 4. **创建小球类**: 定义一个小球类,包含位置、速度等属性,并定义更新位置的方法(如`move()`)。 5. **小球的位置和移动**: 使用小球类,比如用`x`和`y`表示位置,每次循环改变小球的速度方向或大小,让小球沿指定路径移动。 6. **主游戏循环**: ``` running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 更新小球位置 ball.move() # 绘制小球 screen.fill((255, 255, 255)) # 清屏 pygame.draw.circle(screen, (0, 0, 255), ball.position, ball.radius) pygame.display.flip() # 刷新屏幕 clock.tick(60) # 控制帧率 pygame.quit() ``` 7. **结束程序**: 当用户关闭窗口时,退出游戏循环并关闭Pygame。 这是一个基础的框架,你可以根据自己的需求添加更多的细节,例如碰撞检测、得分系统、键盘控制等。如果你有具体的问题,比如不明白某个概念,或者需要帮助编写特定的部分,请告诉我,我会提供更详细的指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值