how to rotate image through mouse in pygame

Solutions on MaxInterview for how to rotate image through mouse in pygame by the best coders in the world

showing results for - "how to rotate image through mouse in pygame"
Myriam
18 Jan 2018
1# if you want to find the angle the mouse position is from the center
2#heres the formula
3angle = 360 - math.atan2(pos[1] - 300, pos[0] - 400) * 180 / math.pi
Greg
15 Mar 2020
1import pygame
2import pygame.font
3
4pygame.init()
5size = (400,400)
6screen = pygame.display.set_mode(size)
7clock = pygame.time.Clock()
8
9def blitRotate(surf, image, pos, originPos, angle):
10
11    # calcaulate the axis aligned bounding box of the rotated image
12    w, h       = image.get_size()
13    box        = [pygame.math.Vector2(p) for p in [(0, 0), (w, 0), (w, -h), (0, -h)]]
14    box_rotate = [p.rotate(angle) for p in box]
15    min_box    = (min(box_rotate, key=lambda p: p[0])[0], min(box_rotate, key=lambda p: p[1])[1])
16    max_box    = (max(box_rotate, key=lambda p: p[0])[0], max(box_rotate, key=lambda p: p[1])[1])
17
18    # calculate the translation of the pivot 
19    pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
20    pivot_rotate = pivot.rotate(angle)
21    pivot_move   = pivot_rotate - pivot
22
23    # calculate the upper left origin of the rotated image
24    origin = (pos[0] - originPos[0] + min_box[0] - pivot_move[0], pos[1] - originPos[1] - max_box[1] + pivot_move[1])
25
26    # get a rotated image
27    rotated_image = pygame.transform.rotate(image, angle)
28
29    # rotate and blit the image
30    surf.blit(rotated_image, origin)
31
32    # draw rectangle around the image
33    pygame.draw.rect (surf, (255, 0, 0), (*origin, *rotated_image.get_size()),2)
34
35font = pygame.font.SysFont('Times New Roman', 50)
36text = font.render('image', False, (255, 255, 0))
37image = pygame.Surface((text.get_width()+1, text.get_height()+1))
38pygame.draw.rect(image, (0, 0, 255), (1, 1, *text.get_size()))
39image.blit(text, (1, 1))
40w, h = image.get_size()
41
42angle = 0
43done = False
44while not done:
45    clock.tick(60)
46    for event in pygame.event.get():
47        if event.type == pygame.QUIT:
48            done = True
49        elif event.type == pygame.KEYDOWN:
50            if event.key==pygame.K_ESCAPE:
51                done = True
52
53    pos = (screen.get_width()//2, screen.get_height()//2)
54    pos = (200, 200)
55
56    screen.fill(0)
57    blitRotate(screen, image, pos, (w//2, h//2), angle)
58    angle += 1
59
60    pygame.draw.line(screen, (0, 255, 0), (pos[0]-20, pos[1]), (pos[0]+20, pos[1]), 3)
61    pygame.draw.line(screen, (0, 255, 0), (pos[0], pos[1]-20), (pos[0], pos[1]+20), 3)
62    pygame.draw.circle(screen, (0, 255, 0), pos, 7, 0)
63
64    pygame.display.flip()
65
66pygame.quit()
67
queries leading to this page
pygame image resizepygame set image rotationtransform rotate pygamepygame rotate image around pivotpygame blit rotationhow to rotate an image towards mouse in pygamepygame transform scale examplepygame transform scalepygame zoomset rotation of image pygamepygame image rotaterotate an image pygamerotate image pygaemhow to resize images in pygamepygame flip imagepygame scaling imagerotate image pygameblit rotate pygamehow to make an image rotate in pygamehow to rotate a image in pygamerotate a surface pygmaepygame scale imagepygame transform recalepygame rotate image pygame rotate to mouseresizing images pygamepygasme scalepygame rotate imagespygame rotate objecthow to scale and transform image pygamerotate in pygamepygame image rotatetransform pygamepygame rotate spriterotate recntanlge image pygamepygame sizerotate pygamepygame image rotate centerresize pygame imagehow to rotate images in pygame on an axishow to resize an image pygamehow to rotate image in pygamepygame transformhow to rotate something twards mouse in pygameptgame rotate imagehow to rotate a image in pygamewpytgame transform rotate 28 29pygame transform rotate objecthow to rotate a picture in pygamehow to make a cimage grow then shrink in pygamepygame function that rotate an imagehow to rotate img in pygamepygame image transformhow to make pygame tranform scale betterpygame transform rotatepygame image transform rotate 28 29pygame crop imagepygame image rotationset rotation of image in pygamepygame transform flip vs rotatepython pygame rotate functionrotate rect pygameimage pygame transform positionpygame rotate transformrezise object pygamerotating image pygamepygame transform rotatepygame transform fliphow to transform scale in pygamehow to resize an image in pygamepygame rotationrotate image from center pygamepygame rotate surfaceresize surface pygamehow can we rotate a image about a point in pygamepygame rotatepygame resize imagehow to rotate from centre of image pygamepygame trasnform scaletranformation image in pygamehow to resize a image pygamehow to rotate a image about a point in pygamehow to rotate an image pygamepygame surface get rotationrotate a box to mouse in pygamepygame change angle imagespygame transform rotozoomrotate photo in pygamerotate images in pygamerotater img pygamepygame rotate a drawn recttransform an image on a surfacepygame rotate image to mousehow to rotate an image in pygamehow to rotate img in pygame horizontalyscale surface pygamepygame transform scalepygame set rotation of imagehow to rotate image to a angle in pygamerotate image around center pygamepygame scale surfacehow to rotate a box to mouse in pygameresize image pygamerotate image python pygamepygame transformhow to scale and rotate image in pygamescale pygame imagehow to rotate a image to mouse in pygamepygame transform fliprotate image in pygametransform image pygamepygame rotating imagepygame scalerotate image crashing pygamehow to rotate an object so its pointing at the mouse in pygamehow to rotate a sprite pygametransform scale pygamepygame rotate memoryhow permenatl rotate image pygamepygame rotate image around pointpygame transform scale 2xrotate img pygamescale image pygamehow to rotate image pygamescaling pygame imagehow to rotate an image using rect and vector in pygamepygame image scalehow to rotate rect towards the mouse pygamepygame rotate image 5cscale pygame surfacepygame transform rotatehow to rotate to your mouse pygamehow to resize imgs in pygamepygame how to rotate towards mousepygame rotate image around centerrotation image pygamescale tranformation image in pygamepygame change angle of imagehow to rotate images in pygamepygame rotatingpygame transform rotate 28 29smooth transformation pygamehow to make the player rotate to mouse pygamehow to rotate image through mouse in pygamepygame change surface sizescale transformation image in pygamepygame transform laplacian 28 29how to rotate image through mouse in pygame