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

如何使箭头图像移动旋转方向?

如何解决如何使箭头图像移动旋转方向?

我有一个向左和向右旋转的箭头图像 VIDEO,当我点击我的箭头时,您可以在视频中看到 停止旋转并且我想要它移动它停止旋转的方向,我该怎么做? 现在我只有它,所以当我点击旋转停止时,但不确定如何让它移动到它停止旋转的方向

enter image description here

我试图做到这一点,当玩家点击箭头开始移动但它不会像旋转的地方那样移动它只会向下移动我的箭头VIDEO

rotating_arrow = True

move = False
# our main loop
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            rotating_arrow = False
            move = True


    if move:
        move_angle = bow2.angle + 15 # The arrow is painted approximately 15° downwards
        speed = 20
        move_vec = pygame.math.Vector2(speed,0).rotate(move_angle)
        bow2.x += move_vec.x
        bow2.y += move_vec.y
        bow2.rect.topleft = round(bow2.x),round(bow2.y)

这是我旋转图像的设置方式



def blitRotate(surf,image,pos,originPos,angle):

    # calcaulate the axis aligned bounding Box of the rotated image
    w,h         = image.get_size()
    sin_a,cos_a = math.sin(math.radians(angle)),math.cos(math.radians(angle)) 
    min_x,min_y = min([0,sin_a*h,cos_a*w,sin_a*h + cos_a*w]),max([0,sin_a*w,-cos_a*h,sin_a*w - cos_a*h])

    # calculate the translation of the pivot 
    pivot        = pygame.math.Vector2(originPos[0],-originPos[1])
    pivot_rotate = pivot.rotate(angle)
    pivot_move   = pivot_rotate - pivot

    # calculate the upper left origin of the rotated image
    origin = (pos[0] - originPos[0] + min_x - pivot_move[0],pos[1] - originPos[1] - min_y + pivot_move[1])

    # get a rotated image
    rotated_image = pygame.transform.rotate(image,angle)

    # rotate and blit the image
    surf.blit(rotated_image,origin)


我的箭类

class arrow:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.shootsright = pygame.image.load("ner.png")

        self.image = self.shootsright
        self.rect  = self.image.get_rect(center = (self.x,self.y))
        self.look_at_pos = (self.x,self.y)

        self.isLookingAtPlayer = False
        self.look_at_pos = (x,y)
        self.angle = 0
        self.gun_size = self.image.get_size()
        self.pivot = (8,self.gun_size[1]//2)
        
        
        self.rect = pygame.Rect(x,width)
    def draw(self):


        blitRotate(window,self.image,self.rect.center,self.pivot,self.angle)
        pygame.draw.rect(window,self.color,self.rect)



解决方法

如果你想朝某个方向发射子弹,方向是在子弹发射的那一刻定义的,但它不会连续改变。这意味着箭射出后,角度不会改变。

通过三角函数sincos计算箭头的方向。定义箭头的速度。更改每一帧中箭头的位置属性(xy):

speed = 20
self.x += speed * math.cos(math.radians(move_angle))
self.y -= speed * math.sin(math.radians(move_angle))

更新 rect 属性:

self.rect.topleft = round(self.x),round(self.y)

由于箭头图片是按一定角度绘制的,可能需要修正运动角度:

move_angle = self.angle - 15 # The arrow is painted approximately 15° downwards

完整的arrow.draw方法:

class arrow:
   # [...]

   def draw(self):
        move_angle = self.angle - 15 # The arrow is painted approximately 15° downwards
        speed = 20
        self.x += speed * math.cos(math.radians(move_angle))
        self.y -= speed * math.sin(math.radians(move_angle))
        self.rect.topleft = round(self.x),round(self.y)

        blitRotate(window,self.image,self.rect.center,self.pivot,self.angle)
        pygame.draw.rect(window,self.color,self.rect)

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