微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

放大精灵后弹丸轨迹混乱

如何解决放大精灵后弹丸轨迹混乱

我的屏幕是 1920 x 1080,我在其上绘制所有内容显示器是 960 x 540。我有一个弹丸,它与鼠标的位置进行方程以确定其轨迹,但是因为我已经放大了鼠标的所有内容pos 仍然像它的 1920 x 1080 而不是 960 x 540。

射弹轨迹的类方法我有相机移动,这就是为什么 player.rect.x 被滚动减去)

    def shoot(self,player,mouse_x,mouse_y,speed,scroll):
        self.dx,self.dy = mouse_x - (player.rect.x - scroll[0]),mouse_y - (player.rect.y - scroll[1])
        distance = math.hypot(self.dx,self.dy)
        self.dx,self.dy = self.dx / distance,self.dy / distance

放大代码

surf = pg.transform.scale(display,WINDOW_SIZE)
    screen.blit(surf,(0,0))
    pg.display.update()  # updates the screen every frame
    clock.tick(FPS)  # caps framerate at 60 frames per second

屏幕显示分辨率

# (0,0) is in the top left corner of the window
WIDTH = 1920  # 1920 / 15 = 128 (Math for tile map)
HEIGHT = 1080  # 1080 / 15 = 72 (Math for tile map)

TILE_SIZE = 15

WINDOW_SIZE = (WIDTH,HEIGHT)

FPS = 60

screen = pg.display.set_mode(WINDOW_SIZE)  # Initiate screen
display = pg.Surface((960,540))

黄色立方体是弹丸,这就是放大镜头时的样子

bullet being shot in 960 x 540

这是在 1920 x 1080 下拍摄时的样子

bullet being shot in 1920 x 1080

在两张图片中,我都点击了右键。

请有人帮我找到子弹轨迹/鼠标位置的修复

解决方法

只需缩放鼠标位置:

def shoot(self,player,mouse_x,mouse_y,speed,scroll):
   
    mx = mouse_x * 960 / 1920
    my = mouse_y * 540 / 1080

    self.dx,self.dy = mx - (player.rect.x - scroll[0]),my - (player.rect.y - scroll[1])
    distance = math.hypot(self.dx,self.dy)
    self.dx,self.dy = self.dx / distance,self.dy / distance

或者,您可以在使用 pygame.mouse.get_pos 获取鼠标位置后缩放鼠标位置:

mouse_pos = pygame.mouse.get_pos()
mouse_pos = (mouse_pos[0] * 960 / 1920,mouse[1] * 540 / 1080)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。