Search code examples
pythonpython-3.xpygamemit-scratch

Pygame make sprite walk in given rotation


I did a little Scratch script a long time ago, which I would like to convert to Python with Pygame.

There are plenty of examples showing the rotation of the image but I would like to know how to change the sprite's rotation to make it move in a given direction, without changing the image.

Here is my Scratch code:

Scratch code (set rotation style : don't rotate, point in direction 120, move 10 steps.)

Here is my Pygame sprite class:

class Star(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = img_star
        self.rect = self.image.get_rect()
        self.velocity = [0, 0]
        self.rect.x = random.randint(0, window_x)
        self.rect.y = random.randint(0, window_y)

Solution

  • Detect when the mouse is clicked with the MOUSEBUTTONDOWN event:

    if event.type == pygame.MOUSEBUTTONDOWN:
        mouse_x, mouse_y = event.pos
    

    Compute the vector from the center of the sprite to the mouse click position:

    dx = mouse_x - star.rect.centerx
    dy = mouse_y - star.rect.centery
    

    Compute the length of the vector (Euclidean distance):

    dist = math.sqrt(dx*dx + dy*dy)
    

    or

    dist = math.hypot(dx, dy)
    

    Normalize the vector (Unit vector). A normalized vector has a length of 1:

    if dist > 0:
        dx /= dist
        dy /= dist
    

    Move the object a certain number of steps in the direction of the vector:

    star.rect.x += steps * dx
    star.rect.y += steps * dy
    

    See also Follow target or mouse


    Minimal example:

    import pygame, random, math
    
    class Star(pygame.sprite.Sprite):
        def __init__(self):
            super().__init__()
            self.image = img_star
            self.rect = self.image.get_rect()
            self.velocity = [0, 0]
            self.rect.x = random.randint(0, window_x)
            self.rect.y = random.randint(0, window_y)
    
    pygame.init()
    window_x, window_y = 500, 500
    window = pygame.display.set_mode((window_x, window_y))
    clock = pygame.time.Clock()
    
    img_star = pygame.Surface((20, 20), pygame.SRCALPHA)
    pygame.draw.circle(img_star, (255, 255, 0), (10, 10), 10)
    star = Star()
    group = pygame.sprite.Group(star)
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False          
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = event.pos
                dx = mouse_x - star.rect.centerx
                dy = mouse_y - star.rect.centery
                dist = math.sqrt(dx*dx + dy*dy)
                steps = 10
                if dist > 0:
                    star.rect.x += steps * dx / dist
                    star.rect.y += steps * dy / dist
    
        window.fill(0)
        group.draw(window)
        pygame.display.flip()
    
    pygame.quit()
    exit()