Rotation based on end points

前端 未结 4 1124
别那么骄傲
别那么骄傲 2021-01-21 12:03

I\'m using pygame to draw a line between two arbitrary points. I also want to append arrows at the end of the lines that face outward in the directions the line is traveling.

4条回答
  •  爱一瞬间的悲伤
    2021-01-21 12:33

    Here is the complete code to do it. Note that when using pygame, the y co-ordinate is measured from the top, and so we take the negative when using math functions.

    import pygame
    import math
    import random
    pygame.init()
    
    screen=pygame.display.set_mode((300,300))
    screen.fill((255,255,255))
    
    pos1=random.randrange(300), random.randrange(300)
    pos2=random.randrange(300), random.randrange(300)
    
    pygame.draw.line(screen, (0,0,0), pos1, pos2)
    
    arrow=pygame.Surface((50,50))
    arrow.fill((255,255,255))
    pygame.draw.line(arrow, (0,0,0), (0,0), (25,25))
    pygame.draw.line(arrow, (0,0,0), (0,50), (25,25))
    arrow.set_colorkey((255,255,255))
    
    angle=math.atan2(-(pos1[1]-pos2[1]), pos1[0]-pos2[0])
    ##Note that in pygame y=0 represents the top of the screen
    ##So it is necessary to invert the y coordinate when using math
    angle=math.degrees(angle)
    
    def drawAng(angle, pos):
        nar=pygame.transform.rotate(arrow,angle)
        nrect=nar.get_rect(center=pos)
        screen.blit(nar, nrect)
    
    drawAng(angle, pos1)
    angle+=180
    drawAng(angle, pos2)
    pygame.display.flip()
    

提交回复
热议问题