rotation image pygame

Solutions on MaxInterview for rotation image pygame by the best coders in the world

showing results for - "rotation image pygame"
Mira
26 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 set image rotationpygame sizepygame scale imagepygame rotate a drawn recthow to resize an image pygamerotating image pygamepygame rotate matrixhow to resize imgs in pygamehow to rotate a picture in pygamepygame set rotation of imagepygame scale surfacepygame rotate image around pivotpygame rotate imagespygame transform recalepygame trasnform scalepygame transform rotate objectpygame make an image rotate around a pointhow to rotate images in pygame on an axispygame rotate transformpygame rotate spritesmooth transformation pygamerezise object pygamehow to resize an image in pygamehow to rotate image in pygamepygame transform scale 2xhow to make a cimage grow then shrink in pygamerotate recntanlge image pygamepygame transform scalerotation image pygamepygame image resizerotate rect pygamehow to rotate an image pygamehow can we rotate a image about a point in pygamepygame crop imagepython pygame rotate functionhow to transform scale in pygamepygame image scalepygame image rotate centerpygame rect rotaterotate image crashing pygamehow to rotate an image with pygamerotate image around center pygamerotate a surface pygmaehow to rotate a image to mouse in pygamepygame scaling imagepygame make an image rotate around a point outside the imagehow to scale and rotate image in pygametransform scale pygamepygame rotate image around centerhow to rotate an image around it center in pygamepygame change angle imageshow to rotate an image in pygame around a pointtranformation image in pygamepygame image rotateset rotation of image pygamescale transformation image in pygamepygame transform fliphow to rotate a image in pygamewscale pygame imagerotate pygamerotate image pygaemresize surface pygamepygame image transformresize pygame imagetransform pygamehow to rotate image through mouse in pygamepytgame transform rotate 28 29how to rotate img in pygame horizontalypygame transform flippygame rotationscale image pygamepygame transform scale examplehow to rotate a sprite pygameresize image pygamescale surface pygamepygame transformpygame rotate objectpygame transform laplacian 28 29how to rotate images in pygamepygame change surface sizepygame transform rotatepygame scalerotate image pygamehow to rotate from centre of image pygamehow to scale and transform image pygamehow to resize images in pygamepygame transform rotatescaling pygame imagepygasme scalepygame resize imagepygame rotate image to mousepygame rotate image pygame zoomtransform image pygamepygame make an image rotate over a pointpygame transformpygame image rotateset rotation of image in pygamehow to rotate a image about a point in pygamehow to resize a image pygamehow to rotate an image using rect and vector in pygamehow to make pygame tranform scale betterhow to rotate picture in pygamepygame image transform rotate 28 29pygame rotate image 5cpygame transform scaleresizing images pygamescale tranformation image in pygamepygame rotate surfacepygame rotate image around pointrotate in pygamepygame rotate memorypygame rotatingtransform rotate pygamepygame flip imageimage pygame transform positionpygame image rotationhow to rotate a image in pygamescale pygame surfacepygame blit rotationpygame surface get rotationpygame function that rotate an imagerotate image python pygamehow to rotate img in pygamehow to rotate image to a angle in pygamerotate an image pygamepygame rotating imagehow to make an image rotate in pygamerotater img pygametransform an image on a surfacepygame rotate image centerhow to rotate image pygamerotate photo in pygamepygame transform flip vs rotateblit rotate pygameptgame rotate imagerotate image from center pygamepygame rotatepygame transform rotozoompygame transform rotatepygame change angle of imagepygame transform rotate 28 29rotate img pygamerotate image in pygamerotate images in pygamehow to rotate an image in pygamerotation image pygame