Pygame甚至没有注册鼠标按钮

2024-06-17 11:30:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用pygame做一个RTS风格的游戏。我想一次移动一组选定的单位。我已经实现了这个计算时间,以及x和y的速度。你知道吗

def update(dest_x,dest_y):
        dist_x = dest_x - marine.rect.centerx 
        dist_y = dest_y - marine.rect.centery

        dist = math.hypot(dist_x,dist_y)
        time = dist/25
        if time == 0:
            speed_x = 0
            speed_y = 0
        else:
            speed_x = dist_x/time
            speed_y = dist_y/time

我最初在里面有一个循环,它也做了这样的运动:

def update(dest_x,dest_y):
        dist_x = dest_x - marine.rect.centerx 
        dist_y = dest_y - marine.rect.centery

        dist = math.hypot(dist_x,dist_y)
        time = dist/25
        if time == 0:
            speed_x = 0
            speed_y = 0
        else:
            speed_x = dist_x/time
            speed_y = dist_y/time

        for i in range (int(time)):
            marine.rect.centerx = marine.rect.centerx + speed_x
            marine.rect.centery = marine.rect.centery + speed_y

但问题是,它只是建立了一个动作,然后做了所有的一次。添加:

marine_list.draw(screen)

并不是每一步都能吸引他们。添加等待函数:

pygame.time.delay()

也不能解决问题,因为它在每个单元之间等待,而不是每次移动。无论如何,它再一次把他们所有人都感动了。我怀疑在完成整个模块之前它不会做任何类似于绘图的事情,所以我的想法是绘图部分需要在主游戏循环中,更新模块返回time、speed\ux和speed\y

我想我的问题是,如何获取返回的值并构造循环,以便它每次通过循环时只移动它们一次,但在经过一定数量的循环后就会停止?我刚开始使用python,当返回多个值时,我不知道return是如何工作的。你知道吗

编辑:这就是我调用函数的方式,我检查鼠标点击,然后检查它是否是鼠标右键。你知道吗

if event.type == pygame.MOUSEBUTTONDOWN:
            if pygame.mouse.get_pressed()[2] == (1):
                for marine in marine_list:
                   Marine.update(mouse_pos[0],mouse_pos[1])

Tags: rect游戏iftimedistdefupdatemath