How to make a crosshair that constantly stays in front of the player

I’m a beginner Pygame/Python programmer wanting to know how I can make a crosshair that follows the direction that the sprite is facing, so when the player rotates the crosshair rotates with it staying constantly in front of the player. It’s is for a small TDS game I am making. I have found NO answers on the web for this question so I was hoping I could get answers here.

My code is listed below if it will be helpful.

here is a link to the game.

import pygame
from sys import exit
from pygame.constants import KEYUP, K_LSHIFT, K_SPACE, MOUSEMOTION, K_q
from random import randint
import math
pygame.init()



class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.x = 600
        self.y = 300
        self.image = pygame.image.load('graphics/Robot 1/robot1_gun.png').convert_alpha()
        self.rect = self.image.get_rect()
        self.orig_image = pygame.image.load('graphics/Robot 1/robot1_gun.png').convert_alpha()
        self.rotate_vel = 1
        

    def movement(self):
        key = pygame.key.get_pressed()
        self.rect = self.image.get_rect(center = (self.x, self.y))
        dist = 3 # distance moved in 1 frame, try changing it to 5
        if key[pygame.K_DOWN] or key[pygame.K_s]: # down key
            self.y += dist # move down
        elif key[pygame.K_UP] or key[pygame.K_w]: # up key
            self.y -= dist # move up
        if key[pygame.K_RIGHT] or key[pygame.K_d]: # right key
            self.x += dist # move right
        elif key[pygame.K_LEFT] or key[pygame.K_a]: # left key
            self.x -= dist # move left
  
    
    def rotate(self, surface):
        keys = pygame.key.get_pressed()
        if keys[K_LSHIFT]:
            self.rotate_vel += 5
            self.image = pygame.transform.rotate(self.orig_image, self.rotate_vel)
            self.rect = self.image.get_rect(center=self.rect.center)
            surface.blit(self.image, self.rect)
        if keys[K_SPACE]:
            self.rotate_vel += -5
            self.image = pygame.transform.rotate(self.orig_image, self.rotate_vel)
            self.rect = self.image.get_rect(center=self.rect.center)
            surface.blit(self.image, self.rect)




    def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, self.rect)
            
            
        



 

        

    def update(self):
        self.movement()
        self.draw(screen)
        self.rotate(screen)
    


                
            


        


#screen
clock = pygame.time.Clock()
FPS = 60
screen = pygame.display.set_mode((800, 400))

#player
player_sprite = Player()
player = pygame.sprite.GroupSingle()
player.add(player_sprite)



running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    
    

    

    #screen
    screen.fill('grey')
    #player sprite funtions
    
    player.update()

   


    

    clock.tick(FPS)

    pygame.display.update()

Answer

Use pygame.math.Vector2.from_polar to define a vector by Polar coordinates. Add the center of the player to the vector and draw a cross at the computed position.

e.g.:

class Player(pygame.sprite.Sprite):
    # [...]

    def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, self.rect)

        dir_vec = pygame.math.Vector2()
        dir_vec.from_polar((-100, -self.rotate_vel))
        cross_pos = dir_vec + self.rect.center
        x, y = round(cross_pos.x), round(cross_pos.y)
        pygame.draw.line(surface, (200, 0, 0), (x-10, y), (x+10, y))
        pygame.draw.line(surface, (200, 0, 0), (x, y-10), (x, y+10))