how to rotate image through mouse in pygame

# if you want to find the angle the mouse position is from the center
#heres the formula
angle = 360 - math.atan2(pos[1] - 300, pos[0] - 400) * 180 / math.pi

3.8
10
Krish 100200 points

                                    import pygame
import pygame.font

pygame.init()
size = (400,400)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

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

    # calcaulate the axis aligned bounding box of the rotated image
    w, h       = image.get_size()
    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
    box_rotate = [p.rotate(angle) for p in box]
    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])

    # 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_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])

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

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

    # draw rectangle around the image
    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)

font = pygame.font.SysFont('Times New Roman', 50)
text = font.render('image', False, (255, 255, 0))
image = pygame.Surface((text.get_width()+1, text.get_height()+1))
pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
image.blit(text, (1, 1))
w, h = image.get_size()

angle = 0
done = False
while not done:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_ESCAPE:
                done = True

    pos = (screen.get_width()//2, screen.get_height()//2)
    pos = (200, 200)

    screen.fill(0)
    blitRotate(screen, image, pos, (w//2, h//2), angle)
    angle += 1

    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)

    pygame.display.flip()

pygame.quit()

3.8 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
rotate in pygame pygame rotate image around pivot pygame rotate image to mouse how to rotate image to a angle in pygame how to rotate image pygame how permenatl rotate image pygame how to rotate a image about a point in pygame how can we rotate a image about a point in pygame pygame image rotate center rotate image around center pygame how to make the player rotate to mouse pygame pygame rotate image around point pygame how to rotate towards mouse pygame rotating rotate img pygame rotater img pygame rotate photo in pygame pygame set rotation of image set rotation of image pygame Pygame rotate to mouse how to rotate an image towards mouse in pygame how to rotate a image to mouse in pygame how to rotate to your mouse pygame how to rotate a box to mouse in pygame rotate a box to mouse in pygame how to rotate something twards mouse in pygame rotate image from center pygame how to rotate image through mouse in pygame python pygame rotate function pygame crop image how to rotate an image pygame pygame rotate image around center how to rotate image in pygame how to rotate an image in pygame rotation image pygame how to rotate a picture in pygame rotate pygame how to resize an image pygame pygame.transform .rotate rotate a surface pygmae pygame.transform.flip rotating image pygame pygame transform rotate object how to rotate an image using rect and vector in pygame pygame rotate transform pygame.rotate memory rotate image crashing pygame pygame.transform.rotozoom how to make pygame.tranform.scale better transform an image on a surface scale pygame image pygasme.scale how to rotate a sprite pygame pygame rotate image\ blit rotate pygame smooth transformation pygame rezise object pygame pygame.transform.flip vs rotate pygame image scale pygame rotate a drawn rect resizing images pygame pygame rotate sprite rotate rect pygame how to rotate a image in pygame transform image pygame ptgame rotate image pygame change surface size resize surface pygame pytgame.transform.rotate() pygame rotate surface pygame zoom rotate an image pygame pygame surface get rotation pygame.transform pygame rotate object pygame.image.rotate pygame image rotation pygame rotating image pygame.trasnform.scale resize pygame image transform pygame rotate images in pygame pygame scale surface how to resize an image in pygame pygame.transform.scale 2x pygame image rotate how to make a cimage grow then shrink in pygame pygame image resize pygame transform rotate scale transformation image in pygame scale tranformation image in pygame tranformation image in pygame how to transform scale in pygame pygame scale how to resize a image pygame resize image pygame scaling pygame image pygame size rotate image pygaem how to scale and transform image pygame rotate image pygame pygame.transform.laplacian() transform scale pygame pygame change angle of image pygame.transform.rotate pygame set image rotation set rotation of image in pygame rotate image in pygame pygame rotation how to rotate img in pygame horizontaly how to rotate img in pygame pygame resize image how to resize images in pygame how to resize imgs in pygame scale pygame.Surface scale surface pygame pygame change angle images pygame rotate images pygame transform scale image pygame pygame scaling image pygame.transform.rotate() how to scale and rotate image in pygame pygame transform scale pygame blit rotation pygame image transform pygame transform scale example pygame.transform.scale pygame rotate how to rotate images in pygame transform.rotate pygame pygame.transform.recale pygame transform.flip pygame flip image pygame scale image image pygame transform position pygame rotate image
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source