关于#python#的问题:Pygame控制飞机运动到几个固定的目标所在位置处的实现不了

Pygame控制飞机运动到几个固定的目标所在位置处的实现不了

        for i in range(m):
            speed_A = 3
            to = target[i]  #下个目标
            to_x = to[0]  #下个目标的横坐标
            to_y = to[1]  #下个目标的纵坐标
            if i == 0:
                r = math.sqrt((to_x) ** 2 + (to_y) ** 2)
                sin = to_y / r
                cos = to_x / r
            else:
                now = target[i-1]  #现在所在的位置
                now_x = now[0]
                now_y = now[1]
                r = math.sqrt((now_x-to_x)**2+(now_y-to_y)**2)  #与下个目标的间距
                sin = (to_y - now_y) / r
                cos = (to_x - now_x) / r
                if to_x - now_x < 0:
                    cos = -cos
            vel_x = speed_A * cos
            vel_y = speed_A * sin
            while discovery[i] == False:
                plane_x += vel_x
                plane_y += vel_y
                print(plane_x,plane_y)
                if planet_x == to_x and plane_y == to_y:
                    discover[i] == True
                    speed_A = 0

参考GPT和自己的思路:

根据您提供的代码,我发现有一些变量名拼写错误,如speed_A应该为speed_x,plane_x应该为plane_x等。另外,在探索目标时,您没有检查飞机是否到达目标位置,应该将判断语句if planet_x == to_x and plane_y == to_y:改为if plane_x == to_x and plane_y == to_y:。另外,您在判断sin和cos时,应该先做除数不为0的判断。如果您还有其他的问题,欢迎随时咨询。

参考GPT和自己的思路:

根据你提供的代码,我发现第25行中有一个语法错误,应该是plane\_x而不是planet\_x。另外,第21-27行是一个循环体,但是循环条件discovery[i] == False在何处被修改了呢?可能是修改条件的代码没有放在这个循环体中。 这个问题的解决方案因具体情况而异,可能需要对其他部分的代码进行修改才能实现飞机到达目标位置的功能。可以通过进一步调试代码和查看相关文档来解决问题。

该回答引用GPTᴼᴾᴱᴺᴬᴵ
在您的代码中,目标点到达后会将discovery[i]设为True,但是在代码中使用时写成了discover[i]。这可能会导致程序无法正常工作。
·
此外,在while循环中,您需要使用pygame的事件处理机制,否则该循环将阻止pygame的其他部分(例如屏幕绘制)正常工作。在while循环中应该使用pygame的事件处理函数(例如pygame.event.get())。
·
另外,您的程序可能需要使用pygame的定时器来更新飞机的位置,以便在飞机到达目标点时可以将discovery[i]设为True。

下面是可能更好的代码示例:

import pygame
import math

pygame.init()
screen = pygame.display.set_mode((800, 600))

target = [(100, 100), (400, 200), (200, 500)]
discovery = [False] * len(target)
plane_x, plane_y = 0, 0

def move_plane_to_target():
    global plane_x, plane_y
    for i, to in enumerate(target):
        if discovery[i]:
            continue
        to_x, to_y = to
        r = math.sqrt((to_x - plane_x) ** 2 + (to_y - plane_y) ** 2)
        sin = (to_y - plane_y) / r
        cos = (to_x - plane_x) / r
        vel_x = 3 * cos
        vel_y = 3 * sin
        plane_x += vel_x
        plane_y += vel_y
        if abs(plane_x - to_x) < 5 and abs(plane_y - to_y) < 5:
            discovery[i] = True

def draw_screen():
    screen.fill((255, 255, 255))
    for to in target:
        pygame.draw.circle(screen, (255, 0, 0), to, 10)
    pygame.draw.circle(screen, (0, 0, 255), (int(plane_x), int(plane_y)), 10)
    pygame.display.update()

clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    move_plane_to_target()
    draw_screen()
    if all(discovery):
        break
    clock.tick(60)


此代码将绘制目标点和一个蓝色的圆(表示飞机)。飞机将向每个目标点移动,并在到达每个目标点后将该目标点的discovery设为True。当所有目标点都被发现时,程序将退出。在此代码中,使用了pygame的事件处理机制和定时器,以便程序可以正常工作。